我遇到了使用Swing否定的问题,由于某种原因我的Big Decimal否定和添加不起作用,我的代码编译但是减号和加号计算不起作用,任何帮助都是最受欢迎的。
代码段
//Convert the JLabel to a Double so we can perform negation.
diallerPanelSum = new BigDecimal(balanceAmount.getText());
//Dont allow the Balance to go negative!
if(diallerPanelSum.compareTo(BigDecimal.ZERO)>0)
{
if(e.getSource()==buttonMakeCall)
{
diallerPanelSum.subtract(new BigDecimal("1.0"));
}
if(e.getSource()==buttonSendText)
{
diallerPanelSum.subtract(new BigDecimal("0.10"));
}
if(e.getSource()==buttonTopUp)
{
diallerPanelSum.add(new BigDecimal("10.00"));
}
}
//Convert the Float back to a JLabel
balanceAmount.setText(String.valueOf(diallerPanelSum));
答案 0 :(得分:1)
BigDecimals
是不可变的。因此,您必须再次将add()
或subtract()
等操作的结果分配给BigDecimal
,因为它们会生成新 BigDecimal
。试试这个:
if (diallerPanelSum.compareTo(BigDecimal.ZERO) > 0)
{
if (e.getSource() == buttonMakeCall)
{
diallerPanelSum = diallerPanelSum.subtract(BigDecimal.ONE);
}
if (e.getSource() == buttonSendText)
{
diallerPanelSum = diallerPanelSum.subtract(new BigDecimal("0.10"));
}
if (e.getSource() == buttonTopUp)
{
diallerPanelSum = diallerPanelSum.add(BigDecimal.TEN);
}
}