简体   繁体   English

按下按钮时 JFrame 程序冻结

[英]JFrame program freezes when button is pressed

When the JButton is pressed the program will just freeze and the close button on the title bar will not close it and the only way to close it is with task manager.当按下 JButton 时,程序将冻结,标题栏上的关闭按钮不会关闭它,关闭它的唯一方法是使用任务管理器。

Code for the button being pressed:按下按钮的代码:

class ClickButton implements ActionListener {

    public void actionPerformed( ActionEvent action ) {

        btn.setVisible(false);

        while(!btn.isVisible()) {
            label1.setText("Slaying Demons"); 
            wa(500);
            label1.setText("Slaying Demons.");
            wa(500);
            label1.setText("Slaying Demons.."); 
            wa(500);
            label1.setText("Slaying Demons..."); 
            wa(500);
        }
    }
}

The wa() method is just a simple thread.sleep() method that takes in the parameter for how long it should sleep. wa() 方法只是一个简单的 thread.sleep() 方法,它接受它应该休眠多长时间的参数。

Your problem is that you are using a while loop that never closes.您的问题是您使用的是永远不会关闭的 while 循环。 What your code does is it does the label.set(text) section and because it is in a loop it then does it again.您的代码所做的是它执行 label.set(text) 部分,因为它处于循环中,所以它会再次执行。 Try using a if statement instead.尝试改用 if 语句。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM