看毕老师的视频,讲窗体事件那一节。
毕老师演示的是程序一运行,就会先把windowActivated 和 windowOpened运行一遍。如图bi.png
而我的却两个都不运行。只有再次激活时,才会运行windowActivated,而windowOpened却一直没有运行。如图mine1.png mine2.png
- import java.awt.*;
- import java.awt.event.*;
- class AwtDemo
- {
- public static void main(String[] args)
- {
- Frame f = new Frame();
- f.setVisible(true);
- f.setSize(500,100);
- f.setLocation(300,150);
- f.setLayout(new FlowLayout());
- Button b = new Button("小按钮");
- //f.addWindowListener(new MyWindowAdapter());
- //匿名内部类方式添加窗体监听器
- f.addWindowListener(new WindowAdapter()
- {
- public void windowClosing(WindowEvent e)
- {
- System.out.println("正在关闭");
- System.exit(0);
- }
- public void windowActivated(WindowEvent e)
- {
- System.out.println("激活");
- }
- public void windowOpened(WindowEvent e)
- {
- System.out.println("打开");
- }
- });
- f.add(b);
- }
- }
- class MyWindowAdapter extends WindowAdapter
- {
- public void windowClosing(WindowEvent e)
- {
- System.out.println("关闭");
- }
- }
复制代码
|
|