简体   繁体   中英

Building a GUI using GridBagLayout (Java)

I am trying to build a GUI for a little Java game using a GridBagLayout. Finally it should look like this:

箱形图

This is my code:

    private GridBagConstraints c;

    ...

    c.gridx = 1;
    c.gridy = 0;
    c.weightx = 0;
    add(playButton, c);

    c.gridx = 1;
    c.gridy = 1;
    c.weightx = 0;
    add(optionsButton, c);

    c.gridx = 1;
    c.gridy = 2;
    c.weightx = 0;
    add(manualButton, c);

    c.gridx = 1;
    c.gridy = 3;
    c.weightx = 0;
    add(exitButton, c); 

    c.gridx = 0;
    c.gridy = 4;
    c.weightx = 1;      
    c.anchor = GridBagConstraints.SOUTHWEST;
    add(creditsButton, c);

    c.gridx = 2;
    c.gridy = 4;
    c.weightx = 1;
    c.anchor = GridBagConstraints.SOUTHEAST;
    add(legalNoticeButton, c);

At the moment it looks like this:

按钮布局

My question is who I can set the two buttons to the bottom without setting the four other bottoms to the top?

Something like this is more or less @Gilbert Le Blanc explained (with minor differences):

Basically use another panel to move your two buttons to the south and spread them to WEST and EAST:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestGridBagLayout {

    protected void initUI() {
        JFrame frame = new JFrame("test");
        final JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridBagLayout());
        JButton playButton = new JButton("Play");
        JButton optionsButton = new JButton("Options");
        JButton manualButton = new JButton("Manual");
        JButton exitButton = new JButton("Exit");
        JButton creditsButton = new JButton("Credits");
        JButton legalNoticeButton = new JButton("Legal");
        GridBagConstraints c = new GridBagConstraints();
        c.gridwidth = GridBagConstraints.REMAINDER;
        centerPanel.add(playButton, c);
        centerPanel.add(optionsButton, c);
        centerPanel.add(manualButton, c);
        centerPanel.add(exitButton, c);

        JPanel bottomPanel = new JPanel(new BorderLayout());
        bottomPanel.add(creditsButton, BorderLayout.WEST);
        // Filler component that avoids having the bottom panel too small
        bottomPanel.add(new JComponent() {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(centerPanel.getPreferredSize().width, 0);
            }
        });
        bottomPanel.add(legalNoticeButton, BorderLayout.EAST);
        frame.add(centerPanel);
        frame.add(bottomPanel, BorderLayout.SOUTH);
        frame.pack();
        frame.setMinimumSize(frame.getPreferredSize());
        frame.setVisible(true);
    }

    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
            UnsupportedLookAndFeelException {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new TestGridBagLayout().initUI();
            }
        });
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM