不同组件的布局问题?

Layouts Issue on Different Components?

我正在编写一个 gui 程序,其中使用流布局来设置 components.I 知道此布局从默认中心开始并像这样从左到右

我也知道所有其他布局的结构,但我想制作这样的图形用户界面

该图片中 center.But 布局中使用的所有组件 null.I 只想知道我们如何在这些类型的布局(如边框、流等)中做到这一点。
代码:

     public class Main {

    public static void main(String[] args) {

        JFrame obj = new JFrame();
        obj.setTitle("My Frame");
        obj.setSize(800, 600);
        obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        obj.setLocationRelativeTo(null);

        JLabel l1 = new JLabel("Enter Name");
        l1.setFont(new Font("Tahoma", Font.PLAIN, 21));

        JTextField t1 = new JTextField(20);
        t1.setFont(new Font("Tahoma", Font.PLAIN, 16));

        JButton b1 = new JButton("Submit");

        obj.setLayout(new FlowLayout());
        obj.add(l1);
        obj.add(t1);
        obj.add(b1);

        obj.setVisible(true);

    }

}

您可以使用 BoxLayout。类似于:

box.add(label);
box.add(textField);
box.add(button);
box.add(Box.createVerticalGlue());

您可能需要使用 setAlignmentX(...) 属性 来让组件水平居中。

阅读 How to Use BoxLayout 上的 Swing 教程部分以获取更多信息和示例。

或者您可以使用 GridBagLayout。本教程还有一个关于 How to Use GridBagLayout.

的部分