2012-06-22 48 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