如果是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) { }

}

如果是JFrame, 则调用这段代码即可frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);但是如果是Frame则应该如何关闭呢, 下面就介绍一种方法: package cn.edu.practice.awt; import java.awt.FlowLayout;import java.awt.Frame;import
枚举也是类,在定义枚举的 候,其实是定义了一个枚举集合,然后在这个集合中包含一些列枚举值。比如性别可以是一个枚举集合,可能中包含男性、女性等枚举值。定义枚举集合以性别这个枚举集合为例,先来定义两个枚举值,分别为MALE和FEMALE:publicenum Gender { MALE, FEMALE JAVA编译器会把这MALE和FEMALE转成两个枚举值。枚举类-Enum(name,
closeButton = new JButton("Close"); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { dispose(); JPanel panel = new JPanel(); panel.add(closeButton); setContentPane(panel); pack(); setVisible(true); public static void main( String [] args) { new My Frame (); 这个代码创建了一个 JFrame 窗口,其中包含一个 按钮 ,点击 按钮 后会调用dispose()方法 关闭 窗口。其中,dispose()方法会释放窗口占用的所有资源,并且会触发WindowListener中的windowClosed()方法。如果你想在 关闭 窗口前做一些操作,可以重写windowClosing()方法。 注意,在这个示例中,我们使用了setDefaultCloseOperation( JFrame .EXIT_ON_CLOSE)方法来设置窗口 关闭 的默认操作,即 退出 程序。如果你不想 退出 程序,可以使用其他选项,比如 JFrame .DISPOSE_ON_CLOSE。