Java displayField拒绝清除

时间:2013-05-16 16:25:33

标签: java swing jtextfield

我正在研究一个计算器应用程序(我已经简化了它,使其更容易调试)。当用户点击'='时,IMPORTANTINT将变为1.当用户点击另一个按钮时,该字段应该用CalculatorEngine中的if then语句清除:

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

这样做可以让用户查看结果,然后开始新的计算。 textField不想清除。有人知道为什么吗?谢谢!

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

}

2 个答案:

答案 0 :(得分:0)

在进入dispFieldText if语句块之前,您有效地缓存了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(A boolean通常会处理true/false方案)

答案 1 :(得分:0)

public void setDisplayValue(String val){
    SwingUtilities.invokeLater(new Runnable{
        @Override
    public void run()
    {
            displayField.setText(val);
        }
    });
}