-1

I'm working on a calculator application (which I have simplified downed to make it easier to debug). When the user hits '=' the IMPORTANTINT will change to 1. When the users clicks another button the field is supposed to clear with the if then statement in CalculatorEngine:

        if(IMPORTANTINT == 1){
        System.out.println("Ran the block of code");
        parent.setDisplayValue("");
        IMPORTANTINT = 0;
        System.out.println(IMPORTANTINT);
    }

This is done so the user can view the result and then start a new calculation. The textField doesn't want to clear. Does anyone know why this is? Thanks!

CalculatorEngine.java

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JOptionPane;
import javax.swing.JButton;

public class CalculatorEngine implements  ActionListener{
Calculator parent;
double firstNum, secondNum;
String symbol;
int IMPORTANTINT = 0;
CalculatorEngine(Calculator parent){
    this.parent = parent;   
}
public void actionPerformed(ActionEvent e){
    JButton clickedButton = (JButton) e.getSource();
    String clickedButtonLabel = clickedButton.getText();
    String dispFieldText = parent.getDisplayValue();
    if(IMPORTANTINT == 1){
        System.out.println("Ran the block of code");
        parent.setDisplayValue("");
        IMPORTANTINT = 0;
        System.out.println(IMPORTANTINT);
    }
    if(clickedButtonLabel == "+"){
        firstNum = (Double.parseDouble(parent.getDisplayValue()));
        parent.setDisplayValue("");
        symbol = clickedButtonLabel;
    } else if(clickedButtonLabel == "="){
        IMPORTANTINT = 1;
        secondNum = Double.parseDouble(parent.getDisplayValue());
        double answer = firstNum + secondNum;
        parent.setDisplayValue(Double.toString(answer));
    } else{     


        parent.setDisplayValue(dispFieldText + clickedButtonLabel);
    }
}   

Calculator.java

import javax.swing.*;

import java.awt.GridLayout;
import java.awt.BorderLayout;

public class Calculator {
private JPanel windowContent;
private JPanel p1;
private JPanel sideBar;

private JTextField displayField;
private JButton button8;
private JButton button9;
private JButton buttonEqual;
private JButton buttonPlus;
Calculator(){
  windowContent= new JPanel();
  BorderLayout bl = new BorderLayout(); 
  windowContent.setLayout(bl);
  displayField = new JTextField(30);
  windowContent.add("North",displayField);

  button8=new JButton("8");
  button9=new JButton("9");
  buttonEqual=new JButton("="); 
  buttonPlus = new JButton("+");

  p1 = new JPanel();
  GridLayout gl =new GridLayout(4,3); 
  p1.setLayout(gl);

  sideBar = new JPanel();
  GridLayout gl2 = new GridLayout(5,1); 
  sideBar.setLayout(gl2);
  p1.add(button8);
  p1.add(button9);
  p1.add(buttonEqual); 

sideBar.add(buttonPlus);
  windowContent.add("Center", p1);
  windowContent.add("East", sideBar);

JFrame frame = new JFrame("Calculator");
frame.setContentPane(windowContent);

frame.pack(); 
frame.setVisible(true);
CalculatorEngine calcEngine = new CalculatorEngine(this);

button8.addActionListener(calcEngine);
button9.addActionListener(calcEngine);
buttonEqual.addActionListener(calcEngine);
buttonPlus.addActionListener(calcEngine);

}
public void setDisplayValue(String val){
    displayField.setText(val);
}
public String getDisplayValue(){
    return displayField.getText();
}    
public static void main(String[] args) 
{
    Calculator calc = new Calculator();
}       

}

4

2 回答 2

0

您在进入if 语句块dispFieldText 之前有效地缓存了。IMPORTANTINT因此JTextField被清除,但随后被清除到else块中的缓存值。

if (IMPORTANTINT == 1) {
   dispFieldText = ""; // add this
   ...
} 

if (clickedButtonLabel.equals("+")) {
  ...
} else if (clickedButtonLabel.equals("=")) {
  ...
} else {
   // Field being reset here vvvv
   parent.setDisplayValue(dispFieldText + clickedButtonLabel);
}

确保清除变量。用于String#equals检查String内容。==操作员检查参考Object

旁白:使用Java命名约定,IMPORTANTINT是一个可修改的变量,所以应该是importantInt(Aboolean通常处理一个true/false场景)

于 2013-05-16T16:34:02.857 回答
0
public void setDisplayValue(String val){
    SwingUtilities.invokeLater(new Runnable{
        @Override
    public void run()
    {
            displayField.setText(val);
        }
    });
}
于 2013-05-16T16:35:08.963 回答