Setting the gaps between components and rows explicitly by calling the setHgap() : FlowLayout « Swing « Java Tutorial






Setting the gaps between components and rows explicitly by calling the setHgap()
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class FlowLayoutSettingGaps {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Flow Layout");
    aWindow.setBounds(30, 30, 300, 300); // Size
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    FlowLayout flow = new FlowLayout(); // Create a layout manager
    flow.setHgap(35);                   // Set the horizontal gap
    Container content = aWindow.getContentPane(); // Get the content pane
    content.setLayout(flow); // Set the container layout mgr
    // Now add six button components
    for (int i = 1; i <= 6; i++) {
      content.add(new JButton("Press " + i)); // Add a Button to content pane
    }
    aWindow.setVisible(true); // Display the window
  }
}








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