如何从两个JTextField中计算数字?

huangapple go评论60阅读模式
英文:

How to calculate number from two Jtextfield?

问题

我正在设计一个食品订购系统,我需要获取顾客支付的金额输入,然后减去他们需要支付的总金额以计算找零。我的JTextField不能显示正确的找零金额,它只显示0.0。我不确定我的代码有什么问题。希望你们能帮助我。非常感谢你们的帮助,谢谢!

public Cash() {
    init();
    btnPay.addActionListener(this);
    setVisible(true);
}

public String returnChange1() {
    double change = 0.00 ;
    double custPay;
    String total = lblDisplayTotal.getText();
    double a=Double.parseDouble(total);

    if (!(txtCustPay.getText().isEmpty())){
        custPay = Double.parseDouble(txtCustPay.getText());
        change = custPay - a;
    }
    return String.valueOf(change);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(btnPay)) {
        returnChange1();
    }
}

public void init() {
    txtChange = new JTextField(returnChange1());
    txtChange.setSize(150, 30);
    txtChange.setLocation(150, 250);
    add(txtChange);
}
英文:

I'm designing a food ordering system which I need to get the input of cash that customer paid, then minus with the total price they need to pay to calculate the change. My JTextField can't show the correct answer for the change, it only show 0.0. I'm not sure what's the problem with my code. Hope that you all can help me. Appreciate your helps, thank you!

public Cash() {
    init();
    btnPay.addActionListener(this);
    setVisible(true);
}

public String returnChange1() {
    double change = 0.00 ;
    double custPay;
    String total = lblDisplayTotal.getText();
    double a=Double.parseDouble(total);

    if (!(txtCustPay.getText().isEmpty())){
        custPay = Double.parseDouble(txtCustPay.getText());
        change = custPay - a;
    }
    return String.valueOf(change);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(btnPay)) {
        returnChange1();
    }
}

public void init() {
    txtChange = new JTextField(returnChange1());
    txtChange.setSize(150, 30);
    txtChange.setLocation(150, 250);
    add(txtChange);
}

答案1

得分: 1

你没有将函数分配给文本字段。在按钮操作中,不要仅调用函数,而是应将函数分配给文本字段:txtChange.setText(returnChange1())。此外,尝试在将文本转换为双精度时使用 try 和 catch:

try {
    double a = Double.parseDouble(total);
} catch (NumberFormatException e) {
    e.printStackTrace();
}
上述代码在用户错误输入非数字字符时非常有用

public Cash() {
    init();
    btnPay.addActionListener(this);
    setVisible(true);
}

public String returnChange1() {
    double change = 0.00;
    double custPay;
    String total = lblDisplayTotal.getText();
    double a = Double.parseDouble(total);

    if (!(txtCustPay.getText().isEmpty())) {
        custPay = Double.parseDouble(txtCustPay.getText());
        change = custPay - a;
    }

    return String.valueOf(change);
}

public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(btnPay)) {
        txtChange.setText(returnChange1());
    }
}

public void init() {
    txtChange = new JTextField(returnChange1());
    txtChange.setSize(150, 30);
    txtChange.setLocation(150, 250);
    add(txtChange);
}
英文:

You are not assigning the function to the text field. In the button action, don't simply call the function, in this case what you should do is assign the function to the text field: txtChange.setText(returnChange1()), Also try to put a try and catch where you convert the text to double:

try{
    double a = Double.parseDouble(total);
}catch(NumberFormatException e){
    e.printStackTrace;
}

The above code is useful when the user mistakenly enters a character that is not a number.

public Cash() {
      
    init();
    btnPay.addActionListener(this);
    setVisible(true);

}

public String returnChange1() {

	double change = 0.00;
	double custPay;
	String total = lblDisplayTotal.getText();
	double a = Double.parseDouble(total);

	if (!(txtCustPay.getText().isEmpty())) {
		custPay = Double.parseDouble(txtCustPay.getText());
		change = custPay - a;
	}

	return String.valueOf(change);

}

public void actionPerformed(ActionEvent e) {

	if (e.getSource().equals(btnPay)) {
		txtChange.setText(returnChange1());
	}
}

public void init() {
	txtChange = new JTextField(returnChange1());
	txtChange.setSize(150, 30);
	txtChange.setLocation(150, 250);
	add(txtChange);
}

huangapple
  • 本文由 发表于 2020年9月2日 21:45:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63706935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定