简体   繁体   中英

How can I align components in a JFrame vertically?

I have tried multiple solutions but nothing fits me! I want to align everything vertically on the center of the frame.

    window=new JFrame();
    window.setSize(520, 380);

    window.setTitle("Menu");
    window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    window.setLayout(new FlowLayout());   
    label=new JLabel("Settings",JLabel.CENTER);
    controlPanel= new JPanel();
    controlPanel.setLayout(new GridLayout(10,1));

    controlPanel.add(button1);//these are example components
    controlPanel.add(button2);
    controlPanel.add(button3);

    window.add(label);
    window.add(controlPanel);
    window.setVisible(true);

I want to have as a title the word "Settings" and exactly underneath it my components. I was able to do this with a gridLayout instead of FlowLayout but the title occupies half of the screen(and i dont want that). sorry for a basic question like that but i am new to java :)

You could use GridBagLayout , for example...

GridBagLayout

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

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() {
            setBorder(new EmptyBorder(50, 50, 50, 50));
            setLayout(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.fill = GridBagConstraints.HORIZONTAL;

            add(new JButton("Apples"), gbc);
            add(new JButton("Pears"), gbc);
            add(new JButton("Organges"), gbc);
            add(new JButton("Grapes"), gbc);
        }

    }

}

Remember, with it's flexibility, comes complexity. Have a look at How to Use GridBagLayout for more details

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