Hi guys I am trying to build a simple calculator but I cant get the answer to show in the output in Jtextfield
here is the test
// Testing Calculator
import javax.swing.JFrame;
public class CalculatorTest
{
// Main method
public static void main( String[] args )
{
Calculator calculator = new Calculator();
calculator.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
calculator.setSize( 500, 100 );
calculator.setVisible( true ); // display frame
} // end main
} // end class CalculatorTest
Here is the calculator program.
// Simple java calculator application that determines the sum, difference, product, division of two numbers ie a & b
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
public class Calculator extends JFrame
{
private JLabel number1;
private JLabel number2;
private JLabel answer;
private JTextField num1;
private JTextField num2;
private JTextField result;
private JButton plus;
private JButton subtract;
private JButton multiply;
private JButton divide;
// Calculator constructor
public Calculator()
{
super( "Simple Calculator" );
setLayout( new FlowLayout() );
CalculatorHandler getAnswer = new CalculatorHandler();
// JLabel constructor initiliazation
number1 = new JLabel( "Number 1 " );
add( number1 );
num1 = new JTextField( 5 );
add( num1 );
number2 = new JLabel( "Number 2 " );
add( number2 );
num2 = new JTextField( 5 );
add( num2 );
answer = new JLabel( "Result " );
add( answer );
result = new JTextField(10);
result.setText(getAnswer.getOutput());
add( result );
System.out.println(getAnswer.getOutput());
plus = new JButton( "Add" );
add( plus );
subtract = new JButton( "Subtraction" );
add( subtract );
multiply = new JButton( "Multiply" );
add( multiply );
divide = new JButton( "Divide" );
add( divide );
// register even handlers
CalculatorHandler handler = new CalculatorHandler();
num1.addActionListener( handler );
num2.addActionListener( handler );
plus.addActionListener( handler );
subtract.addActionListener( handler );
multiply.addActionListener( handler );
divide.addActionListener( handler );
} // end constructor calculator
private class CalculatorHandler implements ActionListener
{
private double answer;
private String result ="";
// handle input events
public void actionPerformed( ActionEvent event )
{
double first = Double.parseDouble( num1.getText());
double second = Double.parseDouble( num2.getText());
String output="";
if ( event.getActionCommand() == "Add" )
output = String.format("%f", first + second);
else if ( event.getActionCommand() == "Subtract" )
output = String.format("%f", first - second);
else if ( event.getActionCommand() == "Multiply" )
output = String.format("%f", first * second);
else if ( event.getActionCommand() == "Divide")
output = String.format("%f", first / second);
result = output;
} // end
//public void setOuput( String output )
//{
// result = output;
//}
public String getOutput()
{
return result;
}
}
}// end class Calculator