如何为边框布局添加容器?

How is it possible to add a container for border layout?

我想在边框布局的上方和下方添加标签和按钮。我怎样才能做到这一点?这是我所做的:

import java.awt.*;

import javax.swing.*;

class homework{
    public static void main(String[] args) {
        JFrame frame= new JFrame("border layout");
        frame.setVisible(true);
        JLabel label=new JLabel("Container of BorderLayout");
        JButton button1 = new JButton("NORTH");
        JButton button2 = new JButton("SOUTH");
        JButton button3 = new JButton("EAST");
        JButton button5 = new JButton("CENTER");
        JButton button4 = new JButton("WEST");
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();

        frame.add(panel2);
        label.setLayout(new FlowLayout(0));
        panel2.add(label);
        panel1.setLayout(new BorderLayout());
        panel1.add(button1,BorderLayout.NORTH);
        panel1.add(button2,BorderLayout.SOUTH);
        panel1.add(button3,BorderLayout.EAST);
        panel1.add(button4,BorderLayout.WEST);
        panel1.add(button5,BorderLayout.CENTER);
        frame.add(panel1);

        frame.pack();

    }
}

在border布局的上方和下方,设置新的2个container(例如JPanel),使之成为flow layout。 enter image description here

    JPanel panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.NORTH);

    JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
    panel.add(rdbtnNewRadioButton);

    JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
    panel.add(rdbtnNewRadioButton_1);

    JPanel panel_1 = new JPanel();
    frame.getContentPane().add(panel_1, BorderLayout.SOUTH);

    JLabel lblNewLabel = new JLabel("New label");
    panel_1.add(lblNewLabel);

    JLabel lblNewLabel_1 = new JLabel("New label");
    panel_1.add(lblNewLabel_1);

在 frame.pack() 之前添加类似的内容;代码。

其实上面和下面是什么意思?你是指北方还是南方?如果是,你应该有这样的东西 enter image description here 你应该这样写代码

 import java.awt.*;

   class homework{
    public static void main(String[] args) {
        JFrame frame= new JFrame("border layout");
        frame.setVisible(true);
        JLabel label=new JLabel("Container of BorderLayout");
        JButton button3 = new JButton("EAST");
        JButton button5 = new JButton("CENTER");
        JButton button4 = new JButton("WEST");
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();

        frame.getContentPane().add(panel2);
        label.setLayout(new FlowLayout(0));
        panel2.add(label);
        panel1.setLayout(new BorderLayout());
        panel1.add(button3,BorderLayout.EAST);
        panel1.add(button4,BorderLayout.WEST);
        panel1.add(button5,BorderLayout.CENTER);
        frame.getContentPane().add(panel1);

        JPanel panel = new JPanel();
        panel1.add(panel, BorderLayout.NORTH);

        JLabel lblNewLabel = new JLabel("New label");
        panel.add(lblNewLabel);

        JRadioButton rdbtnNewRadioButton = new JRadioButton("New radio button");
        panel.add(rdbtnNewRadioButton);

        JPanel panel_1 = new JPanel();
        panel1.add(panel_1, BorderLayout.SOUTH);

        JLabel lblNewLabel_1 = new JLabel("New label");
        panel_1.add(lblNewLabel_1);

        JRadioButton rdbtnNewRadioButton_1 = new JRadioButton("New radio button");
        panel_1.add(rdbtnNewRadioButton_1);

        frame.pack();

    }
 }

在两种情况下,您应该在代码中添加两个容器并使它们流式布局。