当前位置: 代码迷 >> Java相关 >> [求助]谁来帮忙加个关闭事件的语句
  详细解决方案

[求助]谁来帮忙加个关闭事件的语句

热度:214   发布时间:2006-08-02 17:27:04.0
[求助]谁来帮忙加个关闭事件的语句

import java.awt.*;
import java.awt.event.*;

class ChoiceTest extends Frame implements WindowListener {

Choice names = new Choice( );
//通过 add() 方法可以添加项目
ChoiceTest(String title) {
super(title);
names.add( "尤" );
names.add( "吴" );
names.add( "陈" );
names.add( "马" );
names.add( "耿" );
add( names );
addWindowListener(this);
}

public static void main(String args[] ){
ChoiceTest y = new ChoiceTest( "姓名选择框" );
y.setLayout(new FlowLayout());
y.setSize(400,250);
y.show();

}
public void windowClosing(WindowEvent e) { System.exit(-1); }
public void windowIconified(WindowEvent e) {}
public void windowDeiconified(WindowEvent e) {}
public void windowClosed(WindowEvent e) {}
public void windowOpened(WindowEvent e) {}

public void windowActivated(WindowEvent e) {}
public void windowDeactivated(WindowEvent e) {}
}
我感觉我这个关闭加的太麻烦了,单为了它实现一个接口...
请问有没有简单点的方法...

[此贴子已经被作者于2006-8-2 17:27:22编辑过]

搜索更多相关的解决方案: 语句  

----------------解决方案--------------------------------------------------------
有个适配器类不用这么麻烦。。。
----------------解决方案--------------------------------------------------------
f.addWindowListener(new MyWindowListener());

写成内部类,用WindowAdapter,代码便于阅读

f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});

----------------解决方案--------------------------------------------------------

谢了


----------------解决方案--------------------------------------------------------
  相关解决方案