Answer the question
In order to leave comments, you need to log in
How to get the center of the screen and put the mouse cursor there in java?
Hello! I study java, I train with swing, awt. A simple program, by pressing a button, the cursor should move to the center of the screen. I calculate the center of the screen like this
Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize ();
int h = sSize.height;
int w = sSize.width;
Robot robot = new Robot();
robot.mouseMove(x, y);
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent event) {
Dimension sSize = Toolkit.getDefaultToolkit ().getScreenSize ();
int h = sSize.height/2;
int w = sSize.width/2;
System.out.println("H = " + h + " W = " + w);
mouseMoveGo(h, w);
frame.remove(button);
frame.add(label);
frame.validate();
frame.repaint();
}
}
public void mouseMoveGo(int x, int y) {
try {
Robot robot = new Robot();
robot.mouseMove(x, y);
System.out.println("X = " + x + " Y = " + y);
} catch (Exception ex) {ex.printStackTrace();}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question