In the above GUI after entering two numbers in the first two text fields, clicki
ID: 3846821 • Letter: I
Question
In the above GUI after entering two numbers in the first two text fields, clicking the Calculate button, the summation result is shown in the third text field. API information is as follows. Method of Integer class public static int parseInt (stringName) Attempts to convert the String object that's supplied as an argument to an int type. If successful, it returns the int value. public String toString(intName) Converts the int value that's supplied as an argument to a String object and returns that String object. Methods for JTextField class: public String getText(): returns the text in the control as a string public void setText(String): set the text in the control public boolean requestFocusInWindow(): moves the focus to the control. Returns false if the focus change request is guaranteed to fail true if it is likely to succeed. public void setEnabled(boolean): Sets the specified boolean to indicate whether or not this TextComponent should be editable. public void set ble(boolean): Sets the focusable state of this Component to the specified value. All GUI controls (i.e., text fields and button) have been declared and instantiated, and their instance variables' names are text1, text2, text3, and button, respectively. Complete the following event handing method for the button. For simplicity, exception handling or data validation is not required. private void buttonActionPerformed (java.awt.event.ActionEvent evt)Explanation / Answer
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Q2 extends Applet implements ActionListener
{
TextField t1 = new TextField(10);
TextField t2 = new TextField(10);
TextField t3 = new TextField(10);
Label l1 = new Label("FIRST NO=:");
Label l2 = new Label("SECOND NO:");
Label l3 = new Label("SUM:");
Button calc = new Button("Calculate");
public void init()
{
add(l1);
add(t1);
add(l2);
add(t2);
add(l3);
add(t3);
add(calc);
calc.addActionListener(this);
}
private void buttonActionPerformed(java.awt.event.Actionevent evt)
{
if(evt.getsource()==calc;
{
int n1 = Integer.parseInt(t1.getText());
int n2 = Integer.parseInt(t2.getText());
t3.setText(" " + (n1 + n2));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.