尝试用 Java 创建一个计算器。按钮输入不会在文本字段中打印

Trying to create a calculator with Java. Button inputs won't print in the textfield

我正在尝试为 class 创建一个科学计算器。我不确定哪里出错了,但它不会将任何按钮输入打印到文本字段中。有人知道我哪里出错了吗?问题来自代码底部的'calc' class。我只对计算器进行编码以响应数字输入,因此其他按钮都不起作用,但数字按钮应该起作用,但我不知道如何修复它。我对编程还是很陌生

package scientificcalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ScientificCalc extends JFrame {
    JFrame Frame;
    JPanel Panel;
    JTextField Text;
    JButton Button[]= new JButton[25]; // Array holds all calculator buttons
    String val0, val1, val2; // Each val records a button pressed
    public ScientificCalc() {
        val0 = val1 = val2 = "";
        Frame = new JFrame("Scientific Calculator");
        Panel = new JPanel();
        Panel.setBackground(Color.RED);
        Text = new JTextField(20);
        Text.setEditable(false);
        Frame.setVisible(true);
        Frame.setSize(400,400);
        Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ActionListener a = null;
        // Following Lines create each button
        Button[0] = new JButton("0");
        Button[0].addActionListener(a);
        Button[1] = new JButton("1");
        Button[1].addActionListener(a);
        Button[2] = new JButton("2");
        Button[2].addActionListener(a);
        Button[3] = new JButton("3");
        Button[3].addActionListener(a);
        Button[4] = new JButton("4");
        Button[4].addActionListener(a);
        Button[5] = new JButton("5");
        Button[5].addActionListener(a);
        Button[6] = new JButton("6");
        Button[6].addActionListener(a);
        Button[7] = new JButton("7");
        Button[7].addActionListener(a);
        Button[8] = new JButton("8");
        Button[8].addActionListener(a);
        Button[9] = new JButton("9");
        Button[9].addActionListener(a);
        Button[10] = new JButton("+");
        Button[10].addActionListener(a);
        Button[11] = new JButton("-");
        Button[11].addActionListener(a);
        Button[12] = new JButton("*");
        Button[12].addActionListener(a);
        Button[13] = new JButton("/");
        Button[13].addActionListener(a);
        Button[14] = new JButton("SQRT");
        Button[14].addActionListener(a);
        Button[15] = new JButton("Sin");
        Button[15].addActionListener(a);
        Button[16] = new JButton("Cos");
        Button[16].addActionListener(a);
        Button[17] = new JButton("Tan");
        Button[17].addActionListener(a);
        Button[18] = new JButton("1/x");
        Button[18].addActionListener(a);
        Button[19] = new JButton("x^2");
        Button[19].addActionListener(a);
        Button[20] = new JButton("log");
        Button[20].addActionListener(a);
        Button[21] = new JButton("!");
        Button[21].addActionListener(a);
        Button[22] = new JButton(".");
        Button[22].addActionListener(a);
        Button[23] = new JButton("=");
        Button[23].addActionListener(a);
        Button[24] = new JButton("Clear");
        Button[24].addActionListener(a);
        // Following Lines add everything to Panel and Frame
        Panel.add(Text);
        Panel.add(Button[0]);
        Panel.add(Button[1]);
        Panel.add(Button[2]);
        Panel.add(Button[3]);
        Panel.add(Button[4]);
        Panel.add(Button[5]);
        Panel.add(Button[6]);
        Panel.add(Button[7]);
        Panel.add(Button[8]);
        Panel.add(Button[9]);
        Panel.add(Button[10]);
        Panel.add(Button[11]);
        Panel.add(Button[12]);
        Panel.add(Button[13]);
        Panel.add(Button[14]);
        Panel.add(Button[15]);
        Panel.add(Button[16]);
        Panel.add(Button[17]);
        Panel.add(Button[18]);
        Panel.add(Button[19]);
        Panel.add(Button[20]);
        Panel.add(Button[21]);
        Panel.add(Button[22]);
        Panel.add(Button[23]);
        Panel.add(Button[24]);
        Frame.add(Panel);
    }
    
    public class calc implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            String i = e.getActionCommand();
            if ((i.charAt(0) >= '0' && i.charAt(0) <= '9') || i.charAt(0) == '.') {
                if (!val1.equals("")) {
                    val2 = val2 + i;
                } else {
                    val0 = val0 + i; // Sets location of each digit when button is pressed
                }
                Text.setText(val0+val1+val2);
            }
        }
    }
    public static void main(String[] args) {
        new ScientificCalc();
    }
}

您当前正在将您设置为 null 的 ActionListener a 添加到所有按钮。将 ActionListener a = null 更改为 ActionListener a = new calc();

您忘记通过 ActionListener a = new calc();

初始化 calc 对象
  1. 使用以小写字母开头的字段名称!
  2. 使用循环代替重复代码!
  3. class 个名字以大写字母开头!
  4. 通过 CustomActionListener 命名计算 class 下面有漂亮的代码!

提问:一直是false为什么还要查? if (!val1.equals(""))

package scientificcalc;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ScientificCalc extends JFrame {
    JFrame frame;
    JPanel panel;
    JTextField textField;
    JButton[] buttonArray = new JButton[25]; // Array holds all calculator buttons
    String val0, val1, val2; // Each val records a button pressed

    public ScientificCalc() {
        val0 = val1 = val2 = "";
        frame = new JFrame("Scientific Calculator");
        panel = new JPanel();
        panel.setBackground(Color.RED);
        textField = new JTextField(20);
        textField.setEditable(false);
        frame.setVisible(true);
        frame.setSize(400, 400);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        CustomActionListener actionListener = new CustomActionListener();
        // Following Lines create each button
        buttonArray[0] = new JButton("0");
        buttonArray[1] = new JButton("1");
        buttonArray[2] = new JButton("2");
        buttonArray[3] = new JButton("3");
        buttonArray[4] = new JButton("4");
        buttonArray[5] = new JButton("5");
        buttonArray[6] = new JButton("6");
        buttonArray[7] = new JButton("7");
        buttonArray[8] = new JButton("8");
        buttonArray[9] = new JButton("9");
        buttonArray[10] = new JButton("+");
        buttonArray[11] = new JButton("-");
        buttonArray[12] = new JButton("*");
        buttonArray[13] = new JButton("/");
        buttonArray[14] = new JButton("SQRT");
        buttonArray[15] = new JButton("Sin");
        buttonArray[16] = new JButton("Cos");
        buttonArray[17] = new JButton("Tan");
        buttonArray[18] = new JButton("1/x");
        buttonArray[19] = new JButton("x^2");
        buttonArray[20] = new JButton("log");
        buttonArray[21] = new JButton("!");
        buttonArray[22] = new JButton(".");
        buttonArray[23] = new JButton("=");
        buttonArray[24] = new JButton("Clear");
        for (int i = 0; i < buttonArray.length; i++) {
            buttonArray[i].addActionListener(actionListener);
        }
        // Following Lines add everything to Panel and Frame
        panel.add(textField);
        for (JButton jButton : buttonArray) {
            panel.add(jButton);
        }
        frame.add(panel);
    }

    class CustomActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String i = e.getActionCommand();
            if ((i.charAt(0) >= '0' && i.charAt(0) <= '9') || i.charAt(0) == '.') {
                if (!val1.equals("")) {
                    val2 = val2 + i;
                } else {
                    val0 = val0 + i; // Sets location of each digit when button is pressed
                }
                textField.setText(val0 + val1 + val2);
            }
        }
    }

    public static void main(String[] args) {
        new ScientificCalc();
    }
}