JAVA的学习日记21-GUI编程4

2021-03-19 00:27

阅读:722

标签:override   owa   vat   awt   window   class   激活   apt   ada   

窗口监听事件

package com.JavaGUI.Demo03;

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(){
        setBackground(Color.BLUE);
        setBounds(100, 100, 200, 200);
        setVisible(true);

        //自己写窗口监听事件
        //addWindowListener(new MyWindowListener());


        this.addWindowListener(
                //匿名内部类,推荐这样写
                new WindowAdapter() {

                    //关闭中
                    @Override
                    public void windowClosing(WindowEvent e) {
                        System.out.println("关闭中");
                        System.exit(0);
                    }

                    //激活
                    @Override
                    public void windowActivated(WindowEvent e) {
                        System.out.println("激活");
                    }

                }
        );
    }

//    class MyWindowListener extends WindowAdapter{
//        @Override
//        public void windowClosing(WindowEvent e) {
//            setVisible(false);//设置不可见
//            System.exit(0);
//        }
//    }

}

键盘监听事件

package com.JavaGUI.Demo03;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class KeyListener {
    public static void main(String[] args) {
        new KeyFrame();
    }
}

class KeyFrame extends Frame{
    public KeyFrame(){
        setBounds(10, 10, 200, 200);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                int code = e.getKeyCode();
                //想要使用什么按键,就使用KeyEvent.VK_XXX
                if(code == KeyEvent.VK_UP){
                    System.out.println("输出了上键");
                }
            }
        });
    }

}

JAVA的学习日记21-GUI编程4

标签:override   owa   vat   awt   window   class   激活   apt   ada   

原文地址:https://www.cnblogs.com/cdoudou/p/13764697.html


评论


亲,登录后才可以留言!