Java tutorial
import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Main extends JFrame { private JPanel childPanel1 = new JPanel(); private JPanel childPanel2 = new JPanel(); private JPanel childPanel3 = new JPanel(); public Main() { childPanel1.setBackground(Color.red); childPanel1.setPreferredSize(new Dimension(300, 40)); childPanel2.setBackground(Color.blue); childPanel2.setPreferredSize(new Dimension(300, 40)); childPanel3.setBackground(Color.yellow); childPanel3.setPreferredSize(new Dimension(300, 40)); JButton myButton = new JButton("Add Component "); myButton.addActionListener(e -> { add(childPanel2, BorderLayout.CENTER); pack(); }); setLocation(10, 200); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); add(childPanel3, BorderLayout.CENTER); add(myButton, BorderLayout.SOUTH); pack(); setVisible(true); } public static void main(String[] args) { Main Main = new Main(); } }