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

时间:2016-10-03 20:19:44

标签: java swing button alignment layout-manager

我创建了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);
            }

        }
    }

}

我看到这个屏幕: 错误的对齐方式:
wrong alignment

但我想得到这个屏幕:
Fine Alignment

1 个答案:

答案 0 :(得分:4)

建议:

  • 让JPanel按住BoxLayout按钮与BoxLayout.LINE_AXIS对齐
  • 将按钮的首选尺寸设置为相同的值
  • 将按钮的alignmentY设置为JComponent.BOTTOM_ALIGNMENT

例如:

enter image description here

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

        }
    }    
}