我不明白为什么会发生

问题描述 投票:0回答:1
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//implementing ActionListener interface
public class Calculator implements ActionListener {
    //Creating variables for our calculations
    double number, answer;
    int calculation;

@Override
    public void actionPerformed(ActionEvent e){
        Object source = e.getSource();
        if (source == buttonExit){
            System.exit(0);
        } else if (source == buttonClear){
            textField.setText("");
        } else if (source == buttonDelete) {

            //Setting functionality for delete button(backspace)
            int length = textField.getText().length();
            int number = length - 1;

            //Workking ON
            if (textField.getText()) == (Double.toString(answer)) {
                textField.setText("");
                answer = Double.NaN;
            } else if (length > 0) {
                StringBuilder back = new StringBuilder(textField.getText());
                back.deleteCharAt(number);
                textField.setText(back.toString());
            }
            } else if (source == buttonZero) {
            if (textField.getText().equals("0")) {
                return;
            } else {
                textField.setText(textField.getText() + "0");
            }

我收到错误 - 令牌“==”上的语法错误,OnlySynchronized 无效

我希望程序检查文本字段是否等于答案变量,如果等于,那么它将清除文本字段和答案变量。我尝试以不同的方式编写同一行代码,但得到不同的错误。

java swing
1个回答
0
投票

你尝试过使用 equals() 吗?

String currentText = textField.getText();

currentText.equals(Double.toString(answer))

© www.soinside.com 2019 - 2024. All rights reserved.