Java程序右上角红叉的响应
我用SWING做的程序,希望用户单击右上角红叉的时候执行一段代码……这个应该怎么做?
[解决办法]
使用WindowsListener进行窗口关闭事件的监听,给你贴一个实例
import javax.swing.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * Created by IntelliJ IDEA. * User: admin * Date: 2011-9-26 * Time: 16:11:50 * To change this template use File | Settings | File Templates. */public class Demo1 extends JFrame { private JButton btn=new JButton("click"); public Demo1(String title){ super(title); this.add(btn); this.pack(); this.setVisible(true); this.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) {// super.windowClosing(e); //To change body of overridden methods use File | Settings | File Templates. //这里写你想实现的代码,我只写了一行println System.out.println("windowClosing"); System.exit(0); } }); } public static void main(String[] args){ new Demo1("demo"); }}