日期:2014-05-20  浏览次数:20650 次

java图形界面的一个小问题,纠结了好久了。
Java code
import java.awt.*;
import java.awt.event.*;
import javax.swing.JFrame;
class Selection extends JFrame implements ActionListener {

    Button c1;
    Panel p = new Panel();
    Panel p1 = new Panel();
    public Selection(String s) {
        super(s);
        Label r = new Label("请选择你要的服务");
        c1 = new Button("查询余额");
        p.add(r);
        p1.add(c1);
        this.setLayout(new GridLayout(6, 1));
        this.add(p);
        this.add(p1);
         // /*各种服务功能*/
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        }); // closing the window
        c1.addActionListener(this);
        setBounds(150, 150, 200, 200);
        setVisible(true);

    }// 构造函数

    public void actionPerformed(ActionEvent e){
           if(e.getSource()==c1){

           dispose();
           BalanceChecck bc=new BalanceChecck("查询余额");}
}
    }
/*查询余额*/
class BalanceChecck extends Frame implements ActionListener {

    Button ba;
    Panel a=new Panel();
    public BalanceChecck(String s){
    super(s);
    Button ba=new Button("返回");
    this.add(a);
    a.add(ba);
    ba.addActionListener(this);
    setLayout(new FlowLayout());
    setVisible(true);
    setBounds(150, 150, 200, 200);
    
    this.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    }); // closing the window
        
   }//public
    
    public void actionPerformed(ActionEvent e){
        if(e.getSource()==ba){
            Selection s = new Selection("选择");
            dispose();
            }//if
    }
}
public  class test {
    public static void main(String args[]) {
        new Selection("ff");
        
    }

}

为什么我点返回按钮会没用啊,e.getSource()==ba为什么没有执行,还是我下面的代码写错了?

------解决方案--------------------
Java code
 Button ba;
    Panel a=new Panel();
    public BalanceChecck(String s){
    super(s);
    Button ba=new Button("返回");
    this.add(a);