JAVA Frame 响应窗口关闭事件
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package Window; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; class FirstWindow extends Frame { MenuBar menubar; Menu menu; MenuItem item1, item2; FirstWindow(String s) { setTitle(s); Toolkit tool = getToolkit(); Dimension dim = tool.getScreenSize(); setBounds(0, 0, 352, 288); menubar = new MenuBar(); menu = new Menu("文件"); item1 = new MenuItem("打开"); item2 = new MenuItem("保存"); menu.add(item1); menu.add(item2); menubar.add(menu); setMenuBar(menubar); setVisible(true); this.addWindowListener(new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { System.exit(0); } }); } } public class Window { public static void main(String args[]) { FirstWindow win = new FirstWindow("一个带菜单的窗口"); } }
看清楚,不是JFrame的派生类~~~
如果是JFrame 只需要加一句话: setDefaultCloseOperation