简体   繁体   English

如何在JFrame中垂直对齐组件?

[英]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). 我能够使用gridLayout而不是FlowLayout来做到这一点,但是标题占据了屏幕的一半(我不想要那个)。 sorry for a basic question like that but i am new to java :) 对不起,像这样的基本问题,但我是java的新手:)

You could use GridBagLayout , for example... 您可以使用GridBagLayout ,例如...

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 看看如何使用GridBagLayout了解更多详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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