zoukankan      html  css  js  c++  java
  • [工具04]java实现获取鼠标的坐标

    本篇博客其实没什么难度可言,在这里分享给大家,是因为有时候我们需要这个工具,java作为跨平台语言的优势在这个软件就可以体现出来,不需修改就可以在windows、mac、linux上使用这个软件。

    这个小工具主要是使用MouseInfo类实时获取鼠标的信息,然后再JDialog上显示出来。

    代码如下:

    
    import java.awt.BorderLayout;
    import java.awt.FlowLayout;
    
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.JLabel;
    import java.awt.Font;
    import java.awt.Point;
    import java.util.Timer;
    import java.util.TimerTask;
    import java.awt.Color;
    
    public class MouseInfo extends JFrame {
    
        private final JPanel contentPanel = new JPanel();
        JLabel value_x = null;
        JLabel value_y = null;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            try {
                MouseInfo info_frame = new MouseInfo();
                info_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                info_frame.setVisible(true);
                info_frame.setAlwaysOnTop(true);
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        Point point = java.awt.MouseInfo.getPointerInfo().getLocation();
                        // System.out.println("Location:x=" + point.x + ", y=" +
                        // point.y);
                        info_frame.value_x.setText("" + point.x);
                        info_frame.value_y.setText("" + point.y);
                    }
                }, 100, 100);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        /**
         * Create the dialog.
         */
        public MouseInfo() {
            setTitle("u9F20u6807u5750u6807u83B7u53D6u5668");
            setBounds(100, 100, 217, 156);
            getContentPane().setLayout(new BorderLayout());
            contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
            getContentPane().add(contentPanel, BorderLayout.CENTER);
            contentPanel.setLayout(null);
    
            JLabel lblx = new JLabel("u5750u6807x:");
            lblx.setFont(new Font("宋体", Font.PLAIN, 15));
            lblx.setBounds(22, 27, 66, 31);
            contentPanel.add(lblx);
    
            JLabel lbly = new JLabel("u5750u6807y:");
            lbly.setFont(new Font("宋体", Font.PLAIN, 15));
            lbly.setBounds(22, 68, 66, 31);
            contentPanel.add(lbly);
    
            value_x = new JLabel("0");
            value_x.setForeground(Color.BLUE);
            value_x.setFont(new Font("宋体", Font.PLAIN, 20));
            value_x.setBounds(82, 27, 66, 31);
            contentPanel.add(value_x);
    
            value_y = new JLabel("0");
            value_y.setForeground(Color.BLUE);
            value_y.setFont(new Font("宋体", Font.PLAIN, 20));
            value_y.setBounds(82, 68, 66, 31);
            contentPanel.add(value_y);
        }
    }
  • 相关阅读:
    解决:Could not resolve archetype org.apache.maven.archetypes
    Spring MVC配置MyBatis输出SQL
    Spring集成MyBatis 通用Mapper以及 pagehelper分页插件
    关于SpringMVC或Struts2接受参数接收不到的原因
    配置quartz启动时就执行一次
    ajaxFileUpload进行文件上传时,总是进入error
    spring mvc注入配置文件里的属性
    java中将一个文件夹下所有的文件压缩成一个文件
    flume failed to start agent because dependencies were not found in classpath
    ubuntu不能安装pip unable to install pip in unbuntu
  • 原文地址:https://www.cnblogs.com/jinhengyu/p/8073120.html
Copyright © 2011-2022 走看看