EDT位置有几个JFrame窗口 [英] EDT location with several JFrame window
问题描述
我有一个Swing JFrame。如果我在程序执行期间在新线程中创建一个新的JFrame,那么EDT会是什么?在最后一个JFrame窗口的当前线程中或在第一个窗口中。
I have a Swing JFrame. If I create a new JFrame in a new thread during the program execution where will be the EDT ? In the current thread of the last JFrame window or in the first window.
编辑:
感谢您的回答。
Thanks for your answers.
我理解他们,我很好。我知道我们不必在EDT的其他地方创建摆动对象,但我遇到问题。
I understand them and i'm ok with them. I know that we don't must create swing object elsewhere that the EDT but I meet problem.
我解释;我开发了一个JAVA应用程序,用于创建和提取存档,如winrar。您可以使用多线程同时创建多个arhive。最近,我希望在每次创建时在新的JFrame中以JprogressBar的形式在存档创建期间添加信息状态。但我的问题是在新的状态框架和创建存档的线程中生成通信。这就是为什么我在存档线程中创建JFrame以更新当前的进度条。
I explain; I developed a JAVA application for create and extract archive like winrar. You can create several arhive in same time with multi-thread. And recently, I wanted add an information status during the archive creation in the form of JprogressBar in a new JFrame at every creation. But my problem is for generate a communication in the new status frame and the thread who create archive. That's why, I create the JFrame in the archive thread for update the progress bar currently.
但是我可以在潜水员信息源和你的答案/评论中阅读它,它反对java摇摆和性能;我不能在EDT的其他地方创建swing对象。
But like i could read it in divers information source and on your answers/comments, it's against java swing and performance; I can't create swing object elsewhere that the EDT.
但是,我应该如何解决我的问题呢?
But then, how should I solve my problem ?
推荐答案
EDT - 事件派发线程 - 与任何具体的GUI组件分开,例如JFrame。
The EDT - the event dispatch thread - is separate from any concrete GUI component, such as a JFrame.
一般来说你应该在EDT上创建所有GUI组件,但这并不意味着他们拥有EDT,EDT也不拥有组件。
Generally you should create all GUI components on the EDT, but that does not mean they own the EDT, nor does the EDT own the components.
要创建两个JFrame,两者都在EDT,您可以执行以下操作:
To create two JFrames, both on the EDT, you can do the following:
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame1 = new JFrame("Frame 1");
frame1.getContentPane().add(new JLabel("Hello in frame 1"));
frame1.pack();
frame1.setLocation(100, 100);
frame1.setVisible(true);
}
});
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame2 = new JFrame("Frame 2");
frame2.getContentPane().add(new JLabel("Hello in frame 2"));
frame2.pack();
frame2.setLocation(200, 200);
frame2.setVisible(true);
}
});
}
这篇关于EDT位置有几个JFrame窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!