Using FlowLayout : FlowLayout « Swing « Java Tutorial






Using FlowLayout
import java.awt.Dimension;
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class FlowLayoutTest {
  public static void main(String[] args) {
    JFrame.setDefaultLookAndFeelDecorated(true);
    JFrame frame = new JFrame("FlowLayout Test");
    frame.setLayout(new FlowLayout());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    String text = "A JTextArea object represents" + "a multiline area for displaying text."
        + "You can change the number of lines" + "that can be displayed at a time.";
    JTextArea textArea1 = new JTextArea(text, 5, 10);
    textArea1.setPreferredSize(new Dimension(100, 100));
    JTextArea textArea2 = new JTextArea(text, 5, 10);
    textArea2.setPreferredSize(new Dimension(100, 100));
    JScrollPane scrollPane = new JScrollPane(textArea2, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
        JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
    textArea1.setLineWrap(true);
    textArea2.setLineWrap(true);
    frame.add(textArea1);
    frame.add(scrollPane);
    frame.pack();
    frame.setVisible(true);
  }
}








14.89.FlowLayout
14.89.1.Laying Out Components in a Flow (Left-to-Right, Top-to-Bottom)
14.89.2.Three constructors available for the FlowLayout manager.
14.89.3.Layout manager
14.89.4.FlowLayout: the default layout manager for a JPanel.
14.89.5.FlowLayout BehaviorFlowLayout Behavior
14.89.6.Using FlowLayoutUsing FlowLayout
14.89.7.Changing the GapChanging the Gap
14.89.8.Setting the gaps between components and rows explicitly by calling the setHgap()Setting the gaps between components and rows explicitly by calling the setHgap()
14.89.9.Demonstrates how to fix common alignment problemsDemonstrates how to fix common alignment problems
14.89.10.Use FlowLayout to hold checkBox, Label and TextField