java怎样获取鼠标在屏幕的坐标

java怎样获取鼠标在屏幕的坐标,第1张

代码如下:

import javaawtBorderLayout;

import javaawtFlowLayout;

import javaxswingJButton;

import javaxswingJDialog;

import javaxswingJFrame;

import javaxswingJPanel;

import javaxswingborderEmptyBorder;

import javaxswingJLabel;

import javaawtFont;

import javaawtPoint;

import javautilTimer;

import javautilTimerTask;

import javaawtColor;

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_framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

            info_framesetVisible(true);

            info_framesetAlwaysOnTop(true);

            Timer timer = new Timer();

            timerschedule(new TimerTask() {                

            @Override

            public void run() {

                  Point point = javaawtMouseInfogetPointerInfo()getLocation();                    

                  // Systemoutprintln("Location:x=" + pointx + ", y=" +

                  // pointy);

                    info_framevalue_xsetText("" + pointx);

                    info_framevalue_ysetText("" + pointy);

                }

            }, 100, 100);

        } catch (Exception e) {

            eprintStackTrace();

        }

    }    

    /

      Create the dialog

     /

    public MouseInfo() {

        setTitle("\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668");

        setBounds(100, 100, 217, 156);

        getContentPane()setLayout(new BorderLayout());

        contentPanelsetBorder(new EmptyBorder(5, 5, 5, 5));

        getContentPane()add(contentPanel, BorderLayoutCENTER);

        contentPanelsetLayout(null);

        JLabel lblx = new JLabel("\u5750\u6807x:");

        lblxsetFont(new Font("宋体", FontPLAIN, 15));

        lblxsetBounds(22, 27, 66, 31);

        contentPaneladd(lblx);

        JLabel lbly = new JLabel("\u5750\u6807y:");

        lblysetFont(new Font("宋体", FontPLAIN, 15));

        lblysetBounds(22, 68, 66, 31);

        contentPaneladd(lbly);

        value_x = new JLabel("0");

        value_xsetForeground(ColorBLUE);

        value_xsetFont(new Font("宋体", FontPLAIN, 20));

        value_xsetBounds(82, 27, 66, 31);

        contentPaneladd(value_x);

        value_y = new JLabel("0");

        value_ysetForeground(ColorBLUE);

        value_ysetFont(new Font("宋体", FontPLAIN, 20));

        value_ysetBounds(82, 68, 66, 31);

        contentPaneladd(value_y);

    }

}

class InputStatus{

int mouseX;

int mouseY;

}

InputStatus inputStatus=new InputStatus();

private final MouseMotionListener mouseMotionListener = new MouseMotionListener() {

public void mouseMoved(MouseEvent e) {

synchronized (inputStatus) {

inputStatusmouseX = egetX();

inputStatusmouseY = egetY();

}

}

public void mouseDragged(MouseEvent e) {

synchronized (inputStatus) {

inputStatusmouseX = egetX();

inputStatusmouseY = egetY();

}

}

};

然后把mouseMotionListener给add到你的窗口或者控件上就行。

InputStatus是自己写的内部类,用于存放鼠标的位置,这样在其他地方就可以用inputStatusmouseX和inputStatusmouseY来取了。synchronized是为了万一你取坐标的代码在其他线程里(你那个线程也要synchronized (inputStatus)),可以确保每次mouseX和mouseY是成对写入和成对读出的。

当然如果你是单线程的应用的话,可以不要synchronize,然后去掉inputStatus相关的代码,类的成员变量这么写

int mouseX,mouseY;

然后处理函数这么写

public void mouseDragged(MouseEvent e) {

mouseX = egetX();

mouseY = egetY();

}

这样比较简单

在Java版中,通过按F3(在某些键盘上需要按Fn_+_F3)可以打开带有诸多信息的调试界面,其中在屏幕左上角就有您的当前坐标。

在基岩版中,可以通过更改世界选项来显示玩家所在位置的方块坐标。如果把“显示坐标”选项打开,则坐标将显示在左上角的框中。可以在创建世界菜单中打开选项。

以上就是关于java怎样获取鼠标在屏幕的坐标全部的内容,包括:java怎样获取鼠标在屏幕的坐标、求救Java 鼠标移动获取坐标问题.、笔记本电脑java怎么打开坐标等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9777940.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存