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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM