如何对齐面板底部不同尺寸的按钮?

How can i align buttons with different sizes at bottom of panel?

我创建了 5 个不同大小的按钮,我尝试使用 setAlignmentY() 方法将对齐方式设置为底部,但它不起作用。

我的代码:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public final class Tester extends JFrame {

    public Tester() {
        setTitle("Tester window");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);

        createMainPanel();

        setVisible(true);
    }

    public static void main(String args[]) {
        Runnable runnable = new Runnable() {
            public void run() {
                new Tester();
            }
        };
        EventQueue.invokeLater(runnable);
    }

    private void createMainPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout());

        mainPanel.add(new CenterPanel(), BorderLayout.SOUTH);
        add(mainPanel);
    }

    private class CenterPanel extends JPanel {
        public CenterPanel() {
            Random r = new Random();
            for (int i = 0; i < 5; ++i) {
                JButton button = new JButton(i + " button");
                button.setPreferredSize(new Dimension(100, r.nextInt(500)+20));
                // button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
                add(button);
            }

        }
    }

}

我得到这个屏幕: 对齐错误:

但是我想要这个屏幕:

建议:

  • 为包含按钮的 JPanel 提供一个与 BoxLayout.LINE_AXIS
  • 对齐的 BoxLayout
  • 将按钮的首选尺寸其最大尺寸设置为相同的值
  • 设置按钮的alignmentY为JComponent.BOTTOM_ALIGNMENT

例如:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.util.Random;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public final class Tester extends JFrame {

    public Tester() {
        setTitle("Tester window");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(800, 600);

        createMainPanel();

        setVisible(true);
    }

    public static void main(String args[]) {
        Runnable runnable = new Runnable() {
            public void run() {
                new Tester();
            }
        };
        EventQueue.invokeLater(runnable);
    }

    private void createMainPanel() {
        JPanel mainPanel = new JPanel(new BorderLayout());

        mainPanel.add(new CenterPanel(), BorderLayout.SOUTH);
        add(mainPanel);
    }

    private class CenterPanel extends JPanel {
        public CenterPanel() {
            // inner panel created to center the buttons in the center panel
            JPanel innerPanel = new JPanel(); // uss flow layout to center buttons
            innerPanel.setLayout(new BoxLayout(innerPanel, BoxLayout.LINE_AXIS));
            add(innerPanel);

            Random r = new Random();
            for (int i = 0; i < 5; ++i) {
                JButton button = new JButton(i + " button");
                button.setAlignmentY(JComponent.BOTTOM_ALIGNMENT);
                int width = 100;
                int height = r.nextInt(500)+20;
                Dimension d = new Dimension(width, height);

                button.setPreferredSize(d);
                button.setMaximumSize(d);
                // button.setMinimumSize(d);
                // button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
                innerPanel.add(button);
            }

        }
    }    
}