如何立即更改 MouseEvent 处理程序中 jLabel 的文本?
我在刷新鼠标操作事件处理程序内的摆动组件时遇到问题。问题是,在此函数中更改的所有 jLabels 在 jButton1MouseClicked() 完成后,它们的更改都是可见的。这是我的功能:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
int cycles = Integer.parseInt(c.settings.get("cycles"));
statusMessageLabel.setText("Data collection in progress...");
for(int i=1;i <= Integer.parseInt(c.settings.get("cycles"));i++) {
jLabelCycle.setText(Integer.toString(i));
//here are some functions which are implementing data collection
if(i < cycles){
int counter = Integer.parseInt(c.settings.get("interval"));
while(counter >= 0){
jLabelTimer.setText(Integer.toString(counter));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(AppCView.class.getName()).log(Level.SEVERE, null, ex);
}
counter--;
}
}
}
statusMessageLabel.setText("Data collection has been finished.");
}
你能帮我吗?我确实需要立即看到此更改,因为一个 jLabel 可视化计数直到下一个周期,第二个 jLabel 可视化实际周期数。在更改它们后,我尝试在所有 jLabel 上调用函数 repaint() 但没有帮助。
谢谢大家的帮助。
I have problem with refreshing swing components inside Mouse action event handler. Problem is that all jLabels which are changed in this function that their changes are visible after jButton1MouseClicked() is done. Here is my function:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
int cycles = Integer.parseInt(c.settings.get("cycles"));
statusMessageLabel.setText("Data collection in progress...");
for(int i=1;i <= Integer.parseInt(c.settings.get("cycles"));i++) {
jLabelCycle.setText(Integer.toString(i));
//here are some functions which are implementing data collection
if(i < cycles){
int counter = Integer.parseInt(c.settings.get("interval"));
while(counter >= 0){
jLabelTimer.setText(Integer.toString(counter));
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Logger.getLogger(AppCView.class.getName()).log(Level.SEVERE, null, ex);
}
counter--;
}
}
}
statusMessageLabel.setText("Data collection has been finished.");
}
Can you please help me ? I really need this changes to be visible immidiaetly because one jLabel visualize counting till next cycle and second one is visualizating actual cycle number. I tried call function repaint() on all jLabels after i changed them but it didnt help.
Thank you all for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 AWT 中,与大多数其他 GUI 系统非常相似,包括重绘在内的所有事件都在单个线程(AWT 事件调度线程 (EDT))上完成。您正在事件侦听器中阻塞该线程,因此标签上的重绘事件无法通过。
我建议重写您的代码以使用
javax.swing.Timer
而不是Thread.sleep
。一般来说,长时间运行的操作应该在单独的线程上处理。任何涉及 AWT 或 Swing 组件的操作都应使用 java.awt.EventQueue.invokeLater 进行排队,以便在 AWT EDT 上运行。
In AWT, much like most other GUI systems, all events including repaints are done on a single thread, the AWT Event Dispatch Thread (EDT). You are blocking that thread in the event listener, so the repaint events on the label cannot get through.
I suggest rewriting your code to use use
javax.swing.Timer
instead ofThread.sleep
.In general, long running operations should be handled on a separate thread. Any operations touching AWT or Swing components should be queued for running on the AWT EDT using
java.awt.EventQueue.invokeLater
.