美文网首页Java自学
Java自学-图形界面 事件监听

Java自学-图形界面 事件监听

作者: Wester西人 | 来源:发表于2020-08-12 13:09 被阅读0次

    Swing 如何进行事件监听

    示例 1 : 按钮监听

    创建一个匿名类实现ActionListener接口,当按钮被点击时,actionPerformed方法就会被调用

    按钮监听
    package gui;
      
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
      
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
      
    public class TestGUI {
        public static void main(String[] args) {
      
            JFrame f = new JFrame("LoL");
            f.setSize(400, 300);
            f.setLocation(580, 200);
            f.setLayout(null);
      
            final JLabel l = new JLabel();
      
            ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
            l.setIcon(i);
            l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
      
            JButton b = new JButton("隐藏图片");
            b.setBounds(150, 200, 100, 30);
      
            // 给按钮 增加 监听
            b.addActionListener(new ActionListener() {
      
                // 当按钮被点击时,就会触发 ActionEvent事件
                // actionPerformed 方法就会被执行
                public void actionPerformed(ActionEvent e) {
                    l.setVisible(false);
                }
            });
      
            f.add(l);
            f.add(b);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            f.setVisible(true);
        }
    }
    

    示例 2 : 键盘监听

    键盘监听器: KeyListener
    keyPressed 代表 键被按下
    keyReleased 代表 键被弹起
    keyTyped 代表 一个按下弹起的组合动作
    KeyEvent.getKeyCode() 可以获取当前点下了哪个键

    键盘监听
    package gui;
      
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
      
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
      
    public class TestGUI {
        public static void main(String[] args) {
      
            JFrame f = new JFrame("LoL");
            f.setSize(400, 300);
            f.setLocation(580, 200);
            f.setLayout(null);
      
            final JLabel l = new JLabel();
      
            ImageIcon i = new ImageIcon("e:/project/j2se/shana.png");
            l.setIcon(i);
            l.setBounds(50, 50, i.getIconWidth(), i.getIconHeight());
      
            // 增加键盘监听
            f.addKeyListener(new KeyListener() {
      
                // 键被弹起
                public void keyReleased(KeyEvent e) {
      
                    // 39代表按下了 “右键”
                    if (e.getKeyCode() == 39) {
                        // 图片向右移动 (y坐标不变,x坐标增加)
                        l.setLocation(l.getX() + 10, l.getY());
                    }
                }
      
                //键被按下
                public void keyPressed(KeyEvent e) {
                    // TODO Auto-generated method stub
      
                }
      
                // 一个按下弹起的组合动作
                public void keyTyped(KeyEvent e) {
      
                }
            });
      
            f.add(l);
      
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            f.setVisible(true);
        }
    }
    

    示例 3 : 鼠标监听

    MouseListener 鼠标监听器
    mouseReleased 鼠标释放
    mousePressed 鼠标按下
    mouseExited 鼠标退出
    mouseEntered 鼠标进入
    mouseClicked 鼠标点击
    在本例中,使用mouseEntered,当鼠标进入图片的时候,图片就移动位置

    鼠标监听
    package gui;
      
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.util.Random;
      
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
      
    public class TestGUI {
        public static void main(String[] args) {
      
            final JFrame f = new JFrame("LoL");
            f.setSize(800, 600);
            f.setLocationRelativeTo(null);
            f.setLayout(null);
      
            final JLabel l = new JLabel();
            ImageIcon i = new ImageIcon("e:/project/j2se/shana_heiheihei.png");
            l.setIcon(i);
            l.setBounds(375, 275, i.getIconWidth(), i.getIconHeight());
      
            f.add(l);
      
            l.addMouseListener(new MouseListener() {
      
                // 释放鼠标
                public void mouseReleased(MouseEvent e) {
                    // TODO Auto-generated method stub
      
                }
      
                // 按下鼠标
                public void mousePressed(MouseEvent e) {
                    // TODO Auto-generated method stub
      
                }
      
                // 鼠标退出
                public void mouseExited(MouseEvent e) {
                    // TODO Auto-generated method stub
      
                }
      
                // 鼠标进入
                public void mouseEntered(MouseEvent e) {
      
                    Random r = new Random();
      
                    int x = r.nextInt(f.getWidth() - l.getWidth());
                    int y = r.nextInt(f.getHeight() - l.getHeight());
      
                    l.setLocation(x, y);
      
                }
      
                // 按下释放组合动作为点击鼠标
                public void mouseClicked(MouseEvent e) {
                    // TODO Auto-generated method stub
      
                }
            });
      
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            f.setVisible(true);
        }
    }
    

    示例 4 : 适配器

    MouseAdapter 鼠标监听适配器
    一般说来在写监听器的时候,会实现MouseListener。
    但是MouseListener里面有很多方法实际上都没有用到,比如mouseReleased ,mousePressed,mouseExited等等。
    这个时候就可以使用 鼠标监听适配器,MouseAdapter 只需要重写必要的方法即可

    package gui;
      
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Random;
      
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
      
    public class TestGUI {
        public static void main(String[] args) {
      
            final JFrame f = new JFrame("LoL");
            f.setSize(800, 600);
            f.setLocationRelativeTo(null);
            f.setLayout(null);
      
            final JLabel l = new JLabel("");
      
            ImageIcon i = new ImageIcon("e:/project/j2se/shana_heiheihei.png");
            l.setIcon(i);
            l.setBounds(375, 275, i.getIconWidth(), i.getIconHeight());
      
            f.add(l);
      
            // MouseAdapter 适配器,只需要重写用到的方法,没有用到的就不用写了
            l.addMouseListener(new MouseAdapter() {
      
                // 只有mouseEntered用到了
                public void mouseEntered(MouseEvent e) {
      
                    Random r = new Random();
      
                    int x = r.nextInt(f.getWidth() - l.getWidth());
                    int y = r.nextInt(f.getHeight() - l.getHeight());
      
                    l.setLocation(x, y);
      
                }
            });
      
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
            f.setVisible(true);
        }
    }
    

    更多内容,点击了解: Swing 如何进行事件监听

    相关文章

      网友评论

        本文标题:Java自学-图形界面 事件监听

        本文链接:https://www.haomeiwen.com/subject/oujhdktx.html