Create an applet interface that looks like this: The user can enter two numbers:
ID: 3535511 • Letter: C
Question
Create an applet interface that looks like this:
The user can enter two numbers:
Answer appears as:
How many objects are in the interface?
Your program will create the interface only. We will add functionality to this program next week.
This is the type of format I'm looking for:
// import statements
import javax.swing.*;
import java.awt.*;
// declare the class and make it a JApplet
public class JDivideMeGridLayout extendsJApplet
{
// 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 the dividend");
JLabel secondLabel = new JLabel("Enter the divisor");
JLabel thirdLabel = new JLabel("The quotient is");
JButton pressMe = new JButton("Divide");
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);
publicvoid init( )
{
// declare your container
Container con = getContentPane( );
//set the layout
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);
con.add(thirdLabel);
con.add(quotient);
con.add(pressMe);
}
}
Create an applet interface that looks like this:
The user can enter two numbers:
Answer appears as:
How many objects are in the interface?
Explanation / Answer
// 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 JDivideMeGridLayout extends JApplet
{
public JDivideMeGridLayout() {
}
// 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);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.