For 循环内的停止按钮
我一直在寻找停止按钮,但找不到任何适用于我的代码的内容。
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 。任何帮助都非常感谢。
谢谢。
I have been looking around for a stop button but I can't find anything that applies to my code.
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();
}
}
}
I was thinking of making the jbutton initiate a break; but I can't seem to use jButtonActionPerformed inside a for loop. Any help is greatly apriciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让按钮的点击处理程序设置一个私有字段以指示该操作已“停止”,然后在该字段位于安全位置以取消时让您的
for
循环检查该字段:Make the button's click handler set a private field to indicate that the action has been "stopped", then make your
for
loop check for that field when it is at a safe place to cancel: