1

私はswingを使用してguiで簡単なローン計算機を書いています。JFormattedTextField で正しいフォーマットを確保するために DecimalFormat を使用しています。

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 に新しい値を表示するにはどうすればよいですか?

4

1 に答える 1

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));
      }

   }
}

これは、投稿したコードの少なくとも一部が正常に動作することを示しています。

于 2012-06-22T00:43:20.393 に答える