ptg7068951
Using Components 175
When you add components to a container, you do not specify the place in
the container where the component should be displayed. The arrangement
of components is decided by an object called a layout manager. The simplest
of these managers is the FlowLayoutclass, which is part of the java.awt
package.
To make a container use a specific layout manager, you must first create an
object of that layout manager’s class. You create a FlowLayoutobject with a
statement, such as the following:
FlowLayout flo = new FlowLayout();
After you create a layout manager, you call the container’s setLayout()
method to associate the manager with the container. The only argument to this
method should be the layout manager object, as in the following example:
pane.setLayout(flo);
This statement designates the floobject as the layout manager for the pane
container.
The next application you create, a class called Playback, is a Java applica-
tion that displays a frame with three buttons. Enter the text from Listing
13.2 into a new empty Java file and save the file.
LISTING 13.2 The Full Text of Playback.java
1: importjavax.swing.;
2: importjava.awt.;
3:
4: public classPlayback extendsJFrame {
5: publicPlayback() {
6: super(“Playback”);
7: setLookAndFeel();
8: setSize(225, 80);
9: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
10: FlowLayout flo = new FlowLayout();
11: setLayout(flo);
12: JButton play = new JButton(“Play”);
13: JButton stop = new JButton(“Stop”);
14: JButton pause = new JButton(“Pause”);
15: add(play);
16: add(stop);
17: add(pause);
18: setVisible(true);
19: }
20:
21: private voidsetLookAndFeel() {
22: try {
23: UIManager.setLookAndFeel(