0

As my first applet, I'm making a calculator (only adding at the moment), but I am stuck at figuring out how to get numbers from the listeners. So far I have:

    import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;

public class calculator extends JApplet {

    private JTextField num1;
    private JTextField num2;
    private JButton calculate;
    private JLabel result;
    private double numOne,numTwo;
    private double sum = numOne + numTwo;

    public void init() {
        num1 = new JTextField("",9);
        num2 = new JTextField("",9);
        calculate = new JButton("Calculate");
        result = new JLabel("The Answer is: " + sum);

        num1.addActionListener(new num1Listener());
        num2.addActionListener(new num2Listener());
        calculate.addActionListener(new doMath());

        add(num1);
        add(num2);
        add(calculate);

        setLayout(new FlowLayout());
        setSize(200,200);
    }
    public void paint(Graphics g) {
        super.paint(g);
        g.drawString("The answer is " + sum, 20, 20);
    }
    class num1Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String num1input = num1.getText();
            numOne = Double.parseDouble(num1input);
            }
    }
    class num2Listener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String num2input = num2.getText();
            numTwo = Double.parseDouble(num2input);
        }
    }
    class doMath implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            add(result);
        }
    }
}

How can I get my numOne and numTwo doubles from inside these listeners and into my code? I would like to add them together and store them in the sum variable.

AlecR
  • 47
  • 2
  • 9
  • 1) See also this [calculator example](http://stackoverflow.com/a/7441804/418556). It uses `ScriptEngine` to evaluate the expression in the text area. 2) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). – Andrew Thompson Oct 29 '14 at 01:34

1 Answers1

0

Store them as instance fields within your code...

public class calculator extends JApplet {
    private double number1, number2;

Then assign them the values from the fields...

public void actionPerformed(ActionEvent e) {
    String num1input = num1.getText();
    number1 = Double.parseDouble(num1input);

Having said all that. I might be better to have an "equals" button, which when clicked, gets the values from the text fields, converts them to doubles sums them and displays the result on the screen in one step, rather than trying to use the text fields individually, but that's just me

Updated

In order to display the result, you first need to get the numbers...

double number1 = Double.parseDouble(num1.getText());
double number2 = Double.parseDouble(num2.getText());

You need to sum them together...

sum = number1 + number2;

Then you need to update the result label...

result.setText(Double.toString(sum));

Doing...

private double sum = numOne + numTwo;

Will only add the CURRENT values of numOne and numTwo together, which, but default, are 0, so it has no meaning...

As a leaner, I would advise against using JApplet to learn with, applets have there own issues which are difficult enough for experienced developers to diagnose, stick to good old desktop windows

As an example...

public class Calculator extends JApplet {

    private JTextField num1;
    private JTextField num2;
    private JButton calculate;
    private JLabel result;
    private double numOne, numTwo;
    private double sum = numOne + numTwo;

    public void init() {
        num1 = new JTextField("", 9);
        num2 = new JTextField("", 9);
        calculate = new JButton("Calculate");
        result = new JLabel("The Answer is: " + sum);

        calculate.addActionListener(new DoMath());

        add(num1);
        add(num2);
        add(calculate);
        add(result);

        setLayout(new FlowLayout());
        setSize(200, 200);
    }

    class DoMath implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent e) {
            try {
                double number1 = Double.parseDouble(num1.getText());
                double number2 = Double.parseDouble(num2.getText());
                result.setText(Double.toString(number1 + number2));
            } catch (NumberFormatException exp) {
                result.setText("Bad values, fix em");
            }
        }
    }
}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thanks! I'm now trying to make the button listener function. Why will it not work if I have the listener add(result) one actionEvent? – AlecR Oct 29 '14 at 00:39
  • 1. Don't override `paint` for this, it's way more then you need. 2. Try calling `invalidate();` and `repaint()` after `add(result);` to force the layout manager to update the UI – MadProgrammer Oct 29 '14 at 00:54
  • I followed your steps and it all works, except the two numbers aren't adding and storing in sum. – AlecR Oct 29 '14 at 01:07
  • ...When do you actually add them together...? Shouldn't that be done within the `doMath` listener? – MadProgrammer Oct 29 '14 at 01:09
  • Yes, I added sum = numOne + numTwo into the doMath listener, and it still isn't giving me a correct answer – AlecR Oct 29 '14 at 01:11
  • Thanks for your help, and I'll definitely be sticking to desktop windows after this project (had to do a JApplet for school) – AlecR Oct 29 '14 at 01:49