立即更改 JLabel

发布于 2024-12-11 09:33:56 字数 436 浏览 0 评论 0原文

我的应用程序由两个类文件组成,我们称它们为 A 和 B。A 是主要的 - 执行的文件和 jar 文件的主类。在AI中定义一个JFrame,我在其中添加两个JPanel:其中一个由JButtons和JLabels组成,另一个是图形化的,并在B中实现(B extends JPanel)。

在 BI 中,有一个方法可能需要一段时间,所以我希望向用户提供有关该过程正在进行的信息;也就是说,我想告诉用户还剩下多少计算量。我想通过更新 A 类中的 JLabel 来完成此操作。但是,当我尝试时,我发现 JLabel 仅在该方法完成后才更新,即我只看到它从 0% 运行到 100% 100%,始终如此。

我认为我没有任何相关的代码可以向您展示,但如果您希望我发布一些内容,请告诉我。

我检查过类似的问题,但在我看来,他们问了一些完全不同的问题。

谢谢你,

罗恩。

My application consists of two class files, let's call them A and B. A is the main one - the one which is executed and the main class of the jar file. In A I define a JFrame, to which I add two JPanels: one of them is composed of JButtons and JLabels, and the other one is a graphical one, and is implemented in B (B extends JPanel).

In B I have a method which could take a while, so I wish to give the user information about the process going on there; namely, I'd like to tell the user how much computation is left. I want to do it by updating a JLabel in class A. However, when I try it, I get that the JLabel is updated only after the method had been completed, i.e. instead of seeing it running from 0% to 100% I only see 100%, all the time.

I don't think I have any relevant pieces of code to show you, but if there's something you'd want me to post, please do let me know.

I've checked similar questions but it appears to me that they asked something quite different.

Thank you,

Ron.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

栖竹 2024-12-18 09:33:56

你的问题是线程问题。您的长时间运行的进程正在占用主 Swing 线程(EDT),从而阻止任何绘制、组件更新或用户交互。您需要在后台线程(例如由 SwingWorker 对象提供的线程)中执行长时间运行的进程。这样做,你的 GUI 将变得更加灵敏。

查看Swing 中的并发,了解有关这一重要主题的更多信息。

如果您需要更具体的帮助,您将需要提供更多信息和代码。

Yours is a threading problem. Your long-running process is tying up the main Swing thread, the EDT, preventing any drawing, updating of components, or user interactions. You need to do the long-running process in a background thread such as one supplied by a SwingWorker object. Do this and your GUI will become much more responsive.

Check out Concurrency in Swing for more on this important topic.

If you need more specific help, you will need to supply more information and code.

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