我一直在寻找停止按钮,但我找不到任何适用于我的代码的内容。
if (O1.isSelected()) {
for (int nu = Num; nu > 0; nu--) {
try {
Robot robot = new Robot();
robot.delay(Num2 * 1000);
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}
}
我正在考虑让jbutton开始休息;但我似乎无法在for循环中使用jButtonActionPerformed。任何帮助都很受欢迎。
感谢。
答案 0 :(得分:0)
使按钮的点击处理程序设置一个私有字段以指示该操作已“停止”,然后在该字段位于安全的取消位置时对该字段进行for
循环检查:
for (int nu = Num; nu > 0; nu--) {
try {
Robot robot = new Robot();
robot.delay(Num2 * 1000);
if (this.stopRequested) {
this.stopRequested = !this.stopRequested;
break;
}
robot.keyPress(KeyEvent.VK_H);
robot.keyPress(KeyEvent.VK_E);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_W);
robot.keyPress(KeyEvent.VK_O);
robot.keyPress(KeyEvent.VK_R);
robot.keyPress(KeyEvent.VK_L);
robot.keyPress(KeyEvent.VK_D);
robot.keyPress(KeyEvent.VK_ENTER);
} catch (AWTException e) {
e.printStackTrace();
}
}