Java GroupLayout add gap

Description

Java GroupLayout add gap

import java.awt.Container;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.LayoutStyle; 

public class Main { 
    public static void main(String[] args) { 
        JFrame frame = new JFrame("Simplest GroupLayout"); 
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        Container contentPane = frame.getContentPane(); 

        // Create an object of the GroupLayout class for contentPane 
        GroupLayout groupLayout = new GroupLayout(contentPane); 

        // Set the content pane's layout to a GroupLayout 
        contentPane.setLayout(groupLayout); 

        // Add two JButtons to the content pane 
        JButton b1 = new JButton("Button 1"); 
        JButton b2 = new JButton("Little Bigger Button 2"); 

        groupLayout.setHorizontalGroup( 
                groupLayout.createSequentialGroup() 
                        .addComponent(b1) 
                        .addComponent(b2)); 

        groupLayout.setVerticalGroup( /*from   w  w w  . j  a  v  a  2  s.  c o  m*/
                groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE) 
                        .addComponent(b1) 
                        .addComponent(b2)); 

        groupLayout.setHorizontalGroup( 
            groupLayout.createSequentialGroup() 
                       .addComponent(b1) 
                       .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) 
                       .addComponent(b2)); 
        
        frame.pack(); 
        frame.setVisible(true); 
    } 
} 



PreviousNext

Related