|
|
Laying Out Components within a Container |
Here's an applet that shows aFlowLayoutin action.
Your browser can't run 1.0 Java applets, so here's a picture of the window the program brings up:
Note: Because the preceding applet runs using Java Plug-in 1.1.1, it is a Swing 1.0.3 version of the applet. To run the Swing 1.1 Beta 3 version of the applet, you can use the JDK Applet Viewer to viewFlow.html, specifyingswing.jarin the Applet Viewer's class path. For more information about running applets, refer to About Our Examples.
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. (Here's the whole program. The program runs either within an applet, with the help ofAppletButton, or as an application.)TheJPanel contentPane = new JPanel(); contentPane.setLayout(new FlowLayout()); contentpane.setFont(new Font("Helvetica", Font.PLAIN, 14)); 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")); setContentPane(contentPane);FlowLayoutclass 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.Examples that Use FlowLayout
[PENDING]
|
|
Laying Out Components within a Container |