使用JFrame分隔标签,按钮等

时间:2015-09-02 03:24:24

标签: java swing jframe

我最近学会了如何用JFrame做基本的事情。我不想弄清楚我何时向一个框架添加面板。如何将标签,按钮等分隔到各自的行上。

对于此代码,我试图将按钮分隔到标签下面的新行。

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

2 个答案:

答案 0 :(得分:4)

您可以使用gridBag布局正确执行此操作。使用此答案,您可以实现目标。我添加了新的方法来向布局添加组件。 您也可以使用Insets来管理边框。

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

enter image description here

答案 1 :(得分:3)

首先看一下Laying Out Components Within a Container。虽然GridLayout可以做到这一点,但相对简单,您可能希望尽快({3}}尽快开始(恕我直言)

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

    }

}