java 更新ui_Java从另一个线程更新UI组件

我找到了有关该问题的许多答案,但我仍然不明白为什么我的应用程序不会引发任何异常.

我在NetBeans 8中创建了一个新的Java表单应用程序.

我的表单是通过以下主要方法创建和显示的:

public static void main(String args[])

{

/* Set the Nimbus look and feel */

//

/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.

* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html

*/

try

{

for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())

{

if ("Nimbus".equals(info.getName()))

{

javax.swing.UIManager.setLookAndFeel(info.getClassName());

break;

}

}

}

catch (ClassNotFoundException ex)

{

java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

catch (InstantiationException ex)

{

java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

catch (IllegalAccessException ex)

{

java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

catch (javax.swing.UnsupportedLookAndFeelException ex)

{

java.util.logging.Logger.getLogger(MainForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);

}

//

/* Create and display the form */

java.awt.EventQueue.invokeLater(new Runnable()

{

public void run()

{

new MainForm().setVisible(true);

}

});

}

因此,此新的Runnable将创建新的MainForm并将其设置为可见.

然后,在我的代码中,我启动了新线程,这些线程更新了一些jButton和jTextField.代码如下:

private void updateUI() {

updateUIThread = new Thread(() ->

{

while (true) {

try {

jtfIP.setEnabled(!Start && !autoRec);

jtfPort.setEnabled(!Start && !autoRec);

jtfSlaveID.setEnabled(!Start && !autoRec);

jtfTimeout.setEnabled(!Start && !autoRec);

jtfReqInterval.setEnabled(!Start && !autoRec);

jCheckBox1.setEnabled(!Start && !autoRec);

jCBReconnect.setEnabled(!Start && !autoRec);

if (db != null) {

if (!db.getIsOpen()) {

jPBD.setBackground(Color.RED);

jPBD.setForeground(Color.WHITE);

jPBD.setText("ER");

} else {

jPBD.setBackground(Color.GREEN);

jPBD.setForeground(Color.BLACK);

jPBD.setText("OK ");

}

} else {

jPBD.setBackground(Color.RED);

jPBD.setForeground(Color.WHITE);

jPBD.setText(" ER ");

}

if (autoRec){

jbtnConnect.setText("Auto");

if (Start && Connected) {

jbtnConnect.setForeground(Color.BLACK);

jbtnConnect.setBackground(Color.GREEN);

} else {

jbtnConnect.setForeground(Color.WHITE);

jbtnConnect.setBackground(Color.RED);

}

} else {

if (Start) {

jbtnConnect.setText("Disconnect");

jbtnConnect.setForeground(Color.BLACK);

jbtnConnect.setBackground(Color.GREEN);

} else {

jbtnConnect.setText("Connect");

jbtnConnect.setForeground(Color.WHITE);

jbtnConnect.setBackground(Color.RED);

}

}

jtfErroriCitire.setText(String.valueOf(totalErrors));

try

{

Thread.sleep(300);

jPanel4.repaint(1);

}

catch (InterruptedException ex)

{

Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);

}

}

catch (Exception ex) {

Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, null, ex);

}

}

});

updateUIThread.start();

}

并且还有其他像以上这样启动的线程,在这里我得到了不同的值,这些值在上述线程中进行了更新.

我的问题是为什么我的代码不会对从另一个线程更新的UI元素引发任何异常?我没有使用SwingUtilities.invokeLater(new Runnable(){//代码在这里});

而且我的代码执行得很好…

谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值