如果是JFrame, 则调用这段代码即可frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
但是如果是Frame则应该如何关闭呢, 下面就介绍一种方法:
package cn.edu.practice.awt;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class TestCloseFrame extends Frame{
/**
* @param args
*/
public static void main(String[] args) {
TestCloseFrame tcf= new TestCloseFrame ();
tcf.setLayout(new FlowLayout());
tcf.setSize(500, 400);
tcf.addWindowListener(new TestWindowListener());
}
}
class TestWindowListener implements WindowListener{
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
}
public void windowClosed(WindowEvent e) { }
public void windowClosing(WindowEvent e) {
e.getWindow().dispose();
}
public void windowDeactivated(WindowEvent e) { }
public void windowDeiconified(WindowEvent e) { }
public void windowIconified(WindowEvent e) { }
public void windowOpened(WindowEvent e) { }
}