哪位高手能帮我把这个程序改一下?谢谢

quaitong 2008-03-17 03:27:11
下面一段代码是用来回去鼠标所在位置的像素值的,但是其中的
int x = (int) MouseInfo.getPointerInfo().getLocation().getX();
int y = (int) MouseInfo.getPointerInfo().getLocation().getY();
这两行代码是只能在jdk 1.5以后的版本中运行,我想找一个1.4.×中的api代替一下,但是找了半天也找不到,请大牛指教:)



import java.awt.*;
import javax.swing.*;
import javax.swing.BorderFactory;

public class PixelGetter extends JFrame {
JPanel contentPane;

Robot robot;

JTextField textX = new JTextField();

JTextField textY = new JTextField();

JLabel labelX = new JLabel();

JLabel labelY = new JLabel();

public static void main(String[] args) {

PixelGetter f = new PixelGetter();
f.go();

}//end of main


public PixelGetter() {

this.setTitle("-_-");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setResizable(false);
this.setSize(138,88);
this.setLocation(0, 0);

contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);

try {
robot = new Robot();
} catch (Exception ex) {
System.out.println(ex);
}

textX.setBorder(BorderFactory.createLoweredBevelBorder());
textX.setBounds(38, 8, 58, 18);
textY.setBorder(BorderFactory.createLoweredBevelBorder());
textY.setBounds(38, 36, 58, 18);

labelX.setText("x : ");
labelX.setBounds(15, 1, 20, 30);
labelY.setText("y : ");
labelY.setBounds(15, 29, 17, 31);

contentPane.add(labelX);
contentPane.add(labelY);
contentPane.add(textX);
contentPane.add(textY);

this.setVisible(true);
//this.setAlwaysOnTop(true); Use this if the version is 1.5 or later
this.toFront();

}//end of constructor

public void go(){

try {
while (true) {
int x = (int) MouseInfo.getPointerInfo().getLocation()
.getX();
int y = (int) MouseInfo.getPointerInfo().getLocation()
.getY();

textX.setText(" "+x);
textY.setText(" "+y);

Thread.sleep(20);//in case of wasting the CPU
}//end of while
} catch (Exception e) {
e.printStackTrace();
}//end of try-catch
}//end of go
}//end of class
...全文
110 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
quaitong 2008-03-27
  • 打赏
  • 举报
回复
这样改了之后显示的不是相对屏幕的像素信息,而是相对组件的了。。。。
Leejun527 2008-03-17
  • 打赏
  • 举报
回复
可以试试这样做:
添加一个鼠标移动事件监听函数void OnMouseMove(MouseEvent e)
{
int x= e.getX();
int y= e.getY();
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧