简体   繁体   English

setValue到JFormattedTextField没有在GUI上显示

[英]setValue to JFormattedTextField does not show on GUI

I'm writing a simple loan calculator with gui using swing. 我正在写一个简单的贷款计算器与gui使用swing。 I am using DecimalFormat to ensure correct formatting wtih JFormattedTextField . 我正在使用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));

    }
}

The problem I'm having is that when I try to assign a value to loanAmountField, it doesn't update it on my GUI's JFormattedTextField. 我遇到的问题是,当我尝试为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));
    }

}

How do I display the new value on my GUI JFormattedTextField? 如何在GUI JFormattedTextField上显示新值?

By SSCCE , I mean something like this: 通过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));
      }

   }
}

Which shows that at least some of the code you've posted works fine. 这表明您发布的代码中至少有一些工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM