我已经使用GroupLayout在JPanel上定位组件,但我似乎无法让它工作。当我在水平和垂直组中注释掉前6个组件时,它工作正常,但我需要这些组件。所有组件都存在于水平和垂直组中,因此不应该是问题。这是代码
JPanel centerPanel = new JPanel();
GroupLayout layoutCenterPanel = new GroupLayout(centerPanel);
layoutCenterPanel.setAutoCreateGaps(true);
layoutCenterPanel.setAutoCreateContainerGaps(true);
centerPanel.setLayout(layoutCenterPanel);
layoutCenterPanel.setHorizontalGroup(
layoutTopPanel.createSequentialGroup()
.addGroup(layoutCenterPanel.createParallelGroup()
.addGroup(layoutCenterPanel.createSequentialGroup()
.addComponent(fileTypeComboBox)
.addComponent(fileExtensionComboBox)
.addComponent(locationComboBox)
)
.addGroup(layoutCenterPanel.createSequentialGroup()
.addComponent(jScrollPane1)
.addComponent(jScrollPane2)
.addComponent(jScrollPane3)
)
)
.addGroup(layoutCenterPanel.createParallelGroup()
.addComponent(searchFileContent)
.addComponent(searchHiddenFiles)
.addComponent(searchArchivedFiles)
.addComponent(searchReadOnlyFiles)
.addComponent(indexOnly)
.addComponent(entireSpace)
)
);
layoutCenterPanel.setVerticalGroup(
layoutCenterPanel.createParallelGroup()
.addGroup(layoutCenterPanel.createSequentialGroup()
.addGroup(layoutCenterPanel.createParallelGroup()
.addComponent(fileTypeComboBox)
.addComponent(fileExtensionComboBox)
.addComponent(locationComboBox)
)
.addGroup(layoutCenterPanel.createParallelGroup()
.addComponent(jScrollPane1)
.addComponent(jScrollPane2)
.addComponent(jScrollPane3)
)
)
.addGroup(layoutCenterPanel.createSequentialGroup()
.addComponent(searchFileContent)
.addComponent(searchHiddenFiles)
.addComponent(searchArchivedFiles)
.addComponent(searchReadOnlyFiles)
.addComponent(indexOnly)
.addComponent(entireSpace)
)
);
add(centerPanel);
...和stacktrace的一部分
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at javax.swing.GroupLayout.areParallelSiblings(GroupLayout.java:1175)
at javax.swing.GroupLayout.access$1000(GroupLayout.java:208)
at javax.swing.GroupLayout$AutoPreferredGapSpring.addTarget(GroupLayout.java:3338)
at javax.swing.GroupLayout$ParallelGroup.insertAutopadding(GroupLayout.java:2619)
at javax.swing.GroupLayout$SequentialGroup.insertAutopadding(GroupLayout.java:2232)
at javax.swing.GroupLayout$SequentialGroup.insertAutopadding(GroupLayout.java:2232)
at javax.swing.GroupLayout.insertAutopadding(GroupLayout.java:1144)