Sleep方法锁定了我的GUI [英] Sleep method locks my GUI

查看:104
本文介绍了Sleep方法锁定了我的GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的是在我的程序类中使代码的某些部分等待一段时间,但我的问题是我尝试它的方式使我的GUI卡住了。我的意思是,按下一个按钮让我的程序等待一段时间,而我的程序正在等待我按下一个按钮,按下按钮但是之后如果我尝试按下任何其他按钮而程序仍在等待我不能,我按下的第一个按钮似乎被按下而另一个按钮无法按下,虽然程序执行我想做的事情(接受所有命令)唯一的问题是我的GUI被锁定。

What I want to do is to make some part of the code in a class of my programm to wait for sometime, but my problem is that the way I am trying to it makes my GUI to stuck. I mean, pressing a button i make my program to wait for sometime, while my program is waiting i press a button, the button is pressed but after that if i try to press any other button while the program is still waiting i can't, the first button i pressed seems to be pressed and the other button can't be pressed although the program does what I want to do(accepts all the commands) the only problem is that my GUI is locked.

推荐答案

您需要使用多个线程。需要完成的任何工作可能需要花费任何重要/显着的时间,需要在自己的线程中完成。 (这肯定包括你打电话给 sleep 的任何代码。)

You need to use multiple threads. Any "work" that needs to be done that may take any significant / noticeable amount of time needs to be done in its own thread. (This certainly includes any code where you are calling sleep.)

这里有3个很好的参考资料:

Here are 3 good references:

  • http://docs.oracle.com/javase/tutorial/uiswing/concurrency/
  • http://java.sun.com/products/jfc/tsc/articles/threads/threads1.html
  • http://java.sun.com/developer/technicalArticles/Threads/swing/

这篇关于Sleep方法锁定了我的GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆