1

我正在尝试在一个大面板中对齐 2 个 Jpanles。我无法正确对齐它们。我在这里给出了源代码的链接。如果您运行源代码,您会看到“新付款方式”单选按钮位于中心,而不是位于付款选项面板下方。我怎么弄到那里。对于无法发布屏幕截图以及长代码,我感到非常抱歉。提前致谢。

4

1 回答 1

2

作为替代方案,请考虑BoxLayout,如下所示。

在此处输入图像描述

import java.awt.EventQueue;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;

/** @see http://stackoverflow.com/questions/6911309 */
public class PaymentPanel extends Box {

    public PaymentPanel() {
        super(BoxLayout.Y_AXIS);
        this.add(new JLabel("Payment Setup"));
        this.add(Box.createVerticalStrut(10));
        this.add(new JRadioButton("New payment Method", true));
        this.add(Box.createVerticalStrut(10));
        this.add(new JLabel("Invoice"));
    }

    private void display() {
        JFrame f = new JFrame("PaymentPanel");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(this);
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new PaymentPanel().display();
            }
        });
    }
}
于 2011-08-02T13:38:19.730 回答