简体   繁体   中英

Separating Labels, Buttons, Etc. with JFrame

I recently learned how to do basic things with JFrame. I am not trying to figure out when i add panels to a frame. How can I separate labels, buttons, etc. onto their own separate lines.

For this code I am trying to separate the buttons onto a new line underneath the label.

public GameFrame(){
    setSize(854, 480);
    setVisible(true);
    setResizable(false);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setTitle("Board Games");

    JPanel thePanel = new JPanel();
    this.add(thePanel);

    JLabel header = new JLabel("Board Game Galleria");
    thePanel.add(header);

    JButton select = new JButton("Select a game");
    thePanel.add(select);

    JButton start = new JButton("Start over");
    thePanel.add(start);

    JButton exit = new JButton("Exit");
    thePanel.add(exit);
}

You can use gridBag layout to do it correctly. Using this answer you can achieve the goal.I added new method to adding components to the layout. Also you can use Insets to manage the borders as well.

public class Main
{

//An Insets object is a representation of the borders of a container. 
//It specifies the space that a container must leave at each of its edges. The space can be a border, a blank space, or a title.

private static final Insets insetsData = new Insets(2, 2, 2, 2);

public static void main(final String args[])
{

//main frame
final JFrame mainFrame = new JFrame("GridBagLayout");
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//set layout to your frame
mainFrame.setLayout(new GridBagLayout());

//adding header
JLabel header = new JLabel("Board Game Galleria", SwingConstants.CENTER);
addComponentToGridBag(mainFrame, header, 0, 0, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

JButton select = new JButton("Select a game");
addComponentToGridBag(mainFrame, select, 0, 1, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

JButton start = new JButton("Start over");
addComponentToGridBag(mainFrame, start, 0, 2, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

JButton exit = new JButton("Exit");
addComponentToGridBag(mainFrame, exit, 0, 3, 1, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH);

mainFrame.setSize(500, 200);
mainFrame.setVisible(true);
}

/**
 * Adding components to the gridBag layout.
 * 
 */
 private static void addComponentToGridBag(Container container,    Component component, int gridx, int gridy,
                                        int gridwidth, int gridheight, int anchor, int fill)
{

GridBagConstraints gridBagConstraints = new GridBagConstraints(gridx, gridy, gridwidth, gridheight, 1.0, 1.0,
    anchor, fill, insetsData, 0, 0);
container.add(component, gridBagConstraints);
}
}

在此处输入图片说明

Start by having a look at Laying Out Components Within a Container . While GridLayout can do this, relatively simply, you're going to want to get your head around GridBagLayout sooner rather then later (IMHO)

GridBagLayout

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestLayout {

    public static void main(String[] args) {
        new TestLayout();
    }

    public TestLayout() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER; // This is a trick
            gbc.fill = GridBagConstraints.HORIZONTAL;
            JPanel thePanel = new JPanel();
            this.add(thePanel);

            JLabel header = new JLabel("Board Game Galleria");
            add(header, gbc);

            JButton select = new JButton("Select a game");
            add(select, gbc);

            JButton start = new JButton("Start over");
            add(start, gbc);

            JButton exit = new JButton("Exit");
            add(exit, gbc);
        }

    }

}

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