简体   繁体   English

当我尝试运行NetBeans GUI应用程序时,它保持冻结状态

[英]NetBeans GUI Application keeps freezing when I try to run it

I am using NetBeans 6.9.1 programming, and I am currently working with the Swing GUI. 我正在使用NetBeans 6.9.1编程,并且当前正在使用Swing GUI。 Every time I run my program and click on the "calculate" button to determine the results, the program freezes. 每次我运行程序并单击“计算”按钮以确定结果时,程序都会冻结。 Here is the chunk of my code that causes it to freeze: 这是导致其冻结的代码块:

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


}

I just don't really understand why it keeps freezing, and some help would be appreciated, thank you. 我只是不太明白为什么它会保持冻结状态,感谢您的帮助。

Your while (such and such) loops are running continuously and blocking the Swing event thread, preventing the thread from painting the GUI and interacting with the user, effectively freezing your application. 您的while (such and such)循环正在连续运行并阻塞了Swing事件线程,从而阻止了该线程绘制GUI并与用户进行交互,从而有效地冻结了您的应用程序。 While these loops make sense in a console program, they don't in an event-driven Swing GUI. 尽管这些循环在控制台程序中有意义,但在事件驱动的Swing GUI中却没有。 Get rid of them, and the freezing will un-freeze. 摆脱它们,冻结将解冻。 Perhaps you want to use if blocks instead here -- hard to tell without more information. 也许您想在这里使用if块- if没有更多信息就很难分辨。

So again perhaps if blocks would work better: 因此,也许再次块可以更好地工作:

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

But I'm not 100% sure. 但是我不确定100%。

I think the problem is you have a close parenthesis in the wrong place. 我认为问题在于您在错误的位置加上了括号。

else{

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

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

Before this while you should have a close parenthesis. 在此之前,您应该有一个紧密的括号。

Also you never modify the riderHeight, so it will just stay in the loops forever. 另外,您永远不会修改riderHeight,因此它将永远保持循环。

You may want these two while loops to be if..then statements instead, as there doesn't seem to be any purpose in a while loop. 您可能希望这两个while循环改为if..then语句,因为while循环似乎没有任何用途。

If you had 如果你有

while(stillOnBike) {
}

Then you could have logic that would eventually exit as the person will eventually get off the bike, but you basically have while(true) . 然后,您可能会有最终会退出的逻辑,因为此人最终会从自行车上下来,但是您基本上有了while(true)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试在netbeans上运行GUI时,它说没有可用的类,我该怎么办? - When I try to run my GUI on netbeans it says that there are no available classes what can I do about that? 我正在制作一个简单的计算器,并且GUI版本一直保持冻结 - I'm making a simple calculator and the GUI build keeps freezing 当我尝试在 .netbeans 中运行 maven 项目时出现错误 - I have an error when I try to run a maven project in netbeans 我对运行文件进行测试时,NetBeans GUI设计的行为不一致 - NetBeans GUI design acting inconsistent when I test with the run file 当我在 NetBeans 中点击运行时,我的 GUI 不会显示 - My GUI will not display when I hit run in NetBeans 当我尝试运行它时,我的应用程序不断崩溃(新的) - My app keeps crashing when i try to run it (New to this) 当我尝试加载数据库时,应用程序不断崩溃 - Application keeps crashing when I try to load the database 使用 Netbeans 12.2。 使用 GUI 设计器我创建了一个按钮。 显示我创建的按钮,但是当我运行应用程序时,按钮消失了 - Using Netbeans 12.2. Using the GUI designer I create a button. Shows the button that I create but when I run the application the button is gone 如何在不冻结 GUI 的情况下运行它 - How can I get this to run without freezing the GUI 启动线程时Java游戏保持冻结? - Java Game Keeps Freezing When I Start A Thread?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM