正确更新 Swing 组件 [英] Updating swing components correctly

查看:38
本文介绍了正确更新 Swing 组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在设置摆动组件的值,就像我设置任何其他变量一样,但是我遇到了这个页面 - https://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/ - 似乎我正在使用事件调度线程对摆动组件进行所有更改 -

I have been setting values on swing components just like I would any other variable, however I came across this page - https://bitguru.wordpress.com/2007/03/21/will-the-real-swing-single-threading-rule-please-stand-up/ - and it seems that I be making all changes to swing components using an event dispatching thread -

所以,这是正确的,我应该更改我更新摆动组件的所有代码

So, is this correct, should I change all the code where I updated swing components from this

    String name = this.getNameTextfield().getText();
    String password = new String(this.getPasswordField().getPassword());
    String confirmPassword = new String(this.getConfirmPasswordField().getPassword());

这个?

java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               String name = this.getNameTextfield().getText();
               String password = new String(this.getPasswordField().getPassword());
               String confirmPassword = new String(this.getConfirmPasswordField().getPassword());
            }
        });

这是标准做法吗?

糟糕,只是复制并粘贴了我的一些组件相关代码,忽略了它没有更新组件的事实.

Oops, just copied and pasted some of my component related code, overlooked the fact that it wasnt updating componenets.

推荐答案

在这种特定情况下,您不修改任何组件,因此您不需要在 EDT 上进行,但是当您修改 UI 时,第二种方法是正确的.

In this specific case you do not modify any component, so you don't need to do it on the EDT, but when you do modify the UI, the second method is correct.

这篇关于正确更新 Swing 组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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