【发布时间】:2014-06-14 15:42:57
【问题描述】:
我创建了以下代码来执行以下操作:
- 新建一个 Word 文件并打开
- 检查任务管理器是否打开,然后继续
- 使用 Robot 类移动到屏幕中心,然后单击 Word 应用程序并键入:Y。
除了输入 Y 之外,它什么都做。
代码:
// Open Word Document
public boolean openWordDoc(String tempProName, String URL, String j) throws IOException, AWTException {
boolean result = false;
String userHomeFolder = System.getProperty("user.home");
File textFile = new File((userHomeFolder + "/Desktop"), j + " " + URL);
BufferedWriter out = new BufferedWriter(new FileWriter(textFile));
out.close();
if (Desktop.isDesktopSupported()) {
Desktop fD = Desktop.getDesktop();
fD.open(textFile);
}
// See if it really did open and wait until it opened to continue
String line = "";
Process p = Runtime.getRuntime().exec("tasklist.exe");
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((!line.equals("WINWORD.EXE"))) {
while ((line = input.readLine()) != null) {
String[] templine = line.split(" ");
if (templine[0].equals("WINWORD.EXE")) {
line = "WINWORD.EXE";
result = true;
break;
}
}
}
// Robot
Robot robot = new Robot();
double x = 0;
double y = 0;
x = Toolkit.getDefaultToolkit().getScreenSize().getWidth() / 2;
y = Toolkit.getDefaultToolkit().getScreenSize().getHeight() / 2;
int xx = (int) Math.round(x);
int yy = (int) Math.round(y);
robot.mouseMove(xx, yy);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.keyPress(KeyEvent.VK_Y);
return result;
}
【问题讨论】:
标签: java ui-automation awtrobot