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; private MyButtonListener aButtonListener; public MyFrame () { aConstraint = new GridBagConstraints(); aButtonListener = new MyButtonListener(); button1 = new JButton("Left"); button2 = new JButton("Right"); label1 = new JLabel("Top left"); label2 = new JLabel("Top right"); button1.setActionCommand("button1"); //Identifier for the button button2.setActionCommand("button2"); //Identifier for the button button1.addActionListener(aButtonListener); button2.addActionListener(aButtonListener); 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,1,0,1,1); addWidget(button1,0,1,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. } public JLabel getLabel1() { return(label1); } public JLabel getLabel2() { return(label2); } }