当我尝试 运行 时,NetBeans GUI 应用程序一直冻结

NetBeans GUI Application keeps freezing when I try to run it

我正在使用 NetBeans 6.9.1 编程,目前正在使用 Swing GUI。每次我 运行 我的程序并单击 "calculate" 按钮确定结果时,程序都会冻结。这是导致它冻结的代码块:

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String backTrouble;
    String heartTrouble;
    int riderHeight = Integer.parseInt(inputHeight.getText());

    backTrouble = inputBack.getText();
    heartTrouble = inputHeart.getText();

    while ((riderHeight >= 122) && (riderHeight <= 188)){

        if ((backTrouble.equals("N")) && (heartTrouble.equals("N"))){

            responseField.setText("It is OK for you to ride this roller coaster. Have fun!");
        }

        else if ((backTrouble.equals("Y")) && (heartTrouble.equals("N"))){

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

        else if ((backTrouble.equals("N")) && (heartTrouble.equals("Y"))){

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else{

            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

    while ((riderHeight < 122) || (riderHeight > 188)){

        responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
    }
    }


}

我只是不太明白为什么它总是死机,希望能提供一些帮助,谢谢。

您的 while (such and such) 循环持续 运行 并阻塞 Swing 事件线程,阻止线程绘制 GUI 和与用户交互,有效地冻结您的应用程序。虽然这些循环在控制台程序中有意义,但在事件驱动的 Swing GUI 中却没有意义。摆脱它们,冻结将解除冻结。也许您想在这里使用 if 块——如果没有更多信息很难判断。

再说一次,也许块会更好地工作:

private void calculateButtonActionPerformed(java.awt.event.ActionEvent evt) {
    String backTrouble;
    String heartTrouble;
    int riderHeight = Integer.parseInt(inputHeight.getText());
    backTrouble = inputBack.getText();
    heartTrouble = inputHeart.getText();

    // get rid of the while loop, 
    // while ((riderHeight >= 122) && (riderHeight <= 188)){
    if ((riderHeight >= 122) && (riderHeight <= 188)){
        if ((backTrouble.equals("N")) && (heartTrouble.equals("N"))){
            responseField.setText("It is OK for you to ride this roller coaster. Have fun!");
        }
        else if ((backTrouble.equals("Y")) && (heartTrouble.equals("N"))){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else if ((backTrouble.equals("N")) && (heartTrouble.equals("Y"))){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
        else{
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }

        // again, no while loops here
        // while ((riderHeight < 122) || (riderHeight > 188)){
        if ((riderHeight < 122) || (riderHeight > 188)){
            responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
        }
    }
}

但我不是 100% 确定。

我认为问题是你的右括号放错了地方。

else{

    responseField.setText("Sorry, it is not safe for you to ride this roller coaster.");
}

while ((riderHeight < 122) || (riderHeight > 188)){

在此之前 while 你应该有一个右括号。

此外,您永远不会修改 riderHeight,因此它将永远停留在循环中。

您可能希望这两个 while 循环成为 if..then 语句,因为 while 循环似乎没有任何目的。

如果你有

while(stillOnBike) {
}

那么你可能会有最终退出的逻辑,因为这个人最终会下车,但你基本上有 while(true)