1

我正在使用 SWING 并使用简单的 Group 布局进行写作

我为布局编写了这段代码:

    JPanel unterPanel = new JPanel();
    GroupLayout layout = new GroupLayout(unterPanel);
    unterPanel.setLayout(layout);
    layout.setAutoCreateGaps(true);
    layout.setAutoCreateContainerGaps(true);

    layout.setHorizontalGroup(
            layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                    .addComponent(header)
                    .addComponent(tableG)
                    .addGroup(layout.createSequentialGroup()
                            .addComponent(b3)
                            .addComponent(b4)
                            .addComponent(b5)
                            .addComponent(b6)
                            ))
            );
    layout.setVerticalGroup(
            layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(b3)
                    .addComponent(b4)
                    .addComponent(b5)
                    .addComponent(b6)
                    )   
                    .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(header)
                            .addComponent(tableG))
            );

    layout.linkSize(SwingConstants.HORIZONTAL, b3, b4, b5, b6);

    return unterPanel;

但是当我编译代码时,我得到:

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Component must be non-null
    at javax.swing.GroupLayout$ComponentSpring.<init>(GroupLayout.java:2878)
    at javax.swing.GroupLayout$ComponentSpring.<init>(GroupLayout.java:2858)
    at javax.swing.GroupLayout$Group.addComponent(GroupLayout.java:1490)
    at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2414)
    at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2384)
    at javax.swing.GroupLayout$Group.addComponent(GroupLayout.java:1471)
    at javax.swing.GroupLayout$ParallelGroup.addComponent(GroupLayout.java:2406)
    at gui.guimain.Produktmaingui(guimain.java:97)
    at gui.guimain.showStart(guimain.java:306)
    at gui.guimain$1.run(guimain.java:410)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

为什么?

我很欣赏你的回答!!!

4

3 回答 3

4

您要添加的组件之一为 null,换句话说,这些变量之一是:

  • 标题
  • 表G
  • b3
  • b4
  • b5
  • b6

罪魁祸首在您班级的第 97 行,如堆栈跟踪中所述。

于 2012-10-04T13:27:03.680 回答
2

转到 gui.guimain.Produktmaingui 中的第 97 行,您在那里调用 addComponent。该参数可能为空。如果参数不为空,它可能是参数的内容。

于 2012-10-04T13:27:27.770 回答
1
  • 放入 ( ) JTable_JScrollPaneBorderLayout.CENTER

  • 创建另一个JPanelGridLayout放在那里 4. 按钮,1st JButton在左侧更改为setVisible(false)( BorderLayout.SOUTH)

  • JPanels这些拖到JTabbedPanes Tab

于 2012-10-04T13:30:04.167 回答