Why does the second JFrame does not show components?

不问归期 提交于 2019-12-13 03:38:49

问题


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...");

Anyone know why?

EDIT: 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...");

The Progress class Screen shot


回答1:


in the Crypt class i have registered a button ActionListener. OnClick it opens a second JFrame But it is empty

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.

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.



来源:https://stackoverflow.com/questions/25078810/why-does-the-second-jframe-does-not-show-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!