Swing在JFormattedTextField中使用貨幣格式

2019-10-16 22:04:50

下面的範例展示如何建立和使用JFormattedTextField在基於Swing的應用程式中的文字欄位上指定貨幣格式。

使用以下API -

  • JFormattedTextField - 建立格式化文字欄位。
  • NumberFormat - 為JFormattedTextField提供Percentage格式。
  • NumberFormat.getCurrencyInstance() - 獲取貨幣格式。

範例

package com.yiibai.swingdemo;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.LayoutManager;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class SwingTester implements PropertyChangeListener {

   private static JFormattedTextField principleTextField;
   private static JFormattedTextField rateTextField;
   private static JFormattedTextField yearsTextField;
   private static JFormattedTextField amountTextField;
   public static void main(String[] args) {
      SwingTester tester = new SwingTester();
      createWindow(tester);
   }

   private static void createWindow(SwingTester tester) {
      JFrame frame = new JFrame("Swing JFormattedTextField範例");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      createUI(frame, tester);
      frame.setSize(560, 200);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }

   private static void createUI(final JFrame frame, SwingTester tester) {
      JPanel panel = new JPanel();
      LayoutManager layout = new GridLayout(6,2);
      panel.setLayout(layout); 
      panel.setSize(300, 200);
      panel.setBorder(BorderFactory.createTitledBorder("格式化範例:"));

      NumberFormat principleFormat = NumberFormat.getNumberInstance();
      principleTextField = new JFormattedTextField(principleFormat);
      principleTextField.setName("Principle");
      principleTextField.setColumns(10);
      JLabel principleLabel = new JLabel("原金:");
      principleLabel.setLabelFor(principleTextField);
      principleTextField.setValue(new Double(199999));
      principleTextField.addPropertyChangeListener("value", tester);

      NumberFormat rateFormat = NumberFormat.getPercentInstance();
      rateFormat.setMinimumFractionDigits(2);
      rateTextField = new JFormattedTextField(rateFormat);
      rateTextField.setName("Rate");
      rateTextField.setColumns(10);
      JLabel rateLabel = new JLabel("利率:");
      rateLabel.setLabelFor(rateTextField);
      rateTextField.setValue(new Double(0.1));
      rateTextField.addPropertyChangeListener("value", tester);
      yearsTextField = new JFormattedTextField();
      yearsTextField.setName("Years");
      yearsTextField.setColumns(10);
      JLabel yearLabel = new JLabel("年數:");
      yearLabel.setLabelFor(yearsTextField);
      yearsTextField.setValue(new Double(1));
      yearsTextField.addPropertyChangeListener("value", tester);

      NumberFormat amountFormat = NumberFormat.getCurrencyInstance();
      amountTextField = new JFormattedTextField(amountFormat);
      amountTextField.setName("總量");
      amountTextField.setColumns(10);
      amountTextField.setEditable(false);
      JLabel amountLabel = new JLabel("總金量:");
      amountLabel.setLabelFor(amountTextField);
      amountTextField.setValue(new Double(1999999 + 1999999 * 0.10));

      DateFormat dateFormat = new SimpleDateFormat("YYYY-MM-dd");
      JFormattedTextField today = new JFormattedTextField(dateFormat);
      today.setName("今天");
      today.setColumns(10);
      today.setEditable(false);
      JLabel todayLabel = new JLabel("日期:");
      todayLabel.setLabelFor(today);
      today.setValue(new Date());

      panel.add(principleLabel);
      panel.add(principleTextField); 
      panel.add(rateLabel);
      panel.add(rateTextField);
      panel.add(yearLabel);
      panel.add(yearsTextField);
      panel.add(amountLabel);
      panel.add(amountTextField); 
      panel.add(todayLabel);
      panel.add(today); 
      JPanel mainPanel = new JPanel(); 
      mainPanel.add(panel);

      frame.getContentPane().add(mainPanel, BorderLayout.CENTER);
   } 

   @Override
   public void propertyChange(PropertyChangeEvent evt) {
      double amount, rate, years, principle;

      principle = ((Number)principleTextField.getValue()).doubleValue();
      rate = ((Number)rateTextField.getValue()).doubleValue() * 100;
      years = ((Number)yearsTextField.getValue()).doubleValue();

      amount = principle + principle * rate * years / 100;
      amountTextField.setValue(new Double(amount));
   } 
}

執行上面範例程式碼,得到以下結果:

也可以將語言環境傳遞給NumberFormat.getCurrencyInstance()以獲取所需國家/地區的貨幣符號。