java中获取屏幕坐标的问题
主函数里面可以正常显示屏幕的坐标,但是在label里面就只能显示程序内的坐标,这是为什么?importjava.awt.MouseInfo;importjava.awt....
主函数里面可以正常显示屏幕的坐标,但是在label里面就只能显示程序内的坐标,这是为什么?
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class TestJFrame extends JFrame implements MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = 1L;//序列化保持版本兼容
private JButton b;
private JLabel l;
public TestJFrame(){
super();
setVisible(true);
setSize(300, 300);
JPanel panel=new JPanel();
panel.setVisible(true);
b=new JButton("开始");
b.addMouseListener(new BMouseListener());
panel.add(b);
l=new JLabel("1231524563576");
panel.add(l);
add(panel);
this.addMouseMotionListener(this);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new TestJFrame();
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
System.out.println(mousepoint.x+" "+mousepoint.y);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
l.setText("X坐标为:"+mousepoint.getX()+",Y坐标为:"+mousepoint.getY());
}
} 展开
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.*;
public class TestJFrame extends JFrame implements MouseMotionListener{
/**
*
*/
private static final long serialVersionUID = 1L;//序列化保持版本兼容
private JButton b;
private JLabel l;
public TestJFrame(){
super();
setVisible(true);
setSize(300, 300);
JPanel panel=new JPanel();
panel.setVisible(true);
b=new JButton("开始");
b.addMouseListener(new BMouseListener());
panel.add(b);
l=new JLabel("1231524563576");
panel.add(l);
add(panel);
this.addMouseMotionListener(this);
this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
}
public static void main(String[] args){
new TestJFrame();
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
System.out.println(mousepoint.x+" "+mousepoint.y);
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
Point mousepoint = MouseInfo.getPointerInfo().getLocation();
l.setText("X坐标为:"+mousepoint.getX()+",Y坐标为:"+mousepoint.getY());
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询