在JAVA中进行线程处理时未显示GUI

编程入门 行业动态 更新时间:2024-10-11 05:24:45
本文介绍了在JAVA中进行线程处理时未显示GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在用非常简单的线程(延迟1秒)制作简单的游戏 线程有问题,我的代码有while(true)循环:

I'm making simple game with very simple thread (1 sec delay) got problem with the thread, I have while(true) loop with the code:

try { while (true) { Ltimer.setText(getTimeElapsed()); Thread.currentThread(); Thread.sleep(1000); // Thread sleeping for 1 second } } catch (Exception e) { JOptionPane.showMessageDialog(null, "error with timer"); }

它只是每秒获取字符串并更新标签文本 当我尝试运行gui冻结时,只能看到黑色背景中的标签,所有按钮和bg img都消失了.试图解决 setVisible() repaint() 但一无所获..

it simply get string every second and updates label text when I'm trying to run it the gui freeze and I can only see the label in a black background, all buttons and bg img dissappeared. tried to fix with setVisible() repaint() but got nothing..

还有其他选择吗?

推荐答案

在EDT期间不要使用Thread#sleep(int),那么 Swing的一致性,如果您需要延迟任何操作,请使用 java.swing.Timer ,例如此处缺少EDT

don' use Thread#sleep(int) during EDT, then you have issue with Concurency in Swing, if you need to delay any action use java.swing.Timer, example for EDT lack here

更多推荐

在JAVA中进行线程处理时未显示GUI

本文发布于:2023-11-12 13:20:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581626.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:线程   JAVA   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!