仅 jlabel 的鼠标单击事件正确更新 jlabel 文本属性

发布于 2024-08-11 02:07:16 字数 234 浏览 4 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

醉态萌生 2024-08-18 02:07:16

不确定到底发生了什么,但我首先要确保您的更新发生在事件分派线程中。让更新程序线程调用:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    // ...update the text...
  }
});

请参阅事件调度线程 教程。

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:

SwingUtilities.invokeLater(new Runnable() {
  public void run() {
    // ...update the text...
  }
});

See the Event Dispatch Thread tutorial.

转瞬即逝 2024-08-18 02:07:16

您必须确保对 Swing UI 组件的任何更改都在事件调度线程上执行。以下是实现此目的的两个建议:

Timer

您可能需要查看 javax.swing.Timer(如果目的是定期刷新 JLabel 文本)。计时器在事件调度线程上定期触发 ActionEvent

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          label.setText("foo");
      }
  };
  new Timer(delay, taskPerformer).start();

SwingWorker

或者,您可能需要考虑使用 SwingWorker 执行后台处理。后台工作线程可以通过调用 publish 将信息传递回事件调度线程。

new SwingWorker<Void, String>() {
  // Called by the background thread.
  public Void doInBackground() {
    for (int i=0; i<100; ++i) {
      // Do work.  Periodically publish String data.
      publish("Job Number: " + i);
    }
  }

  // Called on the Event Dispatch Thread.
  @Override
  protected void process(List<String> chunks) {
    // Update label text.  May only want to set label to last
    // value in chunks (as process can potentially be called
    // with multiple String values at once).
    for (String text : chunks) {
      label.setText(text);
    }
  }  
}.execute();

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 fires ActionEvents periodically on the Event Dispatch Thread:

  int delay = 1000; //milliseconds
  ActionListener taskPerformer = new ActionListener() {
      public void actionPerformed(ActionEvent evt) {
          label.setText("foo");
      }
  };
  new Timer(delay, taskPerformer).start();

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.

new SwingWorker<Void, String>() {
  // Called by the background thread.
  public Void doInBackground() {
    for (int i=0; i<100; ++i) {
      // Do work.  Periodically publish String data.
      publish("Job Number: " + i);
    }
  }

  // Called on the Event Dispatch Thread.
  @Override
  protected void process(List<String> chunks) {
    // Update label text.  May only want to set label to last
    // value in chunks (as process can potentially be called
    // with multiple String values at once).
    for (String text : chunks) {
      label.setText(text);
    }
  }  
}.execute();
倥絔 2024-08-18 02:07:16

你为什么不试试这个:
标签名称.paintImmediately(标签名称.getVisibleRect());

why don't u try this:
labelname.paintImmediately(labelname.getVisibleRect());

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文