/* NestedJPanels.java CIS 160 David Klick 2011-09-24 Demonstration of nested JPanels */ import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingUtilities; public class NestedJPanels extends JFrame { public static void main(String[] args) { new NestedJPanels(); } public NestedJPanels() { SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } private void createAndShowGUI() { // create all the containers we will need JPanel top = new JPanel(); JPanel topWest = new JPanel(); JPanel topCenter = new JPanel(); JPanel topEast = new JPanel(); JPanel bottom = new JPanel(); Container c = getContentPane(); // set the layout managers on the containers c.setLayout(new BorderLayout()); top.setLayout(new BorderLayout()); topWest.setLayout(new GridLayout(3,1)); topCenter.setLayout(new FlowLayout()); topEast.setLayout(new GridLayout(3,1)); bottom.setLayout(new FlowLayout()); // create and add widgets to the containers topWest.add(new JButton("topWest 1")); topWest.add(new JButton("topWest 2")); topWest.add(new JButton("topWest 3")); topEast.add(new JButton("topEast 1")); topEast.add(new JButton("topEast 2")); topEast.add(new JButton("topEast 3")); topCenter.add(new JLabel("topCenter")); bottom.add(new JLabel("South on c")); // nest 3 panels inside top panel top.add(topWest, BorderLayout.WEST); top.add(topCenter, BorderLayout.CENTER); top.add(topEast, BorderLayout.EAST); // add two main containers to content pane c.add(top, BorderLayout.CENTER); c.add(bottom, BorderLayout.SOUTH); // set up window properties and make it appear setDefaultCloseOperation(EXIT_ON_CLOSE); setSize(400, 300); setVisible(true); } }