完成Applet计算器

时间:2014-10-29 00:21:55

标签: java swing applet

作为我的第一个小程序,我正在制作一个计算器(目前仅添加计算器),但我一直在想弄清楚如何从听众那里获取数字。到目前为止,我有:

    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);
        }
    }
}

如何从这些侦听器内部和我的代码中获取我的numOne和numTwo双打?我想将它们一起添加并存储在sum变量中。

1 个答案:

答案 0 :(得分:0)

将它们存储为代码中的实例字段...

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

然后从字段中为它们分配值......

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

说完了这一切。我可能最好有一个“等于”按钮,当点击它时,从文本字段中获取值,将它们转换为double s将它们相加并在一个步骤中在屏幕上显示结果,而不是尝试单独使用文本字段,但那只是我

<强>更新

为了显示结果,首先需要获取数字......

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

你需要把它们加在一起......

sum = number1 + number2;

然后您需要更新result标签...

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

...做

private double sum = numOne + numTwo;

只会将numOnenumTwo的CURRENT值一起添加,但默认情况下为0,因此没有任何意义......

作为一个更精简的人,我建议不要使用JApplet来学习,applet有自己的问题,这对于经验丰富的开发人员来说很难诊断,坚持使用旧的桌面窗口

作为一个例子......

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");
            }
        }
    }
}