隐藏按钮,点击按钮后等待 2 秒再显示
我正在制作这个学校项目,我需要让我的代码隐藏一个按钮,然后睡眠,然后继续,但它永远不会隐藏按钮。
我有一个类,它是通过按下按钮启动的,然后它启动另一个应该隐藏按钮的方法,然后它启动另一个类中的一个方法,该方法可以完美地完成一堆事情,然后它启动第三个方法在第三节课中,它应该做的第一件事是睡眠,但它在隐藏我的按钮之前睡眠,并且完全跳过隐藏我的按钮。
这是代码部分(不要介意丹麦值/方法名称):
public void turncard(final int navn,boolean spiller){
knap.setVisible(false);
EqualCheck.storevalue(this,spiller);
}
我知道这部分被 if.. 包围,但它确实被这样调用。
if(spiller){ //tester om det er spilleren der har vendt kort,
kort.repaint(); //og hvis det er, så starter den computerens
Main.spillet.computertur(); //tur.
}
然后在“computertur”方法开始时,
public void computertur() {
for(int i = 0; i < kortene.size(); i++) {
kortene.get(i).knap.setEnabled(false);
}
try {
Thread.sleep(2000);
} catch ( Exception e) {
System.out.println("sleep failed.");
}
我确实读到了关于 invokeAndWait
命令,这对于 EDT:s 来说应该更好,但是我如何使用它来暂停 mythread
...或者甚至是一个更好的问题,为什么它不隐藏按钮? :) 我的意思是,在我看来,在执行其他操作之前调用 setVisible(false)
会在执行其他操作之前隐藏它。为什么不呢?
I have this school project I'm making, where I need to make my code hide a button, and then sleep, and then continue, but it never hides the button.
I have one class which is started by a button press, it then starts another method which should hide the button, and then it starts a method in another class which does a bunch of things which it does perfectly, and then it starts a 3rd method in a 3rd class, where the first thing it should do is sleep, but it sleeps before hiding my button, and totally skips hiding my button.
Here is the codes part (don't mind the danish value/method names):
public void turncard(final int navn,boolean spiller){
knap.setVisible(false);
EqualCheck.storevalue(this,spiller);
}
i know this part is surrounded by a if.. but it does get called like this.
if(spiller){ //tester om det er spilleren der har vendt kort,
kort.repaint(); //og hvis det er, så starter den computerens
Main.spillet.computertur(); //tur.
}
and then at the start of the "computertur" method
public void computertur() {
for(int i = 0; i < kortene.size(); i++) {
kortene.get(i).knap.setEnabled(false);
}
try {
Thread.sleep(2000);
} catch ( Exception e) {
System.out.println("sleep failed.");
}
I did read about the invokeAndWait
command which should be better for EDT:s, but how can I use it to pause mythread
... or even, a better question, why doesn't it hide the button? :) I mean, in my head calling the setVisible(false)
before anything else will hide it before it does anything else.. Why doesn't it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在事件调度线程 (EDT) 上执行代码时,切勿调用 Thread.sleep(...)。这将冻结 GUI 并防止其重新绘制自身。
一般来说,您的代码需要在单独的线程上执行,但将按钮设置为不可见的代码除外。请阅读 Swing 教程中有关并发的部分,了解更多信息解决方案。
You should never invoke Thread.sleep(...) while executing code on the Event Dispatch Thread (EDT). This will freeze the GUI and prevent it from repainting iteself.
In general your code needs to execute on a separate Thread, except for the code that sets the button invisble. Read the section from the Swing tutorial on Concurrency for more information and solutions.