zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    十道海量数据处理面试题与十个方法大总结[转]
    Velocity常用语法详解
    你选择哪一种方式创建线程?
    通俗易懂地讲解TCP建立连接的三次握手和释放连接的四次挥手
    由浅入深的理解网络编程【转】
    [转载]OSI七层模型详解
    简单谈谈数据库索引
    【HBase】 常用命令
    【HBase】知识小结+HMaster选举、故障恢复、读写流程
    【HBase】HBase架构中各种组件的作用
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10756404.html
Copyright © 2011-2022 走看看