我写了一个Java程序,它将最小化一个窗口时,移动到屏幕的较低部分.它通过将鼠标按下Robot
键移动到最小化按钮并按下mousePress
和mouseRelease
键来执行任务.我确信有一种更理想的方式来编写这个程序,但我并不是在寻找这种方式.
在测试时,我注意到了一些奇怪的事情.删除打印语句时,程序没有按预期工作,也没有最小化窗口.PRINT语句应该不会对程序的功能产生任何影响,所以我认为其中可能存在某种损坏.如果没有PRINT语句,Point
变量会因为某种原因而不能实现吗?
I am wondering what is causing this failure and seeking an explanation as to what is going on behind the scenes with this issue.个
以下是代码:
import java.awt.AWTException;
import java.awt.Point;
import java.awt.Robot;
import java.awt.event.InputEvent;
import javax.swing.JFrame;
public class AutoMinimize {
public static void main(String[] args) {
JFrame frame = new JFrame("My mini frame");
frame.setSize(200,200);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
outerloop:
while(true) {
Point location = frame.getLocation();
System.out.println(location); //Comment out this print statement and it doesnt work??
if (location.getY()>700) {
try {
Thread.sleep(1000);
location = frame.getLocation();
Robot r = new Robot();
r.mouseMove(((int) location.getX()+34), ((int) location.getY()+14));
r.mousePress(InputEvent.BUTTON1_MASK);
r.mouseRelease(InputEvent.BUTTON1_MASK);
break outerloop;
} catch (AWTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
注意:鼠标移动适用于MacOS系统,但您可能需要为不同的操作系统调整坐标.