无法让秋千布局按我的意愿工作。

Having trouble getting the swing layout to work as I want.

我有一个简单的 GUI,我只想让一些文本字段相互堆叠,每个框都是一个 JPanel。我目前正在为 JFrame 和 JPanel 使用 FlowLayout,但是当我最大化时它从堆叠变为连续。理想情况下,我希望文本字段保持在 window 的中心,即使它已最大化。我正在考虑使用 BoxLayout,但在让它工作时遇到了一些麻烦。

public static Component textbox(String x){
        JPanel panel = new JPanel(new FlowLayout(5,5,5));
        JLabel lbltAm= new JLabel(x);
        JTextField tftAm = new JTextField(10);
        lbltAm.setFont(new Font("Serif", Font.PLAIN, 14));
        lbltAm.setForeground(Color.white);
        panel.add(lbltAm, BorderLayout.NORTH);
        panel.add(tftAm, BorderLayout.CENTER);
        panel.setBackground(Color.DARK_GRAY);

        Border lowerbevel = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
        panel.setBorder(lowerbevel);

        return panel;
}

private static void createAndShowGUI() {

    //Create and set up the window.


    JFrame frame = new JFrame("Tip Calculator");
    //Add Textbox
    frame.setLayout(new BoxLayout(frame, BoxLayout.Y_AXIS));
    String Label = "Tip Calculator";
    JLabel header = new JLabel(Label);
    header.setFont(new Font("Serif", Font.BOLD, 18));
    frame.add(header);

    frame.add(textbox("Total"));
    frame.add(textbox("Tip %"));
    frame.add(textbox("People"));

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setBackground(Color.gray);
    frame.setPreferredSize(new Dimension(300, 400));
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
}

尝试在 createAndShowGUI 中使用 GridLayout 而不是 BoxLayout。

frame.setLayout(new GridLayout(4,1));

首先,我不知道您为什么在将面板布局设置为 FlowLayout 后使用用于 BorderLayout 的方法向 JPanel 添加组件。

如果您想始终保持组件居中(即使屏幕最大化),FlowLayout 可能不是您使用的最佳选择,因为 FlowLayout 会继续水平放置组件,直到它们填满宽度帧(一行),然后,它开始下一行。因此,如果屏幕尺寸最大化,每行将有更多的组件,它们不会堆叠。我建议像这样使用带有一列的 GridLayout:

frame.setLayout(new GridLayout(3 *(number of rows/components)*, 1));

您可以对 JPanel 执行相同的操作。之后,将每个组件的水平对齐设置为居中,如下所示:

component.setHorizontalAlignment(componentName.CENTER);

希望对您有所帮助!

见仁见智,但为了灵活性,我更喜欢用GridBagLayout

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;

public class Test {

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

    public Test() {
        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;
            add(textbox("Total"), gbc);
            add(textbox("Tip %"), gbc);
            add(textbox("People"), gbc);
        }

        public Component textbox(String x) {
            JPanel panel = new JPanel(new GridBagLayout());
            GridBagConstraints gbc = new GridBagConstraints();
            gbc.gridwidth = GridBagConstraints.REMAINDER;
            gbc.insets = new Insets(5, 5, 5, 5);

            JLabel lbltAm = new JLabel(x);
            JTextField tftAm = new JTextField(10);
            lbltAm.setFont(new Font("Serif", Font.PLAIN, 14));
            lbltAm.setForeground(Color.white);

            panel.add(lbltAm, gbc);
            panel.add(tftAm, gbc);
            panel.setBackground(Color.DARK_GRAY);

            Border lowerbevel = BorderFactory.createBevelBorder(BevelBorder.LOWERED);
            panel.setBorder(lowerbevel);

            return panel;
        }
    }

}

或者如果您希望 "boxes" 在扩展时占据整个区域,您可以使用诸如...

    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridwidth = GridBagConstraints.REMAINDER;
    gbc.weightx = 1;
    gbc.weighty = 1;
    gbc.fill = GridBagConstraints.BOTH;
    add(textbox("Total"), gbc);
    add(textbox("Tip %"), gbc);
    add(textbox("People"), gbc);

结果类似于...

查看Laying Out Components Within a Container and How to Use GridBagLayout了解更多详情