package de.tutorials.swing;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
@SuppressWarnings("serial")
public class GridBagLayoutExample extends JFrame {
public GridBagLayoutExample() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.weightx = c.weighty = 1.0;
add(new JButton("Button #1"), 0, 0, 4, 4, c);
c.weightx = c.weighty = 0.0;
add(new JButton("Button #2"), 4, 0, 1, 1, c);
add(new JButton("Button #3"), 4, 1, 1, 1, c);
add(new JButton("Button #4"), 4, 2, 1, 2, c);
add(new JButton("Button #5"), 0, 4, 1, 1, c);
add(new JButton("Button #6"), 2, 4, 1, 1, c);
add(new JButton("Button #7"), 3, 4, 2, 1, c);
add(new JButton("Button #8"), 1, 5, 1, 1, c);
add(new JButton("Button #9"), 3, 5, 1, 1, c);
pack();
}
private void add(Component c, int gridx, int gridy, int gridwidth,
int gridheight, GridBagConstraints gbc) {
gbc.fill = GridBagConstraints.BOTH;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = gridx;
gbc.gridy = gridy;
gbc.gridwidth = gridwidth;
gbc.gridheight = gridheight;
add(c, gbc);
}
public static void main(String[] a) {
new GridBagLayoutExample().setVisible(true);
}
}