【问题标题】:Java displayField refuses to clearJava displayField 拒绝清除
【发布时间】:2013-05-16 16:25:32
【问题描述】:

我正在开发一个计算器应用程序(我已对其进行了简化以使其更易于调试)。当用户点击“=”时,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);
    }
}   

计算器.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();
}       

}

【问题讨论】:

  • 你确实意识到你的字符串比较是错误的。
  • 你指的是什么文件/方法?
  • int IMPORTANTINT = 0; 如果那是IMPORTANT_INT 它应该是一个常数,对于不是常数的东西,它应该被称为importantInt (&不,没有必要让它上案例只是为了我们的利益)。
  • 是的,我通常使用 camelCase,但希望在此表单上显示人员时使变量易于查找。谢谢:D!

标签: java swing jtextfield


【解决方案1】:
public void setDisplayValue(String val){
    SwingUtilities.invokeLater(new Runnable{
        @Override
    public void run()
    {
            displayField.setText(val);
        }
    });
}

【讨论】:

    【解决方案2】:

    您正在有效地缓存dispFieldText 之前进入IMPORTANTINT if 语句块。因此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 是一个可修改的变量,所以应该是 importantIntboolean 通常处理 true/false 场景)

    【讨论】:

    • 如果我这样做,用户是否不必按两次按钮?一次清除字段,一次添加另一个数字。
    • 谢谢!这完美地工作。你知道为什么原来的方法行不通吗?
    • 上面已经详细解释过了。基本上,您正在缓存 dispFieldText 并始终在 else 块中设置旧值
    • 对不起,我错过了。这是有道理的!
    • 如果您发现它有用,请考虑 accepting this answer...:)
    猜你喜欢
    • 2015-09-01
    • 2012-05-13
    • 2011-11-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2017-05-21
    • 2012-03-31
    • 2010-12-28
    相关资源
    最近更新 更多