我正在使用 swing 编写一个带有 gui 的简单贷款计算器。我正在使用 DecimalFormat 来确保使用 JFormattedTextField 正确格式化。
public static void main(String[] args) {
JFormattedTextField loanAmountField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField interestRateField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField yearField = new JFormattedTextField(new DecimalFormat("####.##"));
JFormattedTextField monthlyPaymentField = new JFormattedTextField(new DecimalFormat("####.##"));
JButton calculateButton = new JButton("Calculate");
//Calculations based on selection
int monthlyTest;
if (monthlyRadioButton.isSelected()){
monthlyTest = 1;
calculateButton.addActionListener(new CalculateListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, monthlyTest));
}
else{
monthlyTest = 0;
calculateButton.addActionListener(new CalculateListener(loanAmountField, interestRateField, yearField, monthlyPaymentField, monthlyTest));
}
}
我遇到的问题是,当我尝试为loanAmountField 分配一个值时,它不会在我的GUI 的JFormattedTextField 上更新它。
class CalculateListener implements ActionListener {
public CalculateListener (JFormattedTextField loanAmountField, JFormattedTextField monthlyPaymentField, JFormattedTextField interestRateField, JFormattedTextField yearField, int monthlyTest)
{
this.interestRateField = interestRateField;
this.yearField = yearField;
this.loanAmountField = loanAmountField;
this.monthlyPaymentField = monthlyPaymentField;
this.monthlyTest = monthlyTest;
}
public void actionPerformed(ActionEvent event){
loanAmountField.setValue(new Double(12.22));
}
}
如何在我的 GUI JFormattedTextField 上显示新值?