[英]Adding JFrame to Main GUI Frame from another class
I have two classes. 我有两节课。 1.One class contains all the GUI code generated by Netbeans GUI builder (call it GUI class) 2.Another class contains some code containing methods (SaveTraffic which extends SwingWorker).
1.一个类包含Netbeans GUI构建器生成的所有GUI代码(称为GUI类)。2.另一个类包含一些包含方法的代码(SaveTraffic扩展了SwingWorker)。
In my SaveTraffic , I create a frame which just pops up and come in front of my GUI when I run my application. 在我的SaveTraffic中,创建一个框架,该框架在运行应用程序时会弹出并出现在GUI的前面。 I want to add this frame to my GUI at the specific location where I want to place it.
我想将此框架添加到我要放置它的特定位置的GUI中。 How is this possible?
这怎么可能? This is my code of SaveTraffic
这是我的SaveTraffic代码
public class SaveTraffic extends SwingWorker<Void, String> {
public static int k = 0;
public GUI f = new GUI();
public static Frame u = new Frame();
public static JTextArea h = new JTextArea();
JScrollPane scrollPane2 = new JScrollPane(h, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
@Override
public Void doInBackground() throws IOException {
u.setSize(950, 200);
u.setLocation(0,550);
u.add(scrollPane2);
u.setVisible(true);
while (f.num() != 2) {
{
publish("Hello World");
}
}
return null;
} //end main function
@Override
public void process(List<String> chunks) {
for (final String text : chunks) {
Runnable r = new Runnable() {
@Override
public void run() {
h.setLineWrap(true);
h.append(text + "\n");
}
};
new Thread(r).start();
}
}
@Override
public void done() {
System.out.println("I am DONE");
}
}
I see several problems in your code: 我在您的代码中看到了几个问题:
setModal()
. setModal()
来选择对话框是模式对话框还是非模式对话框。 (Optional) I would also consider adding a constructor in SaveTraffic and initialize most of the code there. (可选)我还将考虑在SaveTraffic中添加一个构造函数,并在那里初始化大多数代码。
don't built Swing Gui
inside SwingWorker's
methods, SwingWorker
is designated to add / modify / change
value in JComponent(s)
that already exist 不要在
SwingWorker's
方法中构建Swing Gui
,而是指定SwingWorker
在JComponent(s)
add / modify / change
值
add process()
or publish()
to the SwingWorker
, inside this method put value or append text the JTextArea
periodically 向
SwingWorker
添加process()
或publish()
,在此方法内放置值或定期向JTextArea
追加文本
for "Monitoring" SwingWorker's
statuses add PropertyChangeListener 为“监视”
SwingWorker's
状态添加PropertyChangeListener
You make a class, that extends JPanel. 您创建一个类,扩展了JPanel。 Then you do all your code in there, and bind it with your method class.
然后,在其中完成所有代码,并将其与方法类绑定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.