62,628
社区成员
发帖
与我相关
我的任务
分享
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