Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

It currently looks like this: Current GUI Logic Error enter image description here

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

Code for the frame:

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);
}

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
146 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Example

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);
    }

}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...