• java事件监听机制


    1.简单的事件监听

    package demo2;
    
    /*
     * java事件监听机制
     */
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Event;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.nio.ByteOrder;
    
    public class Demo1 extends JFrame implements ActionListener {
        JPanel m;
        JButton jb1, jb2;
    
        public static void main(String[] args) {
            Demo1 demo = new Demo1();
        }
    
        public Demo1() {
            m = new JPanel();
            jb1 = new JButton("黑色");
            jb2 = new JButton("红色");
            this.add(jb1, BorderLayout.NORTH);
            m.setBackground(Color.BLACK);
            this.add(m);
            this.add(jb2, BorderLayout.SOUTH);
            // 注册监听
            jb1.addActionListener(this);
            jb2.addActionListener(this);
            // 指定action命令
            jb1.setActionCommand("aa");
            jb2.setActionCommand("bb");
    
            this.setSize(400, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // 判断哪个按钮被点击
            if (e.getActionCommand().equals("aa")) {
                System.out.println("点击黑色");
                m.setBackground(Color.BLACK);
            } else if (e.getActionCommand().equals("bb")) {
                System.out.println("点击红色");
                m.setBackground(Color.red);
            } else {
                System.out.println("点击错误");
            }
        }
    }
    View Code

    2.设置普通类Cat进行监听

    package demo2;
    
    /*
     * java事件监听机制
     */
    import java.awt.*;
    import javax.swing.*;
    import java.awt.Event;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.nio.ByteOrder;
    
    public class Demo1 extends JFrame implements ActionListener {
        JPanel m;
        JButton jb1, jb2;
        Cat cat;
    
        public static void main(String[] args) {
            Demo1 demo = new Demo1();
        }
    
        public Demo1() {
            cat = new Cat();
            m = new JPanel();
            jb1 = new JButton("黑色");
            jb2 = new JButton("红色");
            this.add(jb1, BorderLayout.NORTH);
            m.setBackground(Color.BLACK);
            this.add(m);
            this.add(jb2, BorderLayout.SOUTH);
            // 注册监听
            jb1.addActionListener(cat);
            jb2.addActionListener(cat);
            // 指定action命令
            jb1.setActionCommand("aa");
            jb2.setActionCommand("bb");
    
            this.setSize(400, 300);
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
    
        }
    
        @Override
        public void actionPerformed(ActionEvent e) {
            // 判断哪个按钮被点击
            if (e.getActionCommand().equals("aa")) {
                System.out.println("点击黑色");
                m.setBackground(Color.BLACK);
            } else if (e.getActionCommand().equals("bb")) {
                System.out.println("点击红色");
                m.setBackground(Color.red);
            } else {
                System.out.println("点击错误");
            }
        }
    
        // 设计cat内部类
        class Cat implements ActionListener {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                // TODO Auto-generated method stub
                if (e.getActionCommand().equals("aa")) {
                    System.out.println("点击黑色");
                    m.setBackground(Color.BLACK);
                } else if (e.getActionCommand().equals("bb")) {
                    System.out.println("点击红色");
                    m.setBackground(Color.red);
                } else {
                    System.out.println("点击错误");
                }
            }
    
        }
    }
    View Code

    3.上下左右移动

    package demo3;
    
    //横着x 竖着y
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class Demo3 extends JFrame {
        Myframe mp;
    
        public static void main(String[] args) {
            Demo3 demo = new Demo3();
        }
    
        public Demo3() {
            mp = new Myframe();
            this.add(mp);
            this.addKeyListener(mp);
            this.setSize(400, 300);
    
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            this.setVisible(true);
        }
    }
    
    // Myframe定义的面板用于绘图
    class Myframe extends JPanel implements KeyListener {
        int x = 10, y = 10;
    
        public void paint(Graphics g) {
            super.paint(g);
            g.fillOval(x, y, 10, 10);
        }
    
        @Override
        public void keyPressed(KeyEvent e) {
            // TODO Auto-generated method stub
            if (e.getKeyCode() == KeyEvent.VK_DOWN) {
                y += 2;
                System.out.println("向下移动");
                this.repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_UP) {
                y -= 2;
                System.out.println("向上移动");
                this.repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
                x -= 2;
                System.out.println("向左移动");
                this.repaint();
            } else if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
                x += 2;
                System.out.println("向右移动");
                this.repaint();
            }
        }
    
        @Override
        public void keyReleased(KeyEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
        @Override
        public void keyTyped(KeyEvent arg0) {
            // TODO Auto-generated method stub
    
        }
    
    }
    View Code
  • 相关阅读:
    根据输入的个数,打印每行做多输出3个的for循环
    在启动页面后面再加载一个广告页,可以定制动画等
    frame.size.height无法直接赋值问题
    iOS开发远程推送
    iOS——UIKeyboardWillShowNotification 监听键盘高度变化
    iOS 浅谈本地通知 UILocalNotification
    iOS中assign、copy 、retain等关键字的含义
    GCD
    xocde快速定位崩溃代码
    关于xcode打包app
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10756404.html
Copyright © 2020-2023  润新知