Make the component stretch in both directions
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Main {
public static void main(String[] argv) throws Exception {
JFrame frame = new JFrame();
GridBagLayout gbl = new GridBagLayout();
frame.setLayout(gbl);
JButton component = new JButton("1");
frame.add(component);
frame.add(new JButton("2"));
gbl.layoutContainer(frame);
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbl.setConstraints(component, gbc);
frame.add(component);
frame.pack();
frame.setVisible(true);
}
}
Related examples in the same category