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.awt.Insets; /* Author: James Tam Version: March 29, 2021 */ public class MyFrame extends JFrame { private JButton button1; private JButton button2; private JLabel label1; private JLabel label2; private GridBagLayout aLayout; private GridBagConstraints aConstraint; public MyFrame () { aConstraint = new GridBagConstraints(); button1 = new JButton("Top"); button2 = new JButton("Bottom"); label1 = new JLabel("Top label"); label2 = new JLabel("Bottom label"); aConstraint.insets = new Insets(1,1,1,1); aLayout = new GridBagLayout(); setLayout(aLayout); // Calling method of super class. addWidget(label1,0,0,1,1); addWidget(label2,0,1,1,1); addWidget(button1,1,0,1,1); addWidget(button2,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. } }