Create from scratch an interactive program that allows the user to enter two int
ID: 3534818 • Letter: C
Question
Create from scratch an interactive program that allows the user to enter two integer values. When they click a button, the sum of the two numbers is displayed.
Create an interface similar to the one shown. It does NOT have to match this exactly in terms of size and spacing. But there should be seven objects within the user interface:
Two JTextFields for user input, either a JTextField or a JLabel for output, a JButton and three descriptive JLabels to label the inputs and output. The user interface should also have a title, which in this case is “Add 2 numbersâ€
The basic structure of the program is as follows:
import java.awt.*; // import the abstract window toolkit
import java.awt.event.*; // this is needed for event-driven programming
import javax.swing.*; // for the swing components
public class SomeClassName extends JFrame
{
// declare all objects in the user interface
// constructor to create a SomeClassName object
public SomeClassName()
{
createUserInterface();
} // end constructor
private void createUserInterface()
{
Container con = getContentPane(); // create a Container
con.setLayout(null); // create a Layout manager
// define the properties of objects in the interface
// and add them to the Container
// assuming you name the JButton addJButton
// define its properties
// and add it to the Container
con.add(addJButton);
// then register it as an ActionListener
addJButton.addActionListener(
// code the ActionListerner call here
); // end call to addActionListener
// define the properties of the user interafce
// its title and size and make it visible
} // end create user interface
private void addJButtonActionPerformed(ActionEvent event)
{
// assuming you named the method addJButtonActionPerformed
// at this point you code the solution to the problem:
// read the inputs into variables, do the calculations
// display the result
}
public static void main(String args[]) // define main method
{
// execution begins here with a call
// to the constructor
SomeClassName application = new SomeClassName();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} // end of class
Create an interface similar to the one shown. It does NOT have to match this exactly in terms of size and spacing. But there should be seven objects within the user interface:
Two JTextFields for user input, either a JTextField or a JLabel for output, a JButton and three descriptive JLabels to label the inputs and output. The user interface should also have a title, which in this case is “Add 2 numbersâ€
Explanation / Answer
/* */ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * * @author -XeS- */ public class Calculator extends JFrame{ // Declare GUI components private JTextField jtfFirst; private JTextField jtfSecond; private JTextField jtfResult; private JLabel jlblFirst; private JLabel jlblSecond; private JLabel jlblResult; private JPanel entryPanel; private JPanel buttonPanel; private JButton jbtAdd; private JButton jbtSubtract; private JButton jbtMultiply; private JButton jbtDivide; // main method to instantiate and customize frame public static void main(String[] args) { Calculator frame = new Calculator(); frame.setTitle( "Four Function Calculator"); frame.setSize( 350, 150 ); frame.setLocation(400, 400); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } // GUI constructor public Calculator() { // Instantiate GUI components for top of frame jlblFirst = new JLabel( "First Number: ", JLabel.CENTER ); jlblSecond = new JLabel( "Second Number: ", JLabel.CENTER ); jlblResult = new JLabel( "Result: ", JLabel.CENTER ); jtfFirst = new JTextField(12); jtfSecond = new JTextField(12); jtfResult = new JTextField(12); entryPanel = new JPanel(); // make result text field uneditable jtfResult.setEditable( false ); // Set layout manager of panel entryPanel.setLayout( new GridLayout(3,2)); // add GUI components to panel entryPanel.add( jlblFirst ); entryPanel.add( jtfFirst ); entryPanel.add( jlblSecond ); entryPanel.add( jtfSecond ); entryPanel.add( jlblResult ); entryPanel.add( jtfResult ); // add entryPanel to frame add(entryPanel, BorderLayout.CENTER); // Instantiate GUI components for bottom of frame jbtAdd = new JButton( "Add" ); jbtSubtract = new JButton( "Subtract" ); jbtMultiply = new JButton( "Multiply" ); jbtDivide = new JButton( "Divide" ); buttonPanel = new JPanel(); // add buttons to panel buttonPanel.add( jbtAdd ); buttonPanel.add( jbtSubtract ); buttonPanel.add( jbtMultiply ); buttonPanel.add( jbtDivide ); // add buttonPanel to frame add( buttonPanel, BorderLayout.SOUTH); //Event Handler jbtAdd.addMouseListener(new List_ButtonADD()); jbtSubtract.addMouseListener(new List_ButtonSUB()); jbtDivide.addMouseListener(new List_ButtonDIV()); jbtMultiply.addMouseListener(new List_ButtonMUL()); } private class List_ButtonADD implements MouseListener { public void mouseClicked(MouseEvent event) { double x=Double.parseDouble(jtfFirst.getText()); double y=Double.parseDouble(jtfSecond.getText()); jtfResult.setText(String.valueOf(x+y)); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } private class List_ButtonDIV implements MouseListener { int cnt=0; public void mouseClicked(MouseEvent event) { double x=Double.parseDouble(jtfFirst.getText()); double y=Double.parseDouble(jtfSecond.getText()); jtfResult.setText(String.valueOf(x/y));; } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } private class List_ButtonSUB implements MouseListener { int cnt=0; public void mouseClicked(MouseEvent event) { double x=Double.parseDouble(jtfFirst.getText()); double y=Double.parseDouble(jtfSecond.getText()); jtfResult.setText(String.valueOf(x-y)); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } private class List_ButtonMUL implements MouseListener { int cnt=0; public void mouseClicked(MouseEvent event) { double x=Double.parseDouble(jtfFirst.getText()); double y=Double.parseDouble(jtfSecond.getText()); jtfResult.setText(String.valueOf(x*y)); } public void mousePressed(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.