import java.awt.Component; import java.awt.GridBagConstraints; import java.awt.GridBagLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.util.Scanner; import java.awt.Insets; public class MyFrame extends JFrame { private JButton left; private JButton right; private JLabel aLabel; private GridBagLayout aLayout; private GridBagConstraints aConstraint; public MyFrame () { MyWindowListener aWindowListener = new MyWindowListener (); addWindowListener(aWindowListener); // Calling method of super class. aConstraint = new GridBagConstraints(); Scanner in = new Scanner(System.in); System.out.print("Buffer size to pad the grid (non-negative only): "); int padding = in.nextInt(); left = new JButton("L: Press me"); right = new JButton("R: Press me"); MyButtonListener aButtonListener = new MyButtonListener(); left.addActionListener (aButtonListener); right.addActionListener (aButtonListener); aLabel = new JLabel("Simple label"); aConstraint.insets = new Insets(padding,padding,padding,padding); aLayout = new GridBagLayout(); setLayout(aLayout); // Calling method of super class. addWidget(aLabel, 0, 0, 1, 1); addWidget(left, 0, 1, 1, 1); addWidget(right, 1, 1, 1, 1); } public void addWidget (Component widget, int x, int y, int w, int h) { aConstraint.gridx = x; aConstraint.gridy = y; aConstraint.gridwidth = w; aConstraint.gridheight = h; aLayout.setConstraints (widget, aConstraint); add(widget); // Calling method of super class. } }