10.32windoweventsample.java

来自「JAVA程序设计的源代码」· Java 代码 · 共 30 行

JAVA
30
字号
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowEventSample extends JFrame implements WindowListener {
public WindowEventSample(){
super("Window Eyent Sample");    //显示调用父类方法
Container container = getContentPane();
container.setLayout(new  FlowLayout());    //设置容器为流布局
JLabel label1 = new JLabel("This is a Window event test!");
label1.setFont(new Font("Serif",Font.BOLD,20));    //设置标签字体
label1.setForeground(Color.red);    //设置标签字体颜色
container.add(label1);
addWindowListener(this);    //设置事件监听
setSize(300,200);
setVisible(true);
}
public void windowClosing (WindowEvent e) {    //窗口关闭事件处理函数
System.exit (0) ; //关闭窗口时退出
}
public void windowClosed(WindowEvent e)  {}    //各种窗口事件处理函数
public void windowOpened(WindowEvent e)  {}
public void windowIconified(WindowEvent e)  {}
public void windowDeiconified(WindowEvent e)  {}
public void windowActivated(WindowEvent e)  {}
public void windowDeactivated(WindowEvent e)  {}
public static void main(String []args)  {
JFrame sample = new WindowEventSample();
}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?