Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other.

Similar presentations


Presentation on theme: "Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other."— Presentation transcript:

1 Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other layout managers to build good GUI applications. Introduction FlowLayout Manager GridLayout Manager Self-check Exercise 1 BorderLayout Manager Self-check Exercise 2 Exercises

2 Unit 132 Introduction to GUI Layout Managers A layout manager determines the placement of components with a container. Each container has a default layout manager. A new layout manager can be installed using the setLayout method. Each layout manager implements one of the two interfaces: LayoutManger or LayoutManger2. Here are some common standard layout managers of Java: oFlowLayout oGridLayout oBorderLayout oGridBagLayout

3 Unit 133 Introduction to Flow Layout FlowLayout places components sequentially from left to right in the order added. Components placement depends on the current size of the container. When the container is resized the components are automatically resized. FlowLayout is the default layout for panels. FlowLayout has three constructors: oFlowLayout() oFlowLayout(int align) oFlowLayout(int align, int hgap, int vgap)

4 Unit 134 Example 1: Flow Layout Test 1 import javax.swing.*; 2 class TestFlowLayout extends JFrame{ 3 JPanel panel = new JPanel(); 4 public TestFlowLayout(){ 5 panel.add(new JButton("1")); 6 panel.add(new JButton("2")); 7 panel.add(new JButton("3")); 8 panel.add(new JButton("4")); 9 panel.add(new JButton("5")); 10 panel.add(new JButton("6")); 11 panel.add(new JButton("7")); 12 setContentPane(panel); 13 setSize(300,300); 14 setTitle("Flow Layout Test"); 15 setVisible(true); 16 } 17 public static void main(String [] args){ 18 new TestFlowLayout(); 19 } 20 }

5 Unit 135 Introduction to Grid Layout GridLayout places components in a rectangular grid. GridLayout is good for laying out containers that look like grids. GridLayout forces occupation of all available container space. GridLayout has three constructors: oGridLayout() oGridLayout(int rows, int cols) oGridLayout(int rows, int cols, int hgap, int vgap)

6 Unit 136 Example 2: Grid Layout Test 1 import java.awt.*; import javax.swing.*; 2 import javax.swing.border.*; 3 class TestGridLayout extends TestFlowLayout{ 4 public TestGridLayout(){ 5 panel.add(new JButton("8")); 6 panel.add(new JButton("9")); 7 panel.add(new JButton("*")); 8 panel.add(new JButton("0")); 9 panel.add(new JButton("#")); 10 JLabel jlb = new JLabel("03-860-4698", SwingConstants.CENTER); 11 Border b =BorderFactory.createBevelBorder(BevelBorder.RAISED); 12 jlb.setBorder(BorderFactory.createTitledBorder(b,"Telephone")); 13 panel.add(jlb); 14 setTitle("Grid Layout Test"); 15 panel.setLayout(new GridLayout(0,3)); 16 setVisible(true); 17 } 18 public static void main(String [] args){ 19 new TestGridLayout(); 20 } 21 }

7 Unit 137 Introduction to Border Layout BorderLayout places components according to five areas: "North", "South", "East", "West" and "Center". When you enlarge a container the center area grabs as much of the new space as possible. BorderLayout is good for maintaining a row of buttons in one of the areas near the edgees. BorderLayout has two constructors: oBorderLayout() oBorderLayout(int hgap, int vgap)

8 Unit 138 Example 3: Border Layout Test 1 import java.awt.*; import javax.swing.*; 2 public class TestBorderLayout extends TestGridLayout{ 3 public TestBorderLayout() { 4 setTitle("Border Layout Test."); 5 JPanel jp1 = (JPanel)getContentPane(); 6 JPanel jp2 = new JPanel(); 7 jp2.setLayout(new BorderLayout()); 8 9 jp2.add(new JButton("NORTH"), "North"); 10 jp2.add(new JButton("WEST"), "West"); 11 jp2.add(new JButton("EAST"), "East"); 12 jp2.add(new JButton("SOUTH"), "South"); 13 14 jp2.add(jp1); 15 setContentPane(jp2); 16 setVisible(true); 17 } 18 public static void main(String args [] ) { 19 new TestBorderLayout(); 20 } 21 }

9 Unit 139 Exercises 1.Move the call to the setVisible() method from the main() method to the TetFlowLayout constructor in Example 1. Then compile and run each of Examples 2, 3 and 4. You will notice some changes when displaying the frames. Explain. 2. Modify Example 2 to add two other types of borders to it. 3.Compare Example 2 and Example 3. In Example 2 we inherited a panel, added more components to it and then changed the panel’ÿ layout manager. In Example 3 we had to create an additional panel. Can Example 3 be written in similar manner to Example 2 without the additional panel? If it is possible, write the equivalent program otherwise explain why it is not possible. 4.Change the fill constraint variable on Line 13 from BOTH to each of the following: NONE, HORIZONTAL and VERTICAL. Run the program in each case and study the effect. 5.Change the value of the gridy constraint variable on Lines 29 to a smaller value. Run the program and observe the result. Do the same for the value on Line 32. Also change these values to bigger values and observe the results.


Download ppt "Unit 131 GUI Layout Managers Learning Outcomes oList and distinguish between the four most common, standard layout managers in Java. oUse these and other."

Similar presentations


Ads by Google