原代码如下,生成的frame窗口不可关闭
package FrameTest;
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameTest1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(200, 800, 100, 100);
frame.setTitle("第一个窗口");
frame.setResizable(true);
frame.setBackground(Color.GRAY);
frame.setVisible(true);
}
}
然后增加下面这一段代码
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
完整代码成了:
import java.awt.Color;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class FrameTest1 {
public static void main(String[] args) {
Frame frame = new Frame();
frame.setBounds(200, 800, 100, 100);
frame.setTitle("第一个窗口");
frame.setResizable(true);
frame.setBackground(Color.GRAY);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
这样运行出来的frame窗口就可以关闭了