Java Reference
In-Depth Information
(a)
(b)
Figure 6.4
(a) The program
InputsPanelApplet
arranges two
JLabel
and two
JTextField
components with a
GridLayout
. (b) This program
MultiPanelApplet
combines the two button panel shown in Figure 6.3 (buttons are side by side due to
the extra horizontal space available) with the panel from
InputsPanelApplet
and a
JTextArea
to display the results of the operations.
FlowLayout
- the default layout manager for
JPanel
. Components are placed in the
order they are added, starting horizontally until all the space is filled and then shifting
down to the next line, starting again at the left. The exact arrangement depends on
the amount of area available and the minimum and maximum sizes allowed for each
component.
GridLayout
- the components are distributed on a uniform grid or table. The size of
the grid is set by the number of rows and columns passed in the constructor. As they are
added, components are placed horizontally left to right until the row cells are filled and
then continue with the next row down.
The following applet shows how to arrange four components using a
GridLay-
out
with two rows and two columns (see Figure 6.4(a)). It also shows how to
initialize the text in a
JTextField
object and a
JLabel
.
import java.awt.*;
import javax.swing.*;
/** Demonstrate the display of a JPanel subclass. **/
public class
InputsPanelApplet
extends JApplet
{
/** Create the interface in the init method **/
public void init ()
{
Container content
-
pane
=
getContentPane ();
// Next create a panel of input fields and labels
InputsPanel inputs
-
panel =
new InputsPanel ("Input x", "1.5",
"Input y", "3.14");
content
-
pane.add (inputs
-
panel);
}
}// InputsPanelApplet
Search WWH ::
Custom Search