如何从右边开始向 JPanel 添加按钮?

How to add buttons to JPanel starting from the right?

如何将按钮和其他组件添加到 JPanel 从右到左?我已经使用 BorderLayout 管理器来做到这一点,但没有用,它们被插入到屏幕中间!

我该怎么做?

添加右对齐的额外 JPanelFlowLayout

JPanel panel = new JPanel(new FlowLayout(FlowLayout.RIGHT), 0, 5); // 0 for horizontal gap

澄清问题很重要,因为向右对齐的内容与按从右到左的顺序添加的内容之间存在差异...

右对齐...

您可以通过多种方式将组件右对齐,您可以使用 GridBagLayout,但最简单的可能是使用 FlowLayout

import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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 FlowLayout(FlowLayout.RIGHT));
            add(new JLabel("Aligned"));
            add(new JLabel("to"));
            add(new JLabel("the"));
            add(new JLabel("right"));
        }

    }

}

从右到左添加...

这只是使用 Component#setComponentOrientation 并将其设置为 ComponentOrientation.RIGHT_TO_LEFT 以更改组件布局的方向

import java.awt.ComponentOrientation;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

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() {
            setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
            setLayout(new FlowLayout(FlowLayout.RIGHT));
            add(new JLabel("Starting"));
            add(new JLabel("from"));
            add(new JLabel("the"));
            add(new JLabel("right"));
        }

    }

}