import java.awt.*; import javax.swing.*; /** Various examples of layouts in the Swing library, useful for * positioning and arranging components in a graphical user * interface. * For more information about layout in Swing, see: * http://java.sun.com/docs/books/tutorial/uiswing/layout/index.html * * */ class LayoutExample { // Flow layout arranges components in rows, creating a new row // when there is no more room in the current row. // Does not resize any of the components. public static void flowLayout() { JButton b1, b2, b3, b4, b5; b1 = new JButton("OneReallyLongButtonName"); b2 = new JButton("Two"); b3 = new JButton("Three"); b4 = new JButton("Four"); b5 = new JButton("Five"); JFrame frame = new JFrame("Flow Layout"); frame.setSize(200,200); Container panel = frame.getContentPane(); panel.setLayout(new FlowLayout()); panel.add(b1); panel.add(b2); panel.add(b3); panel.add(b4); panel.add(b5); frame.setVisible(true); } // Border Layout divides the screen in to 5 regions // When components are added, they must specify the region to which they // belong. // // Components in the NORTH and SOUTH regions are stretched // horizontally so that they fill the panel. // Components in the WEST and EAST region are stretched vertically // to meet the NORTH and SOUTH components. // The CENTER component is stretched to fill the remaining space. public static void borderLayout() { JButton b1, b2, b3, b4, b5; b1 = new JButton("One"); b2 = new JButton("Two"); b3 = new JButton("Three"); b4 = new JButton("Four"); b5 = new JButton("Five"); JFrame frame = new JFrame("Border Layout"); frame.setSize(300,200); Container panel = frame.getContentPane(); // panel.setLayout(new BorderLayout()); // BorderLayout is default for content pane panel.add(b1, BorderLayout.NORTH); //panel.add(b2, BorderLayout.WEST); panel.add(b3, BorderLayout.NORTH); panel.add(b5, BorderLayout.SOUTH); b3.setVisible(false); frame.setVisible(true); } // Grid layout // // Divides the container into a grid of cells, // the constructor must specify the // number of rows and columns. // Each cell contains one component. Components are sized to fill entire cell. public static void gridLayout() { JButton b1, b2, b3, b4, b5; b1 = new JButton("One"); b2 = new JButton("Two"); b3 = new JButton("Three"); b4 = new JButton("Four"); b5 = new JButton("Five"); JFrame frame = new JFrame("Grid Layout"); frame.setSize(200,300); Container panel = frame.getContentPane(); panel.setLayout(new GridLayout(3,2)); // three rows, two columns panel.add(b1); // row 1, column 1 panel.add(b2); // 1,2 panel.add(b3); // 2,1 panel.add(b4); // 2,2 panel.add(b5); // 3,1 frame.setVisible(true); } // Can nest components inside panels. public static void nestedLayout() { JButton b1, b2, b3, b4, b5; b1 = new JButton("One"); b2 = new JButton("Two"); b3 = new JButton("Three"); b4 = new JButton("Four"); b5 = new JButton("Five"); JFrame frame = new JFrame("Nested Panels"); frame.setSize(300,200); JPanel center_panel = new JPanel(); // nothing in this panel (for now!) JPanel east_panel = new JPanel(); east_panel.setLayout(new GridLayout(5,1)); JPanel subpanel = new JPanel(); subpanel.setLayout(new BorderLayout()); east_panel.add(subpanel); subpanel.add(b1, BorderLayout.CENTER); east_panel.add(b2); east_panel.add(b3); east_panel.add(b4); east_panel.add(b5); Container panel = frame.getContentPane(); panel.setLayout(new BorderLayout()); panel.add(center_panel, BorderLayout.CENTER); panel.add(east_panel, BorderLayout.EAST); frame.setVisible(true); } }