如何使Java Swing组件填充可用空间?

时间:2023-02-09 16:46:53

I cannot seem to get my Java Swing components to work together correctly.

我似乎无法使我的Java Swing组件正确地协同工作。

What I want to do, is have a JPanel fill ALL the space available inside a JTabbedPane. At the moment, my setup is as follows:

我想要做的是,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);
    }
}

This creates a windows that is my desired size (dynamically proportional to the screen size, not included in above code). The tab panel that is placed in the center is expanded to fill all the space required, which is exactly what I want. But, the panels I add inside the tab panel are only as big as their content. If I add labels or anything, it only grows as big as the components. I want them to always be expanded to fill the tab panel.

这会创建一个我所需大小的窗口(与屏幕大小动态成比例,不包含在上面的代码中)。放置在中心的选项卡面板将展开以填充所需的所有空间,这正是我想要的。但是,我在选项卡面板中添加的面板仅与其内容一样大。如果我添加标签或任何东西,它只会增加与组件一样大。我希望它们始终可以扩展以填充选项卡面板。

3 个解决方案

#1


33  

Try setting the weights of the GridBagConstraints to non-zero values:

尝试将GridBagConstraints的权重设置为非零值:

gbc.weightx = gbc.weighty = 1.0;

#2


53  

The easiest way is to use a BorderLayout and put the component in the CENTER position.

最简单的方法是使用BorderLayout并将组件放在CENTER位置。

#3


0  

Set MinimumSize of your container to preffered size you want, then set ContentPane of container with your panel.

将容器的MinimumSize设置为所需的优先级,然后使用面板设置容器的ContentPane。

    setMinimumSize(new Dimension(width,height));
    setContentPane(new MyPanel());

This code works for all layouts.

此代码适用于所有布局。

Or simply call for your container:

或者只是打电话给你的容器:

    setContentPane(new MyPanel());

This code works for BorderLayout or Free Design

此代码适用于BorderLayout或Free Design

#1


33  

Try setting the weights of the GridBagConstraints to non-zero values:

尝试将GridBagConstraints的权重设置为非零值:

gbc.weightx = gbc.weighty = 1.0;

#2


53  

The easiest way is to use a BorderLayout and put the component in the CENTER position.

最简单的方法是使用BorderLayout并将组件放在CENTER位置。

#3


0  

Set MinimumSize of your container to preffered size you want, then set ContentPane of container with your panel.

将容器的MinimumSize设置为所需的优先级,然后使用面板设置容器的ContentPane。

    setMinimumSize(new Dimension(width,height));
    setContentPane(new MyPanel());

This code works for all layouts.

此代码适用于所有布局。

Or simply call for your container:

或者只是打电话给你的容器:

    setContentPane(new MyPanel());

This code works for BorderLayout or Free Design

此代码适用于BorderLayout或Free Design