• JavaSE 鼠标事件类(MouseEvent)实现


    {相关信息}
    鼠标事件类(MouseEvent)指组件中发生的鼠标动作事件,例如按下鼠标、释放鼠标、单击鼠标、鼠标光标进入或离开组件的几何 图形、移动鼠标、拖动鼠标。当鼠标移动到某个区域或鼠标单击某个组件时就会触发鼠标事件。使用鼠标事件必须给组件添加一个MouseListener 接口的事件处理器,该接口包含以下 5 个方法:
    void mouseClicked(MouseEvent e):当鼠标在该区域单击时发生
    void mouseEntered(MouseEvent e):当鼠标进入该区域时发生
    void mouseExited(MouseEvent e):当鼠标离开该区域时发生
    void mousePressed(MouseEvent e):当鼠标在该区域按下时发生
    void mouseReleased(MouseEvent e):当鼠标在该区域放开时发生

    {实现效果图}
    实验过程中,在panel区,明显可以看出单击可按下的区别。按一下马上松开是一个点击,获取一次位置信息。
    如果按下不松开,移动后一会儿,再松开,不会获取位置信息。

    {相关代码}

    package sup.orange.learn;
    
    import java.awt.*;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    
    /**
     * Created by re-x on 11/2/14.
     */
    public class MouseEventDemo extends Frame {
    
        Panel panel;
        TextField textfield1, textfield2;
    
        public MouseEventDemo () {
            super();
            init();
        }
    
        public static void main(String[] args) {
            new MouseEventDemo();
        }
    
        public void init() {
            setLayout(new GridLayout(3, 1));
            textfield1 = new TextField(30);
            textfield2 = new TextField();
            add(textfield1);
            add(textfield2);
    
            panel = new Panel();
            panel.setBackground(Color.CYAN);
            add(panel);
    
            panel.addMouseListener(new MouseListener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    textfield2.setText("x="+e.getX()+";y="+e.getY());
                }
    
                @Override
                public void mousePressed(MouseEvent e) {
                    textfield1.setText("mouse pressed");
                }
    
                @Override
                public void mouseReleased(MouseEvent e) {
                    textfield1.setText("mouse released");
                }
    
                @Override
                public void mouseEntered(MouseEvent e) {
                    textfield1.setText("mouse entered");
                }
    
                @Override
                public void mouseExited(MouseEvent e) {
                    textfield1.setText("mouse exited");
                }
            });
    
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    super.windowClosing(e);
                    dispose();
                    System.exit(0);
                }
            });
    
            setSize(200, 300);
            setVisible(true);
        }
    }
  • 相关阅读:
    [国家集训队]数颜色 / 维护队列
    [SP3267]DQUERY
    扩展欧几里得算法详解(exgcd)
    [CTSC2018]混合果汁
    极角排序那些事
    向量的点乘与叉乘学习笔记
    [APIO2014]序列分割
    CF1148D-Dirty Deeds Done Dirt Cheap
    CF176E Archaeology(set用法提示)
    【网络流24题】最长不下降子序列问题
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4223363.html
Copyright © 2020-2023  润新知