Java Swing布局和菜单

Java Swing布局和菜单,java,swing,layout-manager,Java,Swing,Layout Manager,好的,所以我在使用网格布局以这种格式排列窗口时遇到问题,我想将每个标签及其文本字段排列成一行,然后在底部有一个居中按钮 public class Main extends JFrame { //below is how i want to format the frame /*label textfield label textfield label textfield button*/

好的,所以我在使用网格布局以这种格式排列窗口时遇到问题,我想将每个标签及其文本字段排列成一行,然后在底部有一个居中按钮

  public class Main extends JFrame {

   //below is how i want to format the frame
        /*label textfield
          label textfield   
          label textfield   
              button*/

         JPanel panel = new JPanel();
        JLabel nameLabel = new JLabel("Name");
        JTextField nameText = new JTextField(15);
        JLabel addressLabel = new JLabel("Address");
        JTextField addressText = new JTextField(15);

        public Main(){
            setTitle("JSWING");
            setLayout(new GridLayout(3,2));
            setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
            setVisible(true);
            setSize(450,250);

         //setResizable(false);
            setDefaultCloseOperation(EXIT_ON_CLOSE);

            panel.add(nameLabel);
            panel.add(nameText);
            panel.add(addressLabel);
            panel.add(addressText);


            add(panel);
        }
        public static void main(String[] args){
            Main mainFrame = new Main();
            //mainFrame.setVisible(true);
        }
    }
框架的默认布局管理器是BorderLayout。因此,可以将多个面板添加到框架中。一个面板包含标签/文本字段,另一个面板包含按钮


框架的默认布局管理器是BorderLayout。因此,可以将多个面板添加到框架中。一个面板包含标签/文本字段,另一个面板包含按钮。

绘制所需内容。你也可以使用
GridLayout
来描述你在OP中所说的内容。我在代码中添加了它作为注释,我希望它看起来像什么。在paint中绘制你需要的东西。你也可以使用
GridLayout
来描述你在OP中所说的内容。我在代码中添加了它作为注释,我希望它看起来像什么。
//setVisible(true); // don't do this until all components are added to the frame.
...
add(panel, BorderLayout.CENTER);
JPanel buttonPanel = new JPanel();
JButton button = new JButton("Button");
buttonPanel.add( button );
add(buttonPanel, BorderLayout.PAGE_END);
setVisible(true);