我做出来一个简单的界面,我想运行时点击窗口关闭按钮时只把这个窗口关闭但是不想结束程序运行,该怎么写,书上都是System.exit(0);直接就结束程序运行了。
------解决方案--------------------
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Test5 {
public static void main(String[] args){
final Frame f = new Frame();
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
f.setVisible(false);
}
});
f.setVisible(true);
}
}
------解决方案--------------------
也可以用dispose的
- Java code
import java.awt.*;import java.awt.event.*;public class TestFrame extends Frame{ public TestFrame() { this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent event) { dispose(); } }); } public static void main(String[] args) { Frame frame1 = new TestFrame(); Frame frame2 = new TestFrame(); frame1.pack(); frame2.pack(); frame1.setVisible(true); frame2.setVisible(true); }}