仅 jlabel 的鼠标单击事件正确更新 jlabel 文本属性
我正在使用 java 线程来更新应用程序中的 jlabel 文本值 但在更新 jlabel 上的文本之后 jlabel 通过刷新自身定期显示所有过去的更新值和新值,
但是当我在 jlabel 的 mouseclick 事件中使用相同的更新函数时,它会按我的预期更新自身,并仅显示最后一个值,
这会导致此问题 我是否缺少 mouseclick 事件内部调用的一些方法?
nore:应用程序是 japplet
i am using a java thread to update jlabel text value in my application
but after it updated text on jlabel
jlabel showing all past updated values and new value periodically in order via refreshing itself
but when i use same update function within the mouseclick event of jlabel it is updating itself as i expected and showing last value only
what can cause this problem
am i missing some methods which mouseclick event internally calls ?
nore:application is japplet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不确定到底发生了什么,但我首先要确保您的更新发生在事件分派线程中。让更新程序线程调用:
请参阅事件调度线程 教程。
Not sure exactly what's going on but I would start by making sure your update happens in the event dispatch thread. Have your updater thread call:
See the Event Dispatch Thread tutorial.
您必须确保对 Swing UI 组件的任何更改都在事件调度线程上执行。以下是实现此目的的两个建议:
Timer
您可能需要查看 javax.swing.Timer(如果目的是定期刷新
JLabel
文本)。计时器在事件调度线程上定期触发ActionEvent
:SwingWorker
或者,您可能需要考虑使用 SwingWorker 执行后台处理。后台工作线程可以通过调用
publish
将信息传递回事件调度线程。You must ensure that any changes to Swing UI components are performed on the Event Dispatch Thread. Here are two suggestions for accomplishing this:
Timer
You may want to check out javax.swing.Timer if the aim is to periodically refresh the
JLabel
text. The timer firesActionEvent
s periodically on the Event Dispatch Thread:SwingWorker
Alternatively you may want to consider using a SwingWorker to perform your background processing. The background worker thread can communicate information back to the Event Dispatch thread by calling
publish
.你为什么不试试这个:
标签名称.paintImmediately(标签名称.getVisibleRect());
why don't u try this:
labelname.paintImmediately(labelname.getVisibleRect());