我有一个button按钮“退出”,我想让它

setVisible(false);
dispose();
来退出程序。
这两个命令是窗口对象的。。按钮怎么得到窗口对象。。请教ActionListemer里的命令如何写
public class myActionListener implements ActionListener{

public void actionPerformed(ActionEvent e) {                if(e.getActionCommand().equals("退出"))
{

}
                 }
}
谢谢了。

解决方案 »

  1.   

    你的按钮对象不是建立在Frame的class里吗,你直接写this.dispose(),就OK啦啊.
      

  2.   

    JButton b = new JButton();
    b.addActionListener(this);
    public void actionPerformed(ActionEvent e){
     if(e.getSource == b){
        setVisible(false);
    }或者采用匿名内部类
    b.addActionListener(new EventHandle(){
       public void actionPerformed(ActionEvent e){
             if(e.getActionCommand().equalsIgnoreCase("exit"))
               setVisible(false);
    }
    });