为什么第二个JFrame不显示组件? [英] Why does the second JFrame does not show components?
问题描述
我有第一个JFrame,它工作正常.当我按下按钮时,它应该显示一个JProgressBar框架,但是我得到的是空的JFrame.我用
I have the first JFrame and it works fine. When I push a button it is supposed to show a JProgressBar frame , but i get empty JFrame. I open it with
p = new Progress("1/3");
p.setMax(2);
p.setProgress(0, "Getting bytes...");
有人知道为什么吗?
我将解释更多细节(因为有人误解并以错误的方式纠正了我的帖子)-在我的主类中,我启动了第一个JFrame:
new Crypt();
在Crypt类中,我注册了一个按钮ActionListener. OnClick会打开第二个JFrame ,但它为空:
I am going to explain more detail(Because someone misunderstood and corrected my post in the wrong way) - On my main class i start the first JFrame:
new Crypt();
And in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty:
p = new Progress("1/3");
p.setMax(2);
p.setProgress(0, "Getting bytes...");
推荐答案
在地穴类中,我注册了一个按钮ActionListener. OnClick会打开第二个JFrame,但它为空
in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty
从Swing侦听器调用的
代码在Event Dispatch Thread (EDT)
上执行. EDT负责绘制Swing组件.由于您的代码正在EDT y9ou上执行长时间运行的任务,因此在任务完成之前,Swing无法绘画组件.
Code invoked from an Swing listener executes on the Event Dispatch Thread (EDT)
. The EDT is responsible for painting Swing components. Since your code is executing a long running task on the EDT y9ou are preventing Swing from painting the component until the task is finished.
您需要为长期运行的任务启动一个单独的线程.或者更好的是,您可能应该使用SwingWorker
.阅读有关 Swing中的并发的Swing教程中的部分,其中介绍了对此进行了更详细的介绍,并提供了一个SwingWorker的工作示例.
You need to start a separate Thread for your long running task. Or better yet you should probably be using a SwingWorker
. Read the section from the Swing tutorial on Concurrency in Swing which explains this in more detail and provides a working example of a SwingWorker.
这篇关于为什么第二个JFrame不显示组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!