Java Reference
In-Depth Information
since that component may have a fixed size or a limited range of sizes. The user
can override the default size settings with the methods setMinimumSize() ,
setMaximumSize() , and setPreferredSize(Dimension) methods.
The layout manager calls the corresponding getter method to find out the mini-
mum, maximum, and preferred dimensions for a component. A label or button,
for example, wants enough room to display its text or icon. If there is too little
room and the manager cannot satisfy the program's instructions, then it will do
the best it can, according to its own rules. In some cases this will mean that one
or more components will be partially seen or left out of the interface altogether.
There are several layout manager classes to choose from and we will give
here a general description of how they work. (We discussed GridLayout in
Chapter 6.) For more details, see the specifications in the Java 2 API, the Web
Course, and the Sun tutorials [2].
Figure 7.8 Arrangement
of components with
FlowLayout .
Note that you typically build complex interfaces with multiple panels, each
with its own layout manager that best suits the particular subgroup of components.
The FlowLayout is the default for JPanel but you can specify one of the other
layout managers in its constructor or with the setLayout() method.
Some layout managers have been available since Java 1.0 and appear in the
java.awt package. The newer layout managers appear in the javax.swing
package.
7.4.1 FlowLayout
The following applet code demonstrates the coding for the FlowLayout . The
program produces the arrangement shown in Figure 7.8.
import javax.swing.*;
import java.awt.*;
/** Demo of the FlowLayout manager. **/
public class FlowApplet extends JApplet
{
// Add an instance of FlowPanel to the applet.
public void init () {
Container content - pane = getContentPane ();
// Create an instance of our FlowPanel class.
FlowPanel flow - panel = new FlowPanel ();
// And add it to the applet's panel.
content - pane.add (flow - panel);
} // init
} // class FlowApplet
Search WWH ::




Custom Search