my homework problem Complete the code in the Practice class such that each time the user clicks the SUM button, the sum of the two valid integers will be shown in the lower right corner of the window (leave the user's input in the text fields). Here is a before and after view of the graphical user interface when the SUM button is clicked for the first time. 123 456 123 456 SUM SUM 579 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Practice { public static void main(String[] args) { JFrame frame = new JFrame("Add 2"); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); SumPanel panel = new SumPanel(); frame.getContentPane().add(panel); frame.setSize(140,80); frame.setVisible(true); } } class SumPanel extends JPanel { private JTextField op1Field; private JTextField op2Field; private JButton sumButton; private JLabel sumLabel; public SumPanel() { //Step 1; set up the layout setLayout(new GridLayout(2,2,4,4)); op1Field = new JTextField(); op2Field = new JTextField(); sumButton = new JButton("Sum"); sumLabel = newJLabel(); this.add(sumButton); this.add(op1Field); this.add(op2Field); this.add(sumButton); this.add(sumLabel); //Step3: Register the listener in the constructor //create a listener object from the following button listener class ????????????? //Register the button with its listener object ????????????? } //end of constructor SumPanel() //Step 2: Design a button listener class, implements the correct listener interface private class SumButtonListener implements ?????????? { public void action performed(ActionEvent e) { //handle the button event here ????????????? } }//end of SumButtonListener }//end of SumPanel class
my homework problem
Complete the code in the Practice class such that each time the user clicks the SUM button, the sum of the two valid integers will be shown in the lower right corner of the window (leave the user's input in the text fields). Here is a before and after view of the graphical user interface when the SUM button is clicked for the first time.
123 456 123 456
SUM SUM 579
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Practice {
public static void main(String[] args) {
JFrame frame = new JFrame("Add 2");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE);
SumPanel panel = new SumPanel();
frame.getContentPane().add(panel);
frame.setSize(140,80);
frame.setVisible(true);
}
}
class SumPanel extends JPanel {
private JTextField op1Field;
private JTextField op2Field;
private JButton sumButton;
private JLabel sumLabel;
public SumPanel() {
//Step 1; set up the layout
setLayout(new GridLayout(2,2,4,4));
op1Field = new JTextField();
op2Field = new JTextField();
sumButton = new JButton("Sum");
sumLabel = newJLabel();
this.add(sumButton);
this.add(op1Field);
this.add(op2Field);
this.add(sumButton);
this.add(sumLabel);
//Step3: Register the listener in the constructor
//create a listener object from the following button listener class
?????????????
//Register the button with its listener object
?????????????
} //end of constructor SumPanel()
//Step 2: Design a button listener class, implements the correct listener interface
private class SumButtonListener implements ?????????? {
public void action performed(ActionEvent e) {
//handle the button event here
?????????????
}
}//end of SumButtonListener
}//end of SumPanel class
Step by step
Solved in 4 steps with 2 images