Swing - 更新标签 [英] Swing - Update Label

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

问题描述

我有一个消息标签和一个提交按钮.提交按钮将被多次按下,每次按下的操作最多可能需要一分钟.

I have a message label and a submit button. The submit button will be pressed multiple times, and the action for the each press can take up to a minute.

当按钮按下时,我想将消息设置为空,任务完成后,我想将消息设置为完成".

When the button is pressed, I want to set the message to empty, and after the task is complete, I want to set the message to "Complete".

private void submitActionPerformed(java.awt.event.ActionEvent evt) {
   message = "";
   updateMessageLabel();

   doTheTask();

   /* this update is apply to the label after completion */
   message = "Complete";
}

是否可以在运行 submitActionPerformed() 方法之前(或在方法中)但在单击按钮之后更新该消息标签?

Is it possible to update that message label before the submitActionPerformed() method is run (or in the method), but after the the button is clicked?

推荐答案

是的,您可以使用 SwingWorker 线程执行此操作,执行所有预 submitActionPerformed() 活动,例如更新标签,在currentThreadexecute()方法中,调用doTheTask()作为后台作业,使用workerThread.

Yes you can do this using SwingWorker thread, do all the pre submitActionPerformed() activities like updating the label, in the execute() method of the currentThread and call doTheTask() as a background job using worker Thread.

我建议您阅读此文档以获取有关 SwingWorker 线程

I suggest you to go through this documentation for reference about SwingWorker Thread

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

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