如何在线程中更新JFrame标签? -Java [英] How to update JFrame Label within a Thread? - Java

查看:74
本文介绍了如何在线程中更新JFrame标签? -Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试了很多,但似乎无法使其正常工作.

I have tried a lot, but can't seem to get it to work.

在下面的示例中,我被告知要使用EDT.

I was told to use EDT with the following example.

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            //  Modify the GUI here
        }
});

我已经阅读了很多有关此主题的内容,但仍然不理解.我知道线程是什么,但是.invokeLater对我来说仍然毫无意义.老实说,如果您可以 详细解释 ,这将是一个很大的帮助!

I have read on this topic a lot and still don't understand. I get what a thread is, but the .invokeLater still makes no sense to me. Honestly if you can explain in detail this it would be a big help!

程序目标::获取每秒不断创建的随机生成的密钥,以便以后在GUI中对其进行更新.

Goal of Program: To get the randomly generated key that is constantly created every second to update itself afterward in the GUI.

推荐答案

因此有一个EDT(事件调度线程).屏幕上显示的所有操作均由EDT执行.每个Swing应用程序只有一个EDT.

So there is an EDT (Event Dispatch Thread). All actions that appear on your screen are executed by the EDT. There is only one EDT per Swing application.

您处于任意线程中,并且想要通过该线程更新GUI?就像我说的那样,每个swing应用程序只有一个EDT,因此您必须告诉EDT显示标签(或所需的任何上下文).

You are in some arbitrary thread and you want to update the GUI through that thread? Well like I said there is only one EDT for each swing application, so you have to tell that EDT to display the label (or whatever context you want).

这里的想法是,将此Runnable推送到EDT所提取的队列中.最终,在所有其他操作完成之前,EDT将处理您的可运行对象.

The idea here, is you push this Runnable onto a queue that the EDT pulls from. Eventually, your runnable will be processed by the EDT when all other actions before it are completed.

这篇关于如何在线程中更新JFrame标签? -Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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