使用 GridBagLayout 格式化 JPanel [英] Formatting JPanels with GridBagLayout

查看:23
本文介绍了使用 GridBagLayout 格式化 JPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在为我制作的一个小游戏创建一个 GUI.我有 4 个 JPanel,我正在尝试使用 GridBagLayout 在一个框架上定位.

So I am creating a GUI for a small game I'm making as a capstone. I have 4 JPanels I am trying to position on one frame using GridBagLayout.

目前看起来是这样的:

但我希望它在设计上与此更相似:

But I'd like it to be more similar in design to this: Sorry for Bad Paint Skills

框架代码:

public OverlordFrame()
{
    buttonPanel = new ButtonPanel();
    invPanel = new InventoryPanel();
    statPanel = new PlayerStatsPanel(invPanel);
    monstPanel = new MonsterPanel();

    this.setLayout(new GridBagLayout());
    GridBagConstraints c = new GridBagConstraints();

    c.ipadx = 10;
    c.ipady = 50;


    c.gridx = 0;
    c.gridy = 0;
    c.weightx = .1;
    c.weighty = .4;
    c.gridheight = 2;
    c.fill = GridBagConstraints.VERTICAL;
    c.anchor = GridBagConstraints.LINE_START;
    this.add(invPanel, c);


    c.gridx = 1;
    c.gridy = 1;
    c.weightx = .4;
    c.weighty = .3;
    c.gridwidth = 2;
    c.gridheight = 0;
    c.fill = GridBagConstraints.NONE;
    c.anchor = GridBagConstraints.CENTER;
    this.add(buttonPanel, c);


    c.gridx = 2;
    c.gridy = 0;
    c.weightx = .1;
    c.weighty = .4;
    c.gridwidth = 0;
    c.anchor = GridBagConstraints.LINE_END;
    this.add(statPanel, c);


    c.gridx = 1;
    c.weightx = .4;
    c.weighty = .4;
    c.anchor = GridBagConstraints.CENTER;
    this.add(monstPanel, c);


    this.setSize(1440, 810);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setVisible(true);
}

注意:左边的空白区域是 InventoryPanel,在正确的位置,ButtonPanel,也就是 6 个按钮,应该像它们在糟糕的绘画图片中一样,按钮后面的白色区域带有下面的 4 个按钮应该放在怪物所在的位置,右边的 JLabels 应该在右上角.感谢您的帮助

Note: The white space on the left is InventoryPanel, that is kind of in the right place, the ButtonPanel, which is the 6 buttons, should go as they are in the bad paint picture, the white area behind the buttons with the 4 buttons below should go where monster is, and the JLabels on the right should be in the top right corner. Thanks for any help

推荐答案

我不会通过你的代码来尝试找出它不工作的原因,我真的没有时间,但类似于下面的例子似乎与您要查找的内容很接近

I'm not going through your code to try and figure out why it's not working, I don't really have the time, but something like the example below seems to be close to what you're looking for

public class TestPane extends JPanel {

    public TestPane() {
        setLayout(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 2;
        gbc.weightx = 0.3;
        gbc.weighty = 1;
        gbc.fill = GridBagConstraints.BOTH;
        add(makePane(Color.BLUE), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.gridheight = 1;
        gbc.weightx = 0.4;
        gbc.weighty = 0.5;
        add(makePane(Color.MAGENTA), gbc);

        gbc.gridx = 2;
        gbc.weightx = 0.1;
        add(makePane(Color.YELLOW), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.gridwidth = 2;
        gbc.weightx = 0.7;
        gbc.weighty = 0.5;
        gbc.fill = GridBagConstraints.BOTH;
        add(makePane(Color.CYAN), gbc);
    }

    protected JPanel makePane(Color color) {
        JPanel panel = new JPanel();
        panel.setBackground(color);
        return panel;
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(200, 200);
    }

}

话虽如此,我可能很想使用一系列复合 BorderLayout 来产生相同的效果,但这只是我的想法

Having said that, I'd probably be tempted to use a series of compound BorderLayouts to generate the same effect, but that's just me

这篇关于使用 GridBagLayout 格式化 JPanel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆