java获取鼠标坐标
两个问题!1.如何知道在1024×768(17寸屏幕)上,鼠标指针的坐标。如:我要点击开始按钮,将鼠标移动到开始键上,这时,指针的坐标是???2.知道指针坐标后,如何实现...
两个问题!
1. 如何知道在1024×768(17寸屏幕)上,鼠标指针的坐标。如:我要点击开始按钮,将鼠标移动到开始键上,这时,指针的坐标是???
2.知道指针坐标后,如何实现鼠标和键盘动作的模拟?比如:我程序的目的是,让鼠标自动点击“开始按钮”的“所有程序”项。
先让鼠标移动到“开始按钮”,然后单击左键,之后移动到“所有程序”在单击左键。该怎样实现。
谢谢前辈们了! 展开
1. 如何知道在1024×768(17寸屏幕)上,鼠标指针的坐标。如:我要点击开始按钮,将鼠标移动到开始键上,这时,指针的坐标是???
2.知道指针坐标后,如何实现鼠标和键盘动作的模拟?比如:我程序的目的是,让鼠标自动点击“开始按钮”的“所有程序”项。
先让鼠标移动到“开始按钮”,然后单击左键,之后移动到“所有程序”在单击左键。该怎样实现。
谢谢前辈们了! 展开
4个回答
展开全部
//你运行一下,看在你的电脑上能不能用。
//不能用的话肯定是坐标不对
//把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);
}
}
//不能用的话肯定是坐标不对
//把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);
}
}
展开全部
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener;
class myMouseListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
String s="当前鼠标坐标:"+x+','+y;
MouseMove.lab.setText(s);
}
public void mouseDragged(MouseEvent e){};
}
public class MouseMove extends JFrame{
public static JLabel lab=new JLabel();
public MouseMove() {
}
public static void main(String [] args)
{
// MouseMove fm=new MouseMove("鼠标坐标测试");
JFrame fm=new JFrame("鼠标坐标测试");
JPanel fp=new JPanel();
fp.addMouseMotionListener(new myMouseListener());//对在面板上的鼠标移动进行监听。
Container con=fm.getContentPane();
fp.add(lab);
con.add(fp);
fm.setSize(500,400);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import javax.swing.*;
import java.awt.event.*;
import java.util.EventListener;
class myMouseListener implements MouseMotionListener
{
public void mouseMoved(MouseEvent e){
int x=e.getX();
int y=e.getY();
String s="当前鼠标坐标:"+x+','+y;
MouseMove.lab.setText(s);
}
public void mouseDragged(MouseEvent e){};
}
public class MouseMove extends JFrame{
public static JLabel lab=new JLabel();
public MouseMove() {
}
public static void main(String [] args)
{
// MouseMove fm=new MouseMove("鼠标坐标测试");
JFrame fm=new JFrame("鼠标坐标测试");
JPanel fp=new JPanel();
fp.addMouseMotionListener(new myMouseListener());//对在面板上的鼠标移动进行监听。
Container con=fm.getContentPane();
fp.add(lab);
con.add(fp);
fm.setSize(500,400);
fm.setVisible(true);
fm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
楼上只实现了兼听,但是并没有完成LZ说的功能.不晓得LZ是想自己做个程序自动启动某些东西,还是怎么样?如果不是想写程序,可以去网上下载个关于自动程序的东西,可以设置在某时间点执行执行什么程序!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
监听就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询