GUI编程day05

GUI编程day05

窗口监听

public class TestWindow {
    public static void main(String[] args) {
        WindowFrame windowFrame = new WindowFrame();

    }
}

class WindowFrame extends Frame {
    public WindowFrame() {
        setBackground(Color.PINK);
        setVisible(true);
        setBounds(400, 100, 400, 500);
        //addWindowListener(new MyWindowListener());

        this.addWindowListener(
                //匿名内部类
                new WindowAdapter() {

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

                    //激活窗口
                    @Override
                    public void windowActivated(WindowEvent e) {
                        WindowFrame source = (WindowFrame) e.getSource();
                        source.setTitle("被激活了");// 隐藏一次后赋值title
                        System.out.println("windowActivated");
                    }
                }
        );
    }
}

键盘监听

//键盘
public class TestKeyListener {
    public static void main(String[] args) {
        KeyFrame keyFrame = new KeyFrame();
    }

}
class KeyFrame extends Frame {
    public KeyFrame() {
        setBounds(1,2,300,400);
        setVisible(true);
        this.addKeyListener(new KeyAdapter() {
            //键盘按下
            @Override
            public void keyPressed(KeyEvent e) {
                //获得键盘按下的键是哪一个,当前的码
                int keycode = e.getKeyCode();//不需要去记录这个数值,直接使用静态属性 VK_XXXX
                System.out.println(keycode);
                if(keycode == KeyEvent.VK_UP){
                    System.out.println("你按下了上建");
                }
                //根据按下不同操作,产生不同结果
            }
        });
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值