public class Wuziqimain extends Frame implements WindowListener{
Wuziqimain(){
this.setSize(408,386);
this.setVisible(true);
this.setResizable(false);
this.setLocation(350, 150);
this.addWindowListener(this);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
new Wuziqimain();
}
//窗口的关闭事件
public void windowClosing(WindowEvent e) {
// TODO 自动生成方法存根
int resturn= JOptionPane.showConfirmDialog(this,
" 你确实要退出游戏吗?",
" 确 认", JOptionPane.YES_NO_OPTION);
if(resturn==JOptionPane.YES_OPTION) //返回的值
System.exit(0);
}
public void windowActivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowClosed(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeactivated(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowDeiconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowIconified(WindowEvent e) {
// TODO 自动生成方法存根
}
public void windowOpened(WindowEvent e) {
// TODO 自动生成方法存根
}
}
如果你关闭前不询问是否确认关闭,那么只需要在类的构造方法内写上:setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);就行了。
//安全退出程序即关闭窗口
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
});
package Window;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class AWT extends Frame
{
public static void main(String args[])
{
AWT frame=new AWT();
frame.setTitle("Awt frame");
frame.setSize(500, 500);
frame.setLocation(500, 100);
frame.setLayout(new FlowLayout());
frame.addWindowListener(
new WindowAdapter()
{
public void windowClosing(WindowEvent we)
{
System.exit(0);
}
}
);
}
}
//可以直接运行
java的demo里有。在java安装目录中有demo目录,进去找找看……