关于事件处理监听器的疑问???!!!求解惑!!
本帖最后由 oolocal 于 2012-12-14 13:37:49 编辑
刚开始写窗体程序,添加了一些按钮,并给这些按钮添加了监听器,当监听器方法和按钮在同一个类当中的时候能够很好的运行。现在我想要将窗体和监听器分别放在不同的类当中,然后通过类的调用来实现事件的处理,但是在点击的时候监听器没有起作用,反而弹出一些错误,我也看不懂那些错误到底是什么意思。哪位大神来帮忙解惑,为什么将监听器与窗体按钮放在不同的类中就不能很好的执行事件处理??
代码如下:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class csClientATM {
public static void main(String args[]){
ATMframe winATM=new ATMframe();
winATM.clientFrame("客户端ATM");
}
}
class ATMframe extends JFrame {
//JTextField showText;
JTextArea showText;
JButton nuButton[];
JButton queryButton,cashButton,sureButton,cancelButton,exitButton;
JPanel textPanel,buttonPanel;
Box baseBox,nubuttonBox,selectBox,nubox1,nubox2,nubox3,nubox4;
public void clientFrame(String s){
Suprem suprem =new Suprem() ;
//showText=new JTextField(20);
showText=new JTextArea(5,20);
nuButton=new JButton[10];
queryButton=new JButton("查询");
cashButton=new JButton("取款");
sureButton=new JButton("确定");
cancelButton=new JButton("清除");
exitButton=new JButton("退出");
textPanel=new JPanel();
buttonPanel=new JPanel();
nubox1=Box.createHorizontalBox();
nubox2=Box.createHorizontalBox();
nubox3=Box.createHorizontalBox();
nubox4=Box.createHorizontalBox();
baseBox=Box.createVerticalBox();
nubuttonBox=Box.createVerticalBox();
selectBox=Box.createVerticalBox();
showText.setText("欢迎使用ATM,请输入账号:");
showText.setEditable(false);
textPanel.add(showText);
buttonPanel.setLayout(new FlowLayout());
for(int i=0;i<10;i++)
{
nuButton[i]=new JButton(""+(i+1));
nuButton[i].addActionListener(suprem);
cancelButton.addActionListener(suprem);
if(i<3){
nubox1.add(nuButton[i]);
nubox1.add(Box.createHorizontalStrut(10));
}
else if(i>=3&&i<6){
//nuButton[i].addActionListener(suprem);
nubox2.add(nuButton[i]);
nubox2.add(Box.createHorizontalStrut(10));
}
else if(i>=6&&i<9){
//nuButton[i].addActionListener(suprem);
nubox3.add(nuButton[i]);
nubox3.add(Box.createHorizontalStrut(10));
}
else{
nuButton[i]=new JButton("0");
//nuButton[i].addActionListener(suprem);
//nuButton[i].setSize(getSize());
/*JPanel lsPanel=new JPanel(); 在box中添加面板后,增大了1--9按钮和0按钮之间的距离
FlowLayout flow=new FlowLayout();
flow.setAlignment(FlowLayout.LEFT);
lsPanel.setLayout(flow);
lsPanel.add(nuButton[i]);*/
nubox4.add(nuButton[i]);
nubox4.add(Box.createHorizontalStrut(40));
//cancelButton.setPreferredSize(nuButton[i].getPreferredSize());
nubox4.add(cancelButton);
nubox4.add(Box.createHorizontalStrut(10));
}
}
nubuttonBox.add(nubox1);
nubuttonBox.add(Box.createVerticalStrut(6));
nubuttonBox.add(nubox2);
nubuttonBox.add(Box.createVerticalStrut(6));
nubuttonBox.add(nubox3);
nubuttonBox.add(Box.createVerticalStrut(6));
nubuttonBox.add(nubox4);
queryButton.addActionListener(suprem);
cashButton.addActionListener(suprem);
sureButton.addActionListener(suprem);
exitButton.addActionListener(suprem);
selectBox.add(queryButton);
selectBox.add(Box.createVerticalStrut(6));
selectBox.add(cashButton);