2012-06-22 46 views
1

我正在写一个简单的贷款计算器与gui使用摆动。我正在使用DecimalFormat来确保JFormattedTextField格式正确。setValue JFormattedTextField不显示在GUI

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中新的价值?

+0

将侦听器添加到按钮的代码在哪里?我关心的是:看起来你是在JFormattedTextField上设置文本,但它是由GUI显示的JFormattedTextField吗?你确定? –

+0

@HovercraftFullOfEels我不太确定。我相信我可能会创建一个新实例,而不是更新正确的实例。但是,我不知道如何引用正在显示的那个。我根据您的要求更新了代码。 – Huy

+0

你的代码没有意义。您的main方法中有一个if块,取决于用户的选择,但用户在运行此代码时不会选择该块。如果这是你的真实代码,你需要重新考虑事件驱动编程的思路。如果这不是你真正的代码,那么请只在这里显示真实的代码。 –

回答

4

通过SSCCE,我的意思是这样的:

import java.awt.event.*; 
import java.text.NumberFormat; 
import javax.swing.*; 

public class TestCalculatorListener extends JPanel { 
    private JFormattedTextField loanAmountField = new JFormattedTextField(
     NumberFormat.getCurrencyInstance()); 

    public TestCalculatorListener() { 
     loanAmountField.setColumns(8); 
     loanAmountField.setEditable(false); 
     loanAmountField.setFocusable(false); 
     add(loanAmountField); 
     add(new JButton(new CalculateListener(loanAmountField))); 
    } 

    private static void createAndShowGui() { 
     TestCalculatorListener mainPanel = new TestCalculatorListener(); 

     JFrame frame = new JFrame("TestCalculatorListener"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGui(); 
     } 
     }); 
    } 

    private class CalculateListener extends AbstractAction { 
     private JFormattedTextField loanAmountField; 

     public CalculateListener(JFormattedTextField loanAmountField) { 
     super("Calculate"); 
     putValue(MNEMONIC_KEY, KeyEvent.VK_C); 
     this.loanAmountField = loanAmountField; 
     } 

     public void actionPerformed(ActionEvent event) { 
     loanAmountField.setValue(new Double(12.22)); 
     } 

    } 
} 

这表明,至少有一些你已经发布的代码的工作正常。

+0

好开始是_ln(2)_完成! – trashgod

+1

能够解决这个问题。我会尝试下次发布SSCCE :) – Huy