import javax.swing.*;
import java.awt.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class mee {
public static void main(String[] args){
JFrame childfr=new myJFrame();
childfr.setSize(300,400);
childfr.show();
}static class myJFrame extends JFrame implements ActionListener {
public myJFrame ()
{
super();
Container con=getContentPane();
JPanel pan=new JPanel();
JButton bt=new JButton("关闭窗口");
bt.addActionListener(this);
pan.add(bt);
con.add(pan); } public void actionPerformed(ActionEvent e){ System.exit(-1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
}
import java.awt.*;
import java.awt.event.*;/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/public class mee {
public static void main(String[] args){
JFrame childfr=new myJFrame();
childfr.setSize(300,400);
childfr.show();
}static class myJFrame extends JFrame implements ActionListener {
public myJFrame ()
{
super();
Container con=getContentPane();
JPanel pan=new JPanel();
JButton bt=new JButton("关闭窗口");
bt.addActionListener(this);
pan.add(bt);
con.add(pan); } public void actionPerformed(ActionEvent e){ System.exit(-1);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
}
public class AFrame extends Frame{
AFrame(String title){
super();
MyWindowAdapter adapter=new MyWindowAdapter(this); public boolean handleEvent(Event evt)
{
if(evt.id==Event.WINDOW_DESTROY)System.exit(0);
return super.handleEvent(evt);
}
}
……
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
改为System.exit(0);即可
MyWindowAdapter adapter=new MyWindowAdapter(this);
addWindowListener(adapter); 同理吧! 按你的做了,还是不行
to iamwls(我想天是我) : 我试过,关不掉; 按你的意思至少应该隐藏啊,但它也不隐藏的啊。:(
又没有给win 添加事件接收器。
ok了 dylsz(东阳江) ,让我亲你一下吧 :0
只是这里错了 Frame win = new Frame("My Cool Window"); 创建对象用错了类,是我的疏忽;不过事件的接受器是有的: class MyWindowAdapter extends WindowAdapter{
AFrame aFrame;
public MyWindowAdapter(AFrame aFrame){
this.aFrame=aFrame;
}
public void windowClosing(WindowEvent e){
aFrame.setVisible(false);
} 事件监听器接口实例的添加: MyWindowAdapter adapter=new MyWindowAdapter(this);
addWindowListener(adapter);