import java.awt.*; import javax.swing.*; public class GridBagExample { public static void main(String[] args) { JFrame frame = new JFrame("GridBagLayout Example"); JPanel panel = new JPanel(new GridBagLayout()); JButton button1 = new JButton("Button 1"); JButton button2 = new JButton("Button 2"); JButton button3 = new JButton("Button 3"); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridx = 0; c.gridy = 0; panel.add(button1, c); c.gridx = 1; c.gridy = 0; panel.add(button2, c); c.gridx = 2; c.gridy = 0; c.gridwidth = 2; panel.add(button3, c); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); } }In this example, we create a simple layout with three buttons aligned in a row. We use GridBagConstraints to specify the constraints for each button, such as its position, size, and alignment. The code begins by importing the necessary AWT and Swing classes. We then create a JFrame object and a JPanel object with GridBagLayout. We also create three JButton objects for our layout. We then create a GridBagConstraints object and set its properties using setter methods. We set the fill property to HORIZONTAL, which stretches the buttons horizontally to fill the available space. We also set the gridx and gridy properties to specify the column and row positions of each button. Finally, we add the buttons to the panel and add the panel to the frame. We call the pack() method to resize the frame to fit the contents, and set its visibility to true to display it on the screen. Overall, the GridBagConstraints class is a useful tool for creating complex and dynamic layouts in Java applications with minimal code.