I've deployed JButton using multiple layout managers, but it doesn't display properly : <p>I wanted to place several buttons to check the differences between the different layout managers. But the code I wrote shows only the buttons displayed by one layout manager, and the buttons of the other layout manager are not displayed at all. What should I do?</p>

<p>This is my code:</p>

<pre><code>import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;

import javax.swing.*;

public class No2{
    public No2() {
        JFrame frame=new JFrame("MyFrame");
        JPanel MainPanel=new JPanel();
        JPanel panel1=new JPanel();
        JPanel panel2=new JPanel();
        JPanel panel3=new JPanel();

        JButton[] btn=new JButton[5];
        for (int i = 0; i &lt; btn.length; i++) {
            btn[i]=new JButton("Button"+(i+1));
        }

        frame.add(MainPanel);
        MainPanel.setLayout(new GridLayout(3,1));
        MainPanel.add(panel1);
        MainPanel.add(panel2);
        MainPanel.add(panel3);

        panel1.setLayout(new FlowLayout());
        for (int i = 0; i &lt; btn.length; i++) {
            panel1.add(btn[i]);
        }
        panel1.setLayout(new BorderLayout());
        for (int i = 0; i &lt; btn.length; i++) {
            panel2.add(btn[i]);
        }
        panel1.setLayout(new GridLayout());
        for (int i = 0; i &lt; btn.length; i++) {
            panel3.add(btn[i]);
        }


        frame.pack();
        frame.setVisible(true);
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new No2();
    }
}
</code></pre>
