import java.awt.*; import javax.swing.*; public class GridExample extends JFrame { public GridExample() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); JButton button = new JButton("Button"); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; add(button, gbc); pack(); setVisible(true); } public static void main(String[] args) { new GridExample(); } }
import java.awt.*; import javax.swing.*; public class GridExample extends JFrame { public GridExample() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); JButton button1 = new JButton("Button 1"); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 1; add(button1, gbc); JButton button2 = new JButton("Button 2"); gbc.gridx = 1; gbc.gridy = 0; gbc.gridwidth = 1; add(button2, gbc); pack(); setVisible(true); } public static void main(String[] args) { new GridExample(); } }Package library: java.awt.