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

时间:2016-03-20 21:12:44

标签: java swing netbeans netbeans-6.9

我正在使用NetBeans 6.9.1编程,我目前正在使用Swing GUI。每次我运行程序并单击“计算”按钮来确定结果时,程序会冻结。以下是导致其冻结的代码块:

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.");
    }
    }


}

我只是不明白为什么它会一直冻结,一些帮助会受到赞赏,谢谢。

2 个答案:

答案 0 :(得分:2)

您的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.");
        }
    }
}

但我不是百分百肯定。

答案 1 :(得分:2)

我认为问题是你在错误的地方有一个紧密的括号。

else{

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

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

在此之前你应该有一个紧密的括号。

你也永远不会修改riderHeight,所以它会永远留在循环中。

你可能希望这两个while循环是if..then语句,因为while循环似乎没有任何用途。

如果你有

while(stillOnBike) {
}

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