JavaSE系列代码52介绍了窗口事件类WindowEvent的应用。WindowEvent是Java中用于处理窗口事件的类,它继承自ComponentEvent类。窗口事件主要包括窗口打开、关闭、最小化、最大化等操作。
以下是一个简单的示例,展示了如何使用WindowListener接口和WindowEvent类来处理窗口事件:
import java.awt.*;
import java.awt.event.*;
public class WindowEventDemo extends Frame implements WindowListener {
public static void main(String[] args) {
WindowEventDemo windowEventDemo = new WindowEventDemo();
windowEventDemo.addWindowListener(windowEventDemo);
windowEventDemo.setSize(400, 300);
windowEventDemo.setVisible(true);
}
public WindowEventDemo() {
super("WindowEvent Demo");
}
@Override
public void windowOpened(WindowEvent e) {
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
System.out.println("窗口关闭");
System.exit(0);
}
@Override
public void windowClosed(WindowEvent e) {
System.out.println("窗口已关闭");
}
@Override
public void windowIconified(WindowEvent e) {
System.out.println("窗口最小化");
}
@Override
public void windowDeiconified(WindowEvent e) {
System.out.println("窗口从最小化恢复");
}
@Override
public void windowActivated(WindowEvent e) {
System.out.println("窗口激活");
}
@Override
public void windowDeactivated(WindowEvent e) {
System.out.println("窗口失去焦点");
}
}
在这个示例中,我们创建了一个名为WindowEventDemo的类,该类继承了Frame类并实现了WindowListener接口。我们重写了WindowListener接口中的所有方法,以便在窗口事件发生时执行相应的操作。例如,当窗口打开时,我们打印出"窗口打开";当窗口关闭时,我们打印出"窗口关闭"并退出程序。
regular expression
A regular expression is a string containing some special characters, which are called metacharacters in regular expressions. For example, \ \ D in “\ \ DOK” is a special metacharacter, representing any one of 0 to 9. A regular expression is also called a pattern. The strings “9OK” and “1ok” are both strings matching the pattern: “\ \ DOK”. A string that matches a pattern is called a match pattern string, also known as a pattern match string.
import java.awt.*;
import java.awt.event.*;
public class Javase_51 extends Frame //定义主类
{
static Label lab=new Label();
static app13_6 frm=new app13_6();
static MyWinListener winlist= new MyWinListener();
public static void main(String[] args)
{
frm.setLayout(null); //取消页面设置
frm.setTitle("窗口事件");
frm.setBounds(120,50,215,100);
lab.setBounds(25,25,150,50);
frm.add(lab);
frm.addWindowListener(winlist); //设置winlist为frm的事件监听者
frm.setVisible(true);
}
//定义静态内部类MyWinListener并实现WindowListener接口
static class MyWinListener implements WindowListener
{
public void windowOpened(WindowEvent e) //打开窗口时的处理操作
{ lab.setText("打开新窗口"); }
public void windowActivated(WindowEvent e) //激活窗口时的处理操作
{ lab.setText("窗口被激活"); }
public void windowIconified(WindowEvent e) //窗口最小化时的处理操作
{ frm.setTitle("窗口被最小化"); }
public void windowDeiconified(WindowEvent e) //还原窗口时的处理操作
{ frm.setTitle("窗口被还原成正常大小"); }
public void windowClosing(WindowEvent e) //关闭窗口时的处理操作
{
frm.dispose(); //关闭窗口并释放资源
System.exit(0); //程序正常结束
}
public void windowDeactivated(WindowEvent e) //窗口失活时的处理操作
{ } //空操作
public void windowClosed(WindowEvent e) //窗口关闭后的处理操作
{ } //空操作
}
}