Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Help in Java: If you run the Adder.java (using javac Adder.java and then java Ad

ID: 3663805 • Letter: H

Question

Help in Java:
If you run the Adder.java (using javac Adder.java and then java Adder commands on the command line), you will see that when the Calculate button is pressed, nothing happens. Add enough code to the AdderFrame.java class such that when the Calculate button is pressed, sum of the two integer numbers entered into the text fields is shown after the equal sign as demonstrated in the following image attached.

_____________________
Adder.java
___________
import javax.swing.JFrame;

public class Adder{
/**
The main method creates an instance of the
Adder class, which displays
its window on the screen.
*/

public static void main(String[] args)
{
   final int WINDOW_WIDTH = 520; // Window width
   final int WINDOW_HEIGHT = 100; // Window height
AdderFrame adder = new AdderFrame();  
  
// Specify what happens when the close button is clicked.
adder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
   // Set the size of the window.
adder.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  
       // Display the window.
adder.setVisible(true);
}
}

_____________________
AdderFrame.java
___________
import javax.swing.*; // Needed for Swing classes
import java.awt.event.*; // Needed for ActionListener Interface

/**
The AdderFrame class displays a JFrame that
lets the user to add two integers
*/

public class AdderFrame extends JFrame
{
private JPanel panel; // To reference a panel
private JLabel messageLabelAdd; // To reference a label
private JLabel messageLabelequal; // To reference a label
private JTextField firstNumber; // To reference a text field
private JTextField secondNumber; // To reference a text field
private JLabel result;
private JButton calcButton; // To reference a button

/**
Constructor
*/
public AdderFrame()
{
       super("Integer Adder");
  
       /**
       The buildPanel method adds a label, text field, and
       and a button to a panel.
       */
       // Build the panel and add it to the frame.
       // Create a label to display instructions.
       messageLabelAdd = new JLabel(" + ");
       messageLabelequal = new JLabel(" = ");
       result = new JLabel(" 0 ");
       // Create two text fields 10 characters wide.
       firstNumber = new JTextField(10);
       secondNumber = new JTextField(10);
       // Create a button with the caption "Calculate".
       calcButton = new JButton("Calculate");

      

// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();

// Add the label, text field, and button
// components to the panel.
   panel.add(firstNumber);
panel.add(messageLabelAdd);
panel.add(secondNumber);
   panel.add(messageLabelequal);
   panel.add(result);
panel.add(calcButton);

// Add the panel to the frame's content pane.
add(panel);
  
}
}
___________________________________

Integer Adder 120 = 175 calculat

Explanation / Answer

Please find Working Code

you need to ActionListner in your Calculate Button. For that AdderFrame class should implement ActionListner interface

//Adder.java

import javax.swing.JFrame;
public class Adder{
/**
The main method creates an instance of the
Adder class, which displays
its window on the screen.
*/
public static void main(String[] args)
{
final int WINDOW_WIDTH = 520; // Window width
final int WINDOW_HEIGHT = 100; // Window height
AdderFrame adder = new AdderFrame();
  
// Specify what happens when the close button is clicked.
adder.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Set the size of the window.
adder.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  
// Display the window.
adder.setVisible(true);
}
}

//AdderFrame.java

import javax.swing.*; // Needed for Swing classes

import java.awt.event.*; // Needed for ActionListener Interface

/**
The AdderFrame class displays a JFrame that
lets the user to add two integers
*/
public class AdderFrame extends JFrame implements ActionListener
{
private JPanel panel; // To reference a panel
private JLabel messageLabelAdd; // To reference a label
private JLabel messageLabelequal; // To reference a label
private JTextField firstNumber; // To reference a text field
private JTextField secondNumber; // To reference a text field
private JLabel result;
private JButton calcButton; // To reference a button
/**
Constructor
*/
public AdderFrame()
{
super("Integer Adder");

/**
The buildPanel method adds a label, text field, and
and a button to a panel.
*/
// Build the panel and add it to the frame.
// Create a label to display instructions.
messageLabelAdd = new JLabel(" + ");
messageLabelequal = new JLabel(" = ");
result = new JLabel(" 0 ");
// Create two text fields 10 characters wide.
firstNumber = new JTextField(10);
secondNumber = new JTextField(10);
// Create a button with the caption "Calculate".
calcButton = new JButton("Calculate");

// Adding Action Listner to calcButton
calcButton.addActionListener(this);

// Create a JPanel object and let the panel
// field reference it.
panel = new JPanel();
// Add the label, text field, and button
// components to the panel.
panel.add(firstNumber);
panel.add(messageLabelAdd);
panel.add(secondNumber);
panel.add(messageLabelequal);
panel.add(result);
panel.add(calcButton);
// Add the panel to the frame's content pane.
add(panel);

}
@Override
public void actionPerformed(ActionEvent e) {
   float a,b,c;
   if(e.getSource()== calcButton)
   {
   a=Float.parseFloat(firstNumber.getText());
   b=Float.parseFloat(secondNumber.getText());
   c=a+b;
   result.setText(String.valueOf(c));
     
   }
  
  
}
}