简体   繁体   English

从另一个类向主GUI框架添加JFrame

[英]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: 我在您的代码中看到了几个问题:

  1. You are looking for JDialog and not a new frame. 您在寻找JDialog,而不是新框架。 Make sure to pass the correct Dialog owner to the constructor of JDialog. 确保将正确的Dialog所有者传递给JDialog的构造函数。 You can choose wheter the dialog is modal or not through a parameter on the constructor or by calling setModal() . 您可以通过构造函数上的参数或调用setModal()来选择对话框是模式对话框还是非模式对话框。
  2. You are starting new Threads inside the SwingWorker.publish method. 您正在SwingWorker.publish方法中启动新线程。 Those thread modify the UI. 这些线程会修改UI。 Althout JTextArea.append is Thread-safe, I would still not use multiple Threads to update the JTextArea. 尽管JTextArea.append是线程安全的,但我仍然不会使用多个线程来更新JTextArea。 It is unecessary and if some day you add some calls that are not Thread-safe you will end up on a deadlock. 这是不必要的,如果某天您添加了一些不是线程安全的调用,那么最终将陷入死锁。
  3. You are adding components to your frame, setting its size and making it visible in the SwingWorker.doInBackground() which is not run on the EDT. 您正在向框架中添加组件,设置其大小并在未在EDT上运行的SwingWorker.doInBackground()中使其可见。 Instead move that call before executing your SwingWorker, inside the EDT. 而是在执行EDT内的SwingWorker之前移动该调用。
  4. No need to constantly set the line wrap to true on the textarea. 无需在文本区域上始终将换行设置为true。 Just calling it once is enough. 只需调用一次就足够了。
  5. I am not sure what 'k' is used for, but I would beware of static variables. 我不确定'k'是用来做什么的,但是我会提防静态变量。
  6. Why do you make your Frame and your TextArea static? 为什么要使Frame和TextArea保持静态?

(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 ,而是指定SwingWorkerJComponent(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

  • simple example 简单的例子

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM