Frame(非JFrame)如何在按关闭按钮时关闭

来源:互联网 发布:云计算架构师要学什么 编辑:程序博客网 时间:2024/05/01 01:50

如果是JFrame, 则调用这段代码即可frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

但是如果是Frame则应该如何关闭呢, 下面就介绍一种方法:

 

package cn.edu.practice.awt;

 

import java.awt.FlowLayout;

import java.awt.Frame;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

 

public class TestCloseFrame extends Frame{

 

  /**
   * @param args
   */
  public static void main(String[] args) {

    TestCloseFrame tcf= new TestCloseFrame ();
    tcf.setLayout(new FlowLayout());
    tcf.setSize(500, 400);

    tcf.addWindowListener(new TestWindowListener());

  }

}

 

class TestWindowListener implements WindowListener{

  public void windowActivated(WindowEvent e) {
    // TODO Auto-generated method stub
   
  }

 

  public void windowClosed(WindowEvent e) {   }

  public void windowClosing(WindowEvent e) {
    e.getWindow().dispose();
   
  }

 

  public void windowDeactivated(WindowEvent e) { }

  public void windowDeiconified(WindowEvent e) { }

  public void windowIconified(WindowEvent e) { }

  public void windowOpened(WindowEvent e) { } 
 
}

原创粉丝点击