我似乎无法让我的Java Swing组件正确地协同工作。
我想要做的是,使一个JPanel填满JTabbedPane内所有可用的空间。目前,我的设置如下:
public class Gui extends JFrame {
private final EventBus eventBus = EventBus.getInstance();
private final ToolkitUtil toolkitUtil;
private final Menu menu;
private final InfoBar infoBar;
private final JTabbedPane pane;
...
private void buildLayout() {
GridBagConstraints gbc = new GridBagConstraints();
setJMenuBar(menu);
add(pane, BorderLayout.CENTER);
add(infoBar, BorderLayout.SOUTH);
pane.addTab("Plugins", new PluginPanel());
}
}
public class PluginPanel extends JPanel {
private final JPanel modelPanel;
private final JPanel editorPanel;
public PluginPanel() {
setLayout(new GridBagLayout());
modelPanel = new JPanel(new GridBagLayout());
editorPanel = new JPanel(new GridBagLayout());
buildLayout();
}
private void buildLayout() {
GridBagConstraints gbc = new GridBagConstraints();
modelPanel.setBorder(BorderFactory.createTitledBorder("Models"));
editorPanel.setBorder(BorderFactory.createTitledBorder("Editors"));
gbc.gridx = 0;
gbc.fill = GridBagConstraints.BOTH;
modelPanel.add(new JLabel("test label"), gbc);
add(modelPanel, gbc);
gbc.gridx = 1;
add(editorPanel, gbc);
}
}
这将创建一个窗口,其大小符合我的要求(动态比例适应屏幕大小,不包括在上述代码中)。位于中心的选项卡面板将展开以填充所需的所有空间,这正是我想要的。但是,在选项卡面板内添加的面板只有与内容相同的大小。如果添加标签或任何东西,它只会增长到组件的大小。我希望它们始终扩展以填充选项卡面板。