别忘了事件监听,如下:
frame.addWindowListener(new WinedowAdapter() {
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
frame.dispose();
System.exit(0);
}
});
frame.addWindowListener(new WinedowAdapter() {
public void windowClosing(WindowEvent e) {
frame.setVisible(false);
frame.dispose();
System.exit(0);
}
});
javax.swing
Interface WindowConstantsstatic int DISPOSE_ON_CLOSE
The dispose-window default window close operation
static int DO_NOTHING_ON_CLOSE
The do-nothing default window close operation
static int EXIT_ON_CLOSE
The exit application default window close operation.
static int HIDE_ON_CLOSE
The hide-window default window close operation
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
就是为了关闭窗口。
beyond_xiruo(希偌)提到的是1.3以前的做法,不过为了兼容,很多书都这么写,而不是像2中那样写,比如TIJ。编写这种程序,首先应该想到的就是写好这个退出的操作,不然,会给自己带来一些不必要的麻烦。
我试了,可出错:
frame.addWindowListener(new WindowAdapter() {
^
我用的是jdk1.4