|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |
TheFlowLayoutclass provides a very simple layout manager that is used, by default, by
JPanels. Here's an applet that shows a flow layout in action:
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.
FlowLayoutputs components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row,FlowLayoutuses multiple rows. Within each row, components are centered (the default), left-aligned, or right-aligned as specified when theFlowLayoutis created.Below is the code that creates the
FlowLayoutand the components it manages. You can find the whole program inFlowWindow.java. The program runs either within an applet (with the help ofAppletButton) or as an application.Container contentPane = getContentPane(); contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("Button 5"));
TheFlowLayoutclass has three constructors:Thepublic FlowLayout() public FlowLayout(int alignment) public FlowLayout(int alignment, int horizontalGap, int verticalGap)alignmentargument must have the valueFlowLayout.LEFT,FlowLayout.CENTER, orFlowLayout.RIGHT. ThehorizontalGapandverticalGaparguments specify the number of pixels to put between components. If you don't specify a gap value,FlowLayoutuses5for the default gap value.
The following table lists some of the examples that use flow layout.
Example Where Described Notes FlowWindowThis page Sets up a content pane to use FlowLayout.ButtonDemoHow to Use Buttons, Check Boxes, and Radio Buttons ![]()
Uses the default FlowLayoutof aJPanel.
|
|
Start of Tutorial > Start of Trail > Start of Lesson | Search |