For 循环内的停止按钮

发布于 2024-12-15 04:27:35 字数 920 浏览 2 评论 0原文

我一直在寻找停止按钮,但找不到任何适用于我的代码的内容。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岁月静好 2024-12-22 04:27:35

让按钮的点击处理程序设置一个私有字段以指示该操作已“停止”,然后在该字段位于安全位置以取消时让您的 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();
    }
}

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:

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文