package se1021.layoutmanagers; /* * Created on May 1, 2005 * Sample of different layout managers used to create UI's */ import javax.swing.*; import java.awt.*; @SuppressWarnings("serial") // This class is the main class (contains the main method) and also the UI class (as it extends JFrame) public class LayoutManagerSampleApp extends JFrame { public static void main(String[] args) { // Create an instance of ourselves LayoutManagerSampleApp ui = new LayoutManagerSampleApp(); // use default constructor // Initialize some aspects of the JFrame ui.setSize(300,500); ui.setLocation(100, 200 ); ui.setBackground( Color.green ); ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); ui.setResizable(true); // now create the contents // ui.createFlowLayoutUI(); // ui.createBorderLayoutUI(); // ui.createNestedLayoutUI(); // ui.createGridLayoutUI(); // ui.createDoubleNestedJPanelUI(); // the usual stuff needed to actually display the contents of the JFrame ui.setVisible(true); } /** * Simple example of a FlowLayout used within a JFrame * */ private void createFlowLayoutUI() { setTitle("Flow Layout example"); // Get the content pane object within the JFrame Container cp = getContentPane(); // Create a layout manager FlowLayout fl = new FlowLayout(FlowLayout.LEFT); // fl = new FlowLayout(FlowLayout.RIGHT); cp.setLayout(fl); // tells the content pane to use the layout manager cp.setBackground( Color.white ); // create some buttons to be added to the content pane... JButton btnQuit = new JButton("Quit"); btnQuit.setPreferredSize(new Dimension(100,200)); JButton btnHelp = new JButton("Help"); JButton btnAdd = new JButton("Add"); JButton btnSave = new JButton("Save this file to disk"); btnSave.setBounds(100, 100, 100, 100); // ...and add them cp.add( btnQuit ); cp.add( btnHelp ); cp.add( btnAdd ); cp.add( btnSave ); } /** * Simple example of a BorderLayout * */ private void createBorderLayoutUI() { setTitle("Border Layout Example"); Dimension dim = new Dimension(300,300); // a size setMinimumSize(dim); // Get the content pane within the JFrame Container cp = getContentPane(); // Create a layout manager BorderLayout bl = new BorderLayout(10,10); // 10,10=gaps between regions cp.setLayout( bl); // tells the content pane to use the layout manager cp.setBackground( Color.black ); // create some buttons to be added to the content pane... JButton btnQuit = new JButton("Quit"); JButton btnHelp = new JButton("Help"); JButton btnAdd = new JButton("Add"); JButton btnSave = new JButton("Save"); JButton btnDelete = new JButton("Delete"); // ...and add them // cp.add(new JTextField("North"), BorderLayout.NORTH ); cp.add( btnQuit, BorderLayout.NORTH ); cp.add( btnHelp, BorderLayout.SOUTH ); cp.add( btnAdd, BorderLayout.EAST); cp.add( btnSave, BorderLayout.WEST ); // cp.add(new JTextField("Center"), BorderLayout.CENTER ); cp.add(btnDelete, BorderLayout.CENTER ); } /** * Example of nested layout. * Outer BorderLayout is applied directly to the Container; * inner layout is applied to a JPanel within the outer layout * */ private void createNestedLayoutUI() { setTitle("Nested layouts example 1"); // Get the content pane within the JFrame Container cp = getContentPane(); // Create a layout manager BorderLayout bl = new BorderLayout(10,10); // 10,10=gaps between regions cp.setLayout( bl); // tells the content pane to use the layout manager cp.setBackground( Color.white ); // create some buttons to be added to the content pane... JButton btnQuit = new JButton("Quit"); JButton btnHelp = new JButton("Help"); JButton btnAdd = new JButton("Add"); JButton btnSave = new JButton("Save"); // ...and add them // cp.add(new JTextField("North"), BorderLayout.NORTH ); cp.add( btnQuit, BorderLayout.NORTH ); cp.add( btnHelp, BorderLayout.SOUTH ); cp.add( btnAdd, BorderLayout.EAST); cp.add( btnSave, BorderLayout.WEST ); // create a JPanel object that can itself contain components JPanel jp1 = new JPanel(); // associate a layout manager with the JPanel jp1.setLayout( new FlowLayout(FlowLayout.CENTER) ); jp1.setBackground( Color.RED ); // put a title on the JPanel's border jp1.setBorder(BorderFactory.createTitledBorder("inner JPanel")); // jp1.setBorder(BorderFactory.createLoweredBevelBorder()); jp1.add(new JButton("Exit") ); jp1.add(new JButton("Terminate") ); jp1.add(new JButton("Quit the program") ); cp.add(jp1, BorderLayout.CENTER ); // add the panel as the Center member of the outer layout } /** * Example of nested layouts * The BorderLayout is applied to an absolute-positioned JPanel within the Container. * An inner JPanel is placed in a region of the outer panel and uses it's own layout manager. */ private void createDoubleNestedJPanelUI() { setTitle("Nested layouts example 2"); // Get the content pane within the JFrame Container cp = getContentPane(); cp.setBackground( Color.white ); cp.setLayout( null ); // create a JPanel object that can itself contain components JPanel jp1 = new JPanel(); // set the panel's size directly jp1.setBounds(50,50,400,400); // create a layout manager to manage the contents of the panel jp1.setLayout( new BorderLayout(10,10) ); jp1.setBorder(BorderFactory.createTitledBorder("outer JPanel")); jp1.setBackground(Color.CYAN); // add the panel to the content pane cp.add(jp1); // create some buttons to be added to the outer panel... JButton btnQuit = new JButton("Quit"); JButton btnHelp = new JButton("Help"); JButton btnAdd = new JButton("Add"); JButton btnSave = new JButton("Save"); // ...and add them to the outer panel (NOT the content pane) jp1.add( btnQuit, BorderLayout.NORTH ); jp1.add( btnHelp, BorderLayout.SOUTH ); jp1.add( btnAdd, BorderLayout.EAST); jp1.add( btnSave, BorderLayout.WEST ); // create another JPanel object that can itself contain components JPanel jp2 = new JPanel(); // associate a layout manager with this JPanel jp2.setLayout( new FlowLayout(FlowLayout.CENTER) ); // put a title on this JPanel's border jp2.setBorder(BorderFactory.createTitledBorder("inner JPanel")); // jp2.setBorder(BorderFactory.createLoweredBevelBorder()); jp2.add(new JButton("Exit") ); jp2.add(new JButton("Terminate") ); jp2.add(new JButton("Quit the program") ); // add the inner panel to the outer panel jp1.add(jp2, BorderLayout.CENTER ); } /** * Example of nested layouts within an absolutely-positioned JPanel. * The JPanel nests two additional Panels, each containing a different layout. */ private void createGridLayoutUI() { setTitle("Flow Layouts within Grid Example"); // Get the content pane within the JFrame Container cp = getContentPane(); // Create a layout manager BorderLayout bl = new BorderLayout(10,10); // 10,10=gaps between regions cp.setLayout( bl); // tells the content pane to use the layout manager cp.setBackground( Color.black ); cp.setLayout( null ); JPanel absPanel = new JPanel(); // main panel for the frame absPanel.setBounds(10,10,450,450); absPanel.setBorder(BorderFactory.createTitledBorder("main panel")); absPanel.setLayout( new GridLayout(2,3) ); // flow within main panel cp.add(absPanel); JPanel jp1 = new JPanel(); jp1.setLayout( new FlowLayout(FlowLayout.LEFT) ); jp1.setBorder(BorderFactory.createTitledBorder("first Flow layout JPanel ")); jp1.add(new JButton("A") ); jp1.add(new JButton("B") ); jp1.add(new JButton("C") ); JPanel jp2 = new JPanel(); jp2.setLayout( new FlowLayout(FlowLayout.CENTER) ); jp2.setBorder(BorderFactory.createTitledBorder("second Flow layout JPanel ")); jp2.add(new JButton("One") ); jp2.add(new JTextField("Two") ); JLabel label = new JLabel("Three"); label.setHorizontalAlignment(JLabel.CENTER); Font font = label.getFont(); label.setFont(label.getFont().deriveFont(font.PLAIN, 14.0f)); label.setForeground(Color.BLUE); jp2.add(label ); JPanel jp3 = new JPanel(); jp3.setLayout( new FlowLayout(FlowLayout.RIGHT) ); jp3.setBorder(BorderFactory.createTitledBorder("third Flow layout JPanel")); jp3.add(new JButton("G") ); jp3.add(new JButton("H") ); jp3.add(new JButton("I") ); absPanel.add(jp1); absPanel.add(jp2); absPanel.add(jp3); absPanel.add(new JButton("left") ); absPanel.add(new JButton("middle") ); absPanel.add(new JButton("right") ); } }