// import statements import javax.swing.*; import java.awt.*; import java.awt.ev
ID: 3535969 • Letter: #
Question
// import statements
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
// declare the class and make it a JApplet
public class JAddTwo extends JApplet implements
{
public JAddTwo() {
}
// declare the objects you need. This interface needs
// two JTextFields for the input values.
// Two labels for the input and two labels for the output
// Notice that one of the JLabels is empty
// It also needs a button and a GridLayout.
JLabel firstLabel = new JLabel("Enter a number : ");
JLabel secondLabel = new JLabel("Enter a second number : ");
JLabel thirdLabel = new JLabel("The Sum is ");
JButton pressMe = new JButton("Add");
JTextField number1 = new JTextField("", 10);
JTextField number2 = new JTextField("", 10);
JLabel quotient = new JLabel("");
// Declare the grid layout with 4 rows and 2 columns.
GridLayout grid = new GridLayout(4,2);
public void init( )
{
// declare your container
Container con = getContentPane( );
//set the layout
this.setSize(400, 300);
con.setLayout(grid);
// add the objects to the container in the order
// in which you wish them to appear
con.add(firstLabel);
con.add(number1);
con.add(secondLabel);
con.add(number2);
pressMe.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
thirdLabel.setText("The Sum is "+(Integer.parseInt(number1.getText())+Integer.parseInt(number2.getText())));
} catch (NumberFormatException e) {
thirdLabel.setText("Invalid number(s)");
}
}
});
con.add(pressMe);
con.add(thirdLabel);
con.add(quotient);
}
}
Explanation / Answer
What is your question here...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.