【问题标题】:Swing : GridBagLayout adding components not the expected waySwing : GridBagLayout 添加组件不是预期的方式
【发布时间】:2015-10-27 03:48:42
【问题描述】:

我只是想按照this 教程使用 GridBagLayout。

我无法得到我想要的。

我希望每个面板尽可能多地占用空间,但事实并非如此,我真的尝试了一切,按照Oracle Docs中的步骤进行操作

但结果总是一样的:

我真的不明白为什么布局不起作用。

我尝试更改 JFrame 的布局,将面板设置为 ContentPane(),尝试添加在两个网站上找到的约束,但似乎没有任何效果。

感谢您的帮助。


完整代码

public class Monitor extends JFrame{

    // Menu
    private JMenuBar bar = new JMenuBar();
    private JMenu file = new JMenu("File");
    private JMenuItem open = new JMenuItem("Open");
    private JMenuItem exit = new JMenuItem("Exit");

    private JPanel mainPanel = new JPanel();

    private JPanel secondaryPanel;
    private JPanel explorerPanel, tagPanel;

    public Monitor(){
        setTitle("");
        setSize(750,600);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);


        initComps();
    }

    public void initComps(){

        explorerPanel = new JPanel();
        explorerPanel.setBackground(Color.BLACK);
        explorerPanel.setPreferredSize(new Dimension(250,300));

        tagPanel = new JPanel();
        tagPanel.setBackground(Color.yellow);
        tagPanel.setPreferredSize(new Dimension(250, 300));

        secondaryPanel = new JPanel();
        secondaryPanel.setBackground(Color.blue);
        secondaryPanel.setPreferredSize(new Dimension(500, 600));

        mainPanel = new JPanel();
        mainPanel.setPreferredSize(new Dimension(750, 600));
        mainPanel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();

        gbc.gridx = 0 ; gbc.gridy = 0 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(explorerPanel, gbc);

        gbc.gridx = 1 ; gbc.gridy = 0 ; gbc.gridwidth = 2 ; gbc.gridheight = 2;
        mainPanel.add(secondaryPanel, gbc);

        gbc.gridx = 0 ; gbc.gridy = 1 ; gbc.gridwidth = 1 ; gbc.gridheight = 1;
        mainPanel.add(tagPanel, gbc);

        this.setContentPane(mainPanel);

        // Menu Bar
        file.add(open);

        exit.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });
        exit.setMnemonic('q');
        file.add(exit);

        bar.add(file);
        setJMenuBar(bar);
    }

    public static void main(String[] args) {
        new Monitor().setVisible(true);

    }
}

编辑:

添加时

gbc.weightx = 1;
gbc.weighty = 1;

结果

【问题讨论】:

  • 请参阅:madbean.com/anim/totallygridbagmiglayout.com,以免发疯。
  • @Yar 谢谢你的建议。你认为使用 GBL 有可能吗?
  • 是的,这可能是可能的。但 MigLayout 让每个人都永远快乐。
  • @OlivierGrégoire 可能确实是最好的。既然问了这个问题,我等待一个正确的答案。我希望能学到更多东西。 :) 但无论哪种方式都感谢您的建议。

标签: java swing constraints layout-manager gridbaglayout


【解决方案1】:

您需要将GridBagConstraints.weightxGridBagConstraints.weighty 设置为非零值:

来自documentation

GridBagConstraints.weightx、GridBagConstraints.weighty

用于确定如何分配空间,这对于指定调整大小行为很重要。除非您为行 (weightx) 和列 (weighty) 中的至少一个组件指定权重,否则所有组件都会在其容器的中心聚集在一起。这是因为当权重为零(默认值)时,GridBagLayout 对象会在其单元格网格和容器边缘之间放置任何额外的空间。

如果您希望所有组件都填满空间,将其设置为 1 应该可以正常工作。

您在 weightx, weighty 标题下链接的 Oracle tutorial page 上也有这些字段的更多说明。

编辑:

您还需要为组件设置fill,否则默认为NONE

GridBagConstraints.fill

在组件的显示区域大于组件请求的大小时使用,以确定是否(以及如何)调整组件的大小。可能的值是 GridBagConstraints.NONE(默认)、GridBagConstraints.HORIZONTAL(使组件足够宽以水平填充其显示区域,但不要更改其高度)、GridBagConstraints.VERTICAL(使组件足够高以填充其显示区域垂直,但不改变其宽度)和 GridBagConstraints.BOTH(使组件完全填充其显示区域)。

这是我通过将explorerPaneltagPanel 设置为0.33secondaryPanel 设置0.66 并将fill 设置为GridBagConstraints.BOTH 得到的结果:

【讨论】:

  • 也没有用,发帖前我也试过了。看看我的编辑。
  • 谢谢glad3dr。它确实回答了部分问题,但来自小组的比例在另一个答案中更受尊重。虽然赞成。非常感谢您再次提供帮助。 :)
  • 可以通过改变 weightx 和 weighty 值来获得你想要的任何比例。
  • @YassinHajaj 我已更改我的编辑以显示如何调整比例。
【解决方案2】:

你的问题来自你的尺寸。

不要只使用“首选”。也使用最小值。

explorerPanel = new JPanel();
explorerPanel.setBackground(Color.BLACK);
explorerPanel.setMinimumSize(new Dimension(250, 300));
explorerPanel.setPreferredSize(new Dimension(250, 300));

tagPanel = new JPanel();
tagPanel.setBackground(Color.yellow);
tagPanel.setMinimumSize(new Dimension(250, 300));
tagPanel.setPreferredSize(new Dimension(250, 300));

secondaryPanel = new JPanel();
secondaryPanel.setBackground(Color.blue);
secondaryPanel.setMinimumSize(new Dimension(500, 600));
secondaryPanel.setPreferredSize(new Dimension(500, 600));

mainPanel = new JPanel();
mainPanel.setPreferredSize(new Dimension(750, 600));
mainPanel.setMinimumSize(new Dimension(750, 600));
mainPanel.setLayout(new GridBagLayout());

这在我的测试中有效:

【讨论】:

  • 这很令人满意 :) 感谢 Olivier de Bruxelles。非常感谢你。
  • 您可能还想使用GridBagConstraints.fill。多年来我几乎没有碰过任何 GBL/GBC,但我记得它在某种程度上很重要。
  • 肯定会的。祝你有个愉快的夜晚
  • 不推荐将setXXXSize 与 GridBagLayout 一起使用,因为它因忽略它们而臭名昭著。
  • 不推荐使用GridBagLayout,因为它会导致脑损伤:P
【解决方案3】:

您可以使用 BorderLayout 解决此问题

而不是这样:

    this.setContentPane(mainPanel);

你可以这样做:

getContentPane().add(mainPanel, BorderLayout.CENTER);

【讨论】:

  • 发帖前我也试过了,还是不行,谢谢回复。
猜你喜欢
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-18
  • 2011-12-13
  • 2013-03-28
相关资源
最近更新 更多