Java Reference
In-Depth Information
LISTING 7.18
//********************************************************************
// LayoutDemo.java Author: Lewis/Loftus
//
// Demonstrates the use of flow, border, grid, and box layouts.
//********************************************************************
import
javax.swing.*;
public class
LayoutDemo
{
//-----------------------------------------------------------------
// Sets up a frame containing a tabbed pane. The panel on each
// tab demonstrates a different layout manager.
//-----------------------------------------------------------------
public static void
main (String[] args)
{
JFrame frame =
new
JFrame ("Layout Manager Demo");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
JTabbedPane tp =
new
JTabbedPane();
tp.addTab ("Intro",
new
IntroPanel());
tp.addTab ("Flow",
new
FlowPanel());
tp.addTab ("Border",
new
BorderPanel());
tp.addTab ("Grid",
new
GridPanel());
tp.addTab ("Box",
new
BoxPanel());
frame.getContentPane().add(tp);
frame.pack();
frame.setVisible(
true
);
}
}
Flow layout
is one of the easiest layout managers to use. The
JPanel
class uses
flow layout by default. Flow layout puts as many components as possible on a
row, at their preferred size. When a component cannot fit on a row, it is put on
the next row. As many rows as needed are added to fit all components that have
been added to the container. Figure 7.7 depicts a container governed by a flow
layout manager.
The class in Listing 7.20 represents the panel that demonstrates the flow lay-
out in the
LayoutDemo
program. It explicitly sets the layout to be a flow layout
Search WWH ::
Custom Search