ptg7068951
188 HOUR 14: Laying Out a User Interface
Under FlowLayout, components are dropped onto an area in the same way
words are organized on a page in English—from left to right, and then
down to the next line when there’s no more space.
The following example could be used in a frame so that it employs flow
layout when components are added:
FlowLayout topLayout = new FlowLayout();
setLayout(topLayout);
You also can set up a layout manager to work within a specific container,
such as a JPanelobject. You can do this by using the setLayout()method
of that container object.
The Crisisapplication has a GUI with five buttons. Create a new empty
Java file for a class named Crisis. Enter text from Listing 14.1 into the file
and save the file.
LISTING 14.1 The Full Text of Crisis.java
1: importjava.awt.*;
2: importjavax.swing.*;
3:
4: public classCrisis extendsJFrame {
5: JButton panicButton;
6: JButton dontPanicButton;
7: JButton blameButton;
8: JButton mediaButton;
9: JButton saveButton;
10:
11: publicCrisis() {
12: super(“Crisis”);
13: setLookAndFeel();
14: setSize(348, 128);
15: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
16: FlowLayout flo = new FlowLayout();
17: setLayout(flo);
18: panicButton= new JButton(“Panic”);
19: dontPanicButton= new JButton(“Don’t Panic”);
20: blameButton= new JButton(“Blame Others”);
21: mediaButton= new JButton(“Notify the Media”);
22: saveButton= newJButton(“Save Yourself”);
23: add(panicButton);
24: add(dontPanicButton);
25: add(blameButton);
26: add(mediaButton);
27: add(saveButton);
28: setVisible(true);
29: }
30: