Home
01 Getting Started
02 Concepts
03 Variables & Data Types
04 Control Logic
05 Modifiers
06 GUI
07 Other Useful Classes
08 Handling Events
09 Handling Exceptions
10 Project 01 - Mortg Calc
11 More On Applets
12 Layout Manager
a Absolute Positioning
b Flow Layout
c Border Layout
d Grid Layout
e Grid Bag Layout
f Mixing Layout Managers
13 Project 02 - Calculator
14 Threads
15 Project 03 - Wake Up
16 File I/O
17 Project 04 - File I/O
Expanded Table of Contents
Debugging Hints
HTML Review
Download Samples
|
Previous Next
Mixing Layout Managers
In this example, we mixed together several layouts to achieve our user interface. Its most outer layout is a BorderLayout used by the outermost panel called panOuter. The buttons dog, cat, and pig were placed in 3 of its 5 regions, North, South, and West.
For its East region, a panel called panEast was instantiated. This panel uses a GridLayout of 5 rows and 1 column. PanEast was placed inside the East region of the BorderLayout of panOuter. Inside of panEast was placed the radio buttons for a list of car models.
Of the 5 regions of the BorderLayout, we have left the center. For this region, the panel panCenter was instantiated and given a GridLayout with 3 rows and 2 columns. It was loaded with the buttons lion, tiger, bear, deer, horse, and cow. Then it was place in the Center region of panOuter.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MixLay extends JFrame {
JButton dog = new JButton("Dog");
JButton cat = new JButton("Cat");
JButton bird = new JButton("Bird");
JButton pig = new JButton("Pig");
JButton lion = new JButton("Lion");
JButton tiger = new JButton("Tiger");
JButton bear = new JButton("Bear");
JButton deer = new JButton("Deer");
JButton horse = new JButton("Horse");
JButton cow = new JButton("Cow");
JRadioButton[] cars = new JRadioButton[5];
public MixLay() {
super("Animal/Car Mixed Layout");
setSize(350, 350);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panOuter = new JPanel();
BorderLayout bLay = new BorderLayout();
panOuter.setLayout(bLay);
panOuter.add("North", dog);
panOuter.add("South", cat);
panOuter.add("West", pig);
JPanel panEast = new JPanel();
GridLayout gLay1 = new GridLayout(5, 1);
panEast.setLayout(gLay1);
cars[0] = new JRadioButton("Honda");
cars[1] = new JRadioButton("Mustang");
cars[2] = new JRadioButton("Crossfire");
cars[3] = new JRadioButton("BMW");
cars[4] = new JRadioButton("Mercedes");
ButtonGroup bGroup = new ButtonGroup();
for (int i=0; i<5; i++) {
bGroup.add(cars[i]);
panEast.add(cars[i]);
}
panOuter.add("East", panEast);
JPanel panCenter = new JPanel();
GridLayout gLay2 = new GridLayout(3, 2);
panCenter.setLayout(gLay2);
panCenter.add(lion);
panCenter.add(tiger);
panCenter.add(bear);
panCenter.add(deer);
panCenter.add(horse);
panCenter.add(cow);
panOuter.add("Center", panCenter);
add(panOuter);
setVisible(true);
}
public static void main(String[] arguments) {
MixLay frame = new MixLay();
}
}
Previous Next
|
|