 |
|

|
 |

11/24/09 - Layout Managers
|
 |
 |
 |
  | In previous programs with GUIs we placed components in specific positions using a "null" layout. Java has several layout managers to simplify positioning of various components, and by using panels and combining different layout managers, we can give our GUIs a multitude of different appearances.
|
 |
 |
 |
 |
  | Flow Layout - like wordwrap with centering
|
 |
 |
 |
 |
  | /** * Demo of FlowLayout manager * * @author Chuck Iverson * @version 11/24/09 */
import java.awt.*; import javax.swing.*;
public class FlowLayoutWindow { private JFrame window; private final int WINDOW_WIDTH = 600; private final int WINDOW_HEIGHT = 100;
public static void main(String[] args) { FlowLayoutWindow flw = new FlowLayoutWindow(); }
public FlowLayoutWindow() { window = new JFrame("Flow Layout"); // setup window window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); buildGUI(); // add remaining components window.setVisible(true); // make window visible } public void buildGUI() { JPanel panGUI = new JPanel(); // create container for buttons panGUI.setLayout(new FlowLayout()); // set layout manager to flow JButton btn1 = new JButton("Button 1"); // create 3 buttons JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); panGUI.add(btn1); // add buttons to panel panGUI.add(btn2); panGUI.add(btn3); window.add(panGUI); // add panel to window } }
|
 |
 |
 |
 |
  | Grid Layout - rows and columns with equal-size cells
|
 |
 |
 |
 |
  | /** * Demo of GridLayout * * @author Chuck Iverson * @version 11/24/09 */
import java.awt.*; import javax.swing.*;
public class GridPanelWindow { private JFrame window; private final int WINDOW_WIDTH = 600; private final int WINDOW_HEIGHT = 300; public static void main(String[] args) { GridPanelWindow gpw = new GridPanelWindow(); }
public GridPanelWindow() { window = new JFrame("Grid Layout"); // setup window window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildGUI(); // add remaining components
window.setVisible(true); // make window visible } public void buildGUI() { window.setLayout(new GridLayout(2, 3)); // set grid layout: 2 rows, 3 columns
JButton btn1 = new JButton("Button 1"); // create 6 buttons JButton btn2 = new JButton("Button 2"); JButton btn3 = new JButton("Button 3"); JButton btn4 = new JButton("Button 4"); JButton btn5 = new JButton("Button 5"); JButton btn6 = new JButton("Button 6");
JLabel lbl1 = new JLabel("This is cell 1."); // create 6 labels JLabel lbl2 = new JLabel("This is cell 2."); JLabel lbl3 = new JLabel("This is cell 3."); JLabel lbl4 = new JLabel("This is cell 4."); JLabel lbl5 = new JLabel("This is cell 5."); JLabel lbl6 = new JLabel("This is cell 6.");
JPanel pan1 = new JPanel(); // create 6 panels JPanel pan2 = new JPanel(); JPanel pan3 = new JPanel(); JPanel pan4 = new JPanel(); JPanel pan5 = new JPanel(); JPanel pan6 = new JPanel();
pan1.setBackground(Color.red); // add color to panels pan2.setBackground(Color.cyan); pan3.setBackground(Color.blue); pan4.setBackground(Color.yellow); pan5.setBackground(Color.green); pan6.setBackground(Color.magenta);
pan1.add(lbl1); // Add the labels to the panels pan2.add(lbl2); // panels have default FlowLayout pan3.add(lbl3); pan4.add(lbl4); pan5.add(lbl5); pan6.add(lbl6);
pan1.add(btn1); // Add the buttons to the panels pan2.add(btn2); pan3.add(btn3); pan4.add(btn4); pan5.add(btn5); pan6.add(btn6); // Add panels to window window.add(pan1); // Goes into row 1, column 1 window.add(pan2); // Goes into row 1, column 2 window.add(pan3); // Goes into row 1, column 3 window.add(pan4); // Goes into row 2, column 1 window.add(pan5); // Goes into row 2, column 2 window.add(pan6); // Goes into row 2, column 3 } }
|
 |
 |
 |
 |
  | Border Layout - north, south, east, west and center
|
 |
 |
 |
 |
  | /** * Demo of BorderLayout manager * * @author Chuck Iverson * @version 11/24/09 */
import java.awt.*; import javax.swing.*;
public class BorderLayoutWindow { private JFrame window; private final int WINDOW_WIDTH = 800; private final int WINDOW_HEIGHT = 400;
public static void main(String[] args) { BorderLayoutWindow blw = new BorderLayoutWindow(); }
public BorderLayoutWindow() { window = new JFrame("Border Layout"); // setup window window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
buildGUI(); // add remaining components
window.setVisible(true); // make window visible } public void buildGUI() { window.setLayout(new BorderLayout()); // set layout to Border
JPanel pan1 = new JPanel(); // create 5 panels JPanel pan2 = new JPanel(); JPanel pan3 = new JPanel(); JPanel pan4 = new JPanel(); JPanel pan5 = new JPanel();
pan1.setBackground(Color.red); // add color to panels pan2.setBackground(Color.green); pan3.setBackground(Color.yellow); pan4.setBackground(Color.blue); pan5.setBackground(Color.cyan);
JButton btn1 = new JButton("North Button"); // create 5 buttons JButton btn2 = new JButton("South Button"); JButton btn3 = new JButton("East Button"); JButton btn4 = new JButton("West Button"); JButton btn5 = new JButton("Center Button");
pan1.add(btn1); // Add buttons to panels pan2.add(btn2); pan3.add(btn3); pan4.add(btn4); pan5.add(btn5);
window.add(pan1, BorderLayout.NORTH); // Add the panels to window window.add(pan2, BorderLayout.SOUTH); window.add(pan3, BorderLayout.EAST); window.add(pan4, BorderLayout.WEST); window.add(pan5, BorderLayout.CENTER); } }
|
 |
 |
 |
 |
  | Read pp. 740-748 of the text.
|
 |
 |
 |
 |
  | Use some combination of panels and the three layout managers described above to write programs to create the following GUIs (which don't need to actually work).
|
 |
 |
|


 |
 |
 |