JPanel没有正确对齐

时间:2011-08-02 11:30:02

标签: java swing gridbaglayout

我试图在一个大面板中对齐2个JPanles。我无法正确对齐它们。我正在提供源代码here的链接。如果您运行源代码,您可以看到新的付款方式单选按钮位于中心,而不仅仅是付款选项面板下方。我怎么在那里得到它。我非常抱歉无法发布屏幕截图以及长代码。非常感谢TON。

1 个答案:

答案 0 :(得分:2)

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

enter image description here

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();
            }
        });
    }
}
相关问题