java怎样获取鼠标在屏幕的坐标 5
6个回答
展开全部
代码如下:
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);
}
}
展开全部
监听器
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
addMouseListener(new MouseMonitor());
}
public void paint(Graphics g) //显示鼠标坐标
{
g.drawString(msg,sh,ss);
}
class MouseMonitor implements MouseListener {
public void mouseEntered(MouseEvent me) //鼠标进入事件
{
mouseX=getX();
mouseY=getY();
setX=mouseX+"";
setY=mouseY+"";
msg=msg+setX+" "+setY;
repaint();
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mouseClicked(MouseEvent me){}
}
public static void main(String[] args)
{
MyPicture org=new MyPicture();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class MyPicture extends JFrame
{
JFrame frame;
JButton a1,a2,a3;
JPanel panel;
int mouseX,mouseY,sh=10,ss=290;
String msg="坐标:";
String setX,setY;
public MyPicture() //程序界面
{
frame=new JFrame("图形学实验");
a1=new JButton("直线");
a2=new JButton("弧");
a3=new JButton("裁剪");
panel=new JPanel();
panel.add(a1);
panel.add(a2);
panel.add(a3);
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setVisible(true);
addMouseListener(new MouseMonitor());
}
public void paint(Graphics g) //显示鼠标坐标
{
g.drawString(msg,sh,ss);
}
class MouseMonitor implements MouseListener {
public void mouseEntered(MouseEvent me) //鼠标进入事件
{
mouseX=getX();
mouseY=getY();
setX=mouseX+"";
setY=mouseY+"";
msg=msg+setX+" "+setY;
repaint();
}
public void mousePressed(MouseEvent me){}
public void mouseReleased(MouseEvent me) {}
public void mouseExited(MouseEvent me) {}
public void mouseClicked(MouseEvent me){}
}
public static void main(String[] args)
{
MyPicture org=new MyPicture();
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void mousePosition(MouseEvent me){
mouseX=me.getX();
mouseY=me.getY();
}
//或者试试下面这个:
public void mousePosition(MouseEvent me){
mouseX=me.getXOnScreen();
mouseY=me.getYOnScreen();
}
//当然,mouseX、mouseY是要提前定义好的,用来存储鼠标当前的XY坐标,然后调用mouseX和mouseY,你想干嘛就干嘛了
mouseX=me.getX();
mouseY=me.getY();
}
//或者试试下面这个:
public void mousePosition(MouseEvent me){
mouseX=me.getXOnScreen();
mouseY=me.getYOnScreen();
}
//当然,mouseX、mouseY是要提前定义好的,用来存储鼠标当前的XY坐标,然后调用mouseX和mouseY,你想干嘛就干嘛了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
先问个问题,鼠标在窗体上,还是已经脱离窗体
追问
脱离窗体的 也就是说是在屏幕上的坐标
追答
明白你的意思。
这个要想获取鼠标信息,需要调用windows api实现。这个可以通过JNA或者JNI实现调用
需要调用的window api为
BOOL GetCursorPos(struct lpPoint);
lpPoint结构为
struct {
long x;
long y;
} POINT,*PPOINT;
这样就可以获取鼠标的位置了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询