AI智能
改变未来

GUI编程核心技术AWT——窗口监听


GUI编程核心技术AWT——窗口监听

package com.wei.lesson05;import java.awt.*;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;//窗口监听public class TestWindow {public static void main(String[] args) {new WindowFrame();}}class WindowFrame extends Frame{public WindowFrame() {setVisible(true);setBackground(Color.green);setBounds(100,200,200,300);//addWindowListener(new MyWindowListene());this.addWindowListener(//匿名内部类   WindowAdapter是个抽象类,也是WindowListener的实现类new WindowAdapter(){@Override//窗口关闭public void windowClosing(WindowEvent e) {System.out.println(\"windowClosing\");System.exit(0);}@Override//窗口已激活public void windowActivated(WindowEvent e) {WindowFrame source = (WindowFrame)e.getSource();//获取资源System.out.println(\"windowActivated\");source.setTitle(\"被激活了\");}});}/*//监听器          继承WindowListener的实现类,就不用重写WindowListener那么多的方法了class MyWindowListene extends WindowAdapter{@Overridepublic void windowClosing(WindowEvent e) {setVisible(false);//隐藏窗口  最小化System.exit(0);//正常退出  0  非正常退出  1}}*/}

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » GUI编程核心技术AWT——窗口监听