java GroupLayout 一次只能用于一个容器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13533982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:07:46  来源:igfitidea点击:

GroupLayout can only be used with one container at a time

javaswingexceptionlayout-managergrouplayout

提问by Nonconformist

Not sure why this error is coming up. I am using GroupLayout because I want it to do the spacing for me and will be adding more panels to the frame in the future. Below is the stack trace.

不知道为什么会出现这个错误。我使用 GroupLayout 是因为我希望它为我做间距,并且将来会向框架添加更多面板。下面是堆栈跟踪。

Exception in thread "main" java.lang.IllegalArgumentException: GroupLayout can only be used with one Container at a time
    at javax.swing.GroupLayout.checkParent(Unknown Source)
    at javax.swing.GroupLayout.invalidateLayout(Unknown Source)
    at java.awt.Container.invalidate(Unknown Source)
    at java.awt.Component.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at javax.swing.JComponent.addNotify(Unknown Source)
    at javax.swing.JRootPane.addNotify(Unknown Source)
    at java.awt.Container.addNotify(Unknown Source)
    at java.awt.Window.addNotify(Unknown Source)
    at java.awt.Frame.addNotify(Unknown Source)
    at java.awt.Window.pack(Unknown Source)
    at client.AlternateGUI.drawGui(AlternateGUI.java:54)
    at client.AlternateGUI.main(AlternateGUI.java:24)

Here is the code:

这是代码:

package client;

import java.awt.Component;*

public class AlternateGUI {
    private JList people;
    private DefaultListModel dlm;
    private JLabel l1, l2, l3;
    private JFrame alternateGUIFrame;
    private final static ImageIcon unavailableIcon = new ImageIcon("offline.png");

    public static void main(String[] args)
    {
        AlternateGUI ls = new AlternateGUI();
        ls.drawGui();
    }

    public AlternateGUI(){
        dlm = new DefaultListModel();
        people = new JList(dlm);

        alternateGUIFrame = new JFrame();

        JScrollPane peopleScroller = new JScrollPane(people, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        people.setCellRenderer(new CellRenderer());

        GroupLayout alternateGUILayout = new GroupLayout(alternateGUIFrame);
        alternateGUIFrame.setLayout(alternateGUILayout);

        alternateGUILayout.setAutoCreateGaps(true);
        alternateGUILayout.setAutoCreateContainerGaps(true);
        alternateGUILayout.setHorizontalGroup(alternateGUILayout.createSequentialGroup()
                .addComponent(peopleScroller));
        alternateGUILayout.setVerticalGroup(alternateGUILayout.createSequentialGroup()
                .addComponent(peopleScroller));
        }

    public void drawGui() {
        l1 = new JLabel("Hi", unavailableIcon , JLabel.LEFT);
        l2 = new JLabel("Hello", unavailableIcon , JLabel.LEFT);
        l3 = new JLabel("Bye", unavailableIcon , JLabel.LEFT);
        dlm.addElement(l1);
        dlm.addElement(l2);
        dlm.addElement(l3);

        alternateGUIFrame.pack();
        alternateGUIFrame.setVisible(true);
    }

    class CellRenderer implements ListCellRenderer
    {
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
        {
            Component com = (Component)value;
            return com;
        }
    }
}

回答by Hovercraft Full Of Eels

Your problem is that you're unknowingly adding the layout to both the JFrame and its contentPane. The solution is to make sure you add it to the JFrame's contentPane only:

您的问题是您在不知不觉中将布局添加到 JFrame 及其 contentPane。解决方案是确保仅将其添加到 JFrame 的 contentPane 中:

  GroupLayout alternateGUILayout = new GroupLayout(alternateGUIFrame.getContentPane());
  alternateGUIFrame.getContentPane().setLayout(alternateGUILayout);

It's the first line above that matters the most.

最重要的是上面的第一行。

Another option is to work with JPanels, and then add the JPanels to the JFrame's contentPane in its default BorderLayout.CENTER position.

另一种选择是使用 JPanels,然后将 JPanels 添加到 JFrame 的 contentPane 的默认 BorderLayout.CENTER 位置。