简体   繁体   English

将标签设置为在操作侦听器中可见

[英]Setting label to visible in an action listener

I have a JFrame with a button on it. 我有一个带有按钮的JFrame。 When the user presses the button, a time consuming series of actions are performed. 当用户按下按钮时,将执行一系列耗时的操作。 I have a JLabel on the form that says "Please wait" that I want to become visible while the actions are being performed and then go invisible when they are completed. 我在表单上有一个JLabel,上面写着“请稍等”,我希望在执行操作时可以看到它们,而在完成操作后可以看到它们。 So, I put label.setVisible(true) at the beginning of the action listener and label.setVisible(false) at the end, but nothing happens. 因此,我将label.setVisible(true)放在动作侦听器的开头,并将label.setVisible(false)放在结尾,但是什么也没发生。

It seems like the displaying of the label was being queued until the actions were finished, so it just goes visible and then immediately invisible. 似乎标签的显示正在排队,直到操作完成为止,因此它只是可见,然后立即不可见。 How can I make sure the label becomes visible before continuing with the rest of the code in the action listener? 在继续执行动作侦听器中的其余代码之前,如何确保标签可见?

Use a SwingWorker . 使用SwingWorker When the button is clicked call execute . 单击按钮后,调用执行 Update the JLabel when done . 更新JLabel完成

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM