day008-01

监听器

@Override
public void windowClosing(WindowEvent e) {
    System.out.println("widow closing");
    System.exit(0);
}

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

键盘监听

public class TestKeyListener {
    public static void main(String[] args) {
        //键盘监听事件测试
        new KeyFrame();
    }

}
class KeyFrame extends Frame{
    public KeyFrame(){
        setBackground(Color.BLUE);
        setBounds(200,200,400,400);
        setVisible(true);
        addKeyListener(new Mylisten());

    }
    class Mylisten extends KeyAdapter {
        public Mylisten() {
            super();
        }

        @Override
        public void keyTyped(KeyEvent e) {
            System.out.println("keyType类型");
        }

        @Override
        public void keyPressed(KeyEvent e) {
            System.out.println("keyPressed 按下");
            int keyCode = e.getKeyCode();
            System.out.println("keyCode:"+keyCode);
            //可以用来得到键盘对应的数字 左上右下 37 38 39 40  空格32 回车10
        }

        @Override
        public void keyReleased(KeyEvent e) {
            System.out.println("keyReleased放开");
        }
    }

}

Swing

Awt的扩展 可以画很多部件下拉框之类的

JFrame 演示

public class JframeDemo {
    public static void main(String[] args) {
        new JframeDemo().init();
    }
    public void init(){
        JFrame jFrame = new JFrame("this is jframe");
        jFrame.setBackground(Color.BLUE);//此处不生效因为他是个最外层窗口需要获取它容器本身
        jFrame.setBounds(200,200,400,400);
        jFrame.setVisible(true);
        Container container = jFrame.getContentPane();
        container.setBackground(Color.RED);

        //设置文字
        JLabel jlable = new JLabel("welcome to my jframe");
//        jFrame.add(jlable);
        container.add(jlable);
        //文本标签居中
        jlable.setHorizontalAlignment(SwingConstants.CENTER);
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//默认的关闭窗口事件监听
    }
}

Dialog

jDialog 弹窗

public class DialogDemo extends JFrame{

    //构造直接生成窗口
    public DialogDemo(){
        this.setVisible(true);
        this.setSize(500,500);
//        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//弹窗已经默认有关闭
        JButton jButton = new JButton("JBution点击弹出窗口");
        jButton.setBounds(50,50,30,15);
        //点击弹出窗口
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //弹出窗口
                new MyDialog1();
            }
        });
        this.getContentPane().add(jButton);
    }
    class MyDialog1 extends JDialog{
        public MyDialog1(){
            setVisible(true);
            setBounds(300,300,300,300);
        }
    }
    public static void main(String[] args) {
        new DialogDemo();
    }
}
posted @ 2021-01-05 16:47  崇之他和她  阅读(44)  评论(0)    收藏  举报

作者:{author}

出处:{post_url}

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。