「java获取鼠标坐标点」js获取鼠标点击位置坐标

博主:adminadmin 2023-01-14 03:51:09 341

本篇文章给大家谈谈java获取鼠标坐标点,以及js获取鼠标点击位置坐标对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java如何实时获取鼠标坐标,我只能实现首次坐标,并且鼠标移出在已进入才更新

import java.awt.Color;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;

import javax.swing.JLabel;

import javax.swing.JPanel;

public class MyFrame extends JFrame implements MouseMotionListener {

private JPanel jpan1;

private JLabel lab1;

private int x, y;

public MyFrame(String itie) {

super(itie);

setSize(500, 400);

setLocationRelativeTo(null);

setLayout(null);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getContentPane().setBackground(Color.white);// 设置JFrame窗体的背景色

jpan1 = new JPanel();

jpan1.setSize(20, 20);

jpan1.setBackground(Color.red);

//addMouseListener(this); // 添加鼠标事件

this.addMouseMotionListener(this);

add(jpan1);

lab1 = new JLabel("X,Y");

lab1.setBounds(100, 200, 100, 100);

add(lab1);

}

public static void main(String []args){

MyFrame m = new MyFrame("ddd");

m.setVisible(true);

}

@Override

public void mouseDragged(MouseEvent e) {

// TODO Auto-generated method stub

}

@Override

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

x = e.getX();

y = e.getY();

System.out.println(x + "," + y);

lab1.setText("X"+x+" y"+y);

}

}

你既然是移动的鼠标就要用移动的鼠标监听啊。。汗

java获得鼠标相对于窗体的坐标

有的,你可以去继承MouseEvent这个类,里面有个方法叫做,getXOnScreen()和getYOnScreen(),

getXOnScreen

public int getXOnScreen()

返回事件的绝对水平 x 坐标。在虚拟设备多屏幕环境中,桌面区域可以跨越多个物理屏幕设备,这些坐标相对于虚拟坐标系。否则,这些坐标相对于与

Component 的 GraphicsConfiguration 关联的坐标系。

返回:

x 表示绝对水平位置的整数。

getYOnScreen

public int getYOnScreen()

返回事件的绝对垂直 y 坐标。在虚拟设备多屏幕环境中,桌面区域可以跨越多个物理屏幕设备,这些坐标相对于虚拟坐标系。否则,这些坐标相对于与

Component 的 GraphicsConfiguration 关联的坐标系。

返回:

y 表示绝对垂直位置的整数。

这个就是你在窗体(Frame里的位置了),希望能帮到你。

求救Java 鼠标移动获取坐标问题.

class InputStatus{

int mouseX;

int mouseY;

}

InputStatus inputStatus=new InputStatus();

private final MouseMotionListener mouseMotionListener = new MouseMotionListener() {

public void mouseMoved(MouseEvent e) {

synchronized (inputStatus) {

inputStatus.mouseX = e.getX();

inputStatus.mouseY = e.getY();

}

}

public void mouseDragged(MouseEvent e) {

synchronized (inputStatus) {

inputStatus.mouseX = e.getX();

inputStatus.mouseY = e.getY();

}

}

};

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

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

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

int mouseX,mouseY;

然后处理函数这么写

public void mouseDragged(MouseEvent e) {

mouseX = e.getX();

mouseY = e.getY();

}

这样比较简单

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

代码如下:

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("\u9F20\u6807\u5750\u6807\u83B7\u53D6\u5668");

        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("\u5750\u6807x:");

        lblx.setFont(new Font("宋体", Font.PLAIN, 15));

        lblx.setBounds(22, 27, 66, 31);

        contentPanel.add(lblx);

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

        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);

    }

}

java获取鼠标坐标

//你运行一下,看在你的电脑上能不能用。

//不能用的话肯定是坐标不对

//把mouseMoved方法里的注释去掉重新获取

import java.awt.Dimension;

import java.awt.MouseInfo;

import java.awt.Point;

import java.awt.Robot;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseMotionListener;

import javax.swing.JButton;

import javax.swing.JFrame;

import javax.swing.WindowConstants;

public class MouseHelp extends javax.swing.JPanel implements MouseMotionListener {

private JButton textButton;

Robot robot;

/**

* Auto-generated main method to display this

* JPanel inside a new JFrame.

*/

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.getContentPane().add(new MouseHelp());

frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

frame.pack();

frame.setVisible(true);

}

public MouseHelp() {

super();

initGUI();

}

private void initGUI() {

try {

robot=new Robot();

addMouseMotionListener(this);

setPreferredSize(new Dimension(400, 300));

this.setLayout(null);

{

textButton = new JButton();

this.add(textButton);

textButton.setText("\u8fd0 \u884c");

textButton.setBounds(136, 72, 127, 22);

textButton.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e) {

robot.mouseMove(30,745);

try {

Thread.sleep(1000);

robot.mousePress(MouseEvent.BUTTON1_MASK);

robot.mouseRelease(MouseEvent.BUTTON1_MASK);

Thread.sleep(1000);

robot.mouseMove(150,481);

robot.mousePress(MouseEvent.BUTTON1_MASK);

robot.mouseRelease(MouseEvent.BUTTON1_MASK);

} catch (InterruptedException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

}

}});

}

} catch (Exception e) {

e.printStackTrace();

}

}

public void mouseDragged(MouseEvent e) {

// TODO Auto-generated method stub

}

public void mouseMoved(MouseEvent e) {

// TODO Auto-generated method stub

//从这里获取鼠标的全局坐标

//Point mousepoint = MouseInfo.getPointerInfo().getLocation();

//System.out.println(mousepoint.x+"\t"+mousepoint.y);

}

}

java获取鼠标坐标点的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于js获取鼠标点击位置坐标、java获取鼠标坐标点的信息别忘了在本站进行查找喔。