例1:
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- import javax.swing.event.*;
- import javax.swing.text.*;
- import java.beans.PropertyChangeListener;
- import java.beans.PropertyChangeEvent;
- import java.text.*;
- public class FormatterFactoryDemo extends JPanel
- implements PropertyChangeListener {
-
- private double amount = 100000;
- private double rate = .075;
- private int numPeriods = 30;
-
- private JLabel amountLabel;
- private JLabel rateLabel;
- private JLabel numPeriodsLabel;
- private JLabel paymentLabel;
-
- private static String amountString = "Loan Amount: ";
- private static String rateString = "APR (%): ";
- private static String numPeriodsString = "Years: ";
- private static String paymentString = "Monthly Payment: ";
-
- private JFormattedTextField amountField;
- private JFormattedTextField rateField;
- private JFormattedTextField numPeriodsField;
- private JFormattedTextField paymentField;
-
- private NumberFormat amountDisplayFormat;
- private NumberFormat amountEditFormat;
- private NumberFormat percentDisplayFormat;
- private NumberFormat percentEditFormat;
- private NumberFormat paymentFormat;
- public FormatterFactoryDemo() {
- super(new BorderLayout());
- setUpFormats();
- double payment = computePayment(amount,
- rate,
- numPeriods);
-
- amountLabel = new JLabel(amountString);
- rateLabel = new JLabel(rateString);
- numPeriodsLabel = new JLabel(numPeriodsString);
- paymentLabel = new JLabel(paymentString);
-
- amountField = new JFormattedTextField(
- new DefaultFormatterFactory(
- new NumberFormatter(amountDisplayFormat),
- new NumberFormatter(amountDisplayFormat),
- new NumberFormatter(amountEditFormat)));
- amountField.setValue(new Double(amount));
- amountField.setColumns(10);
- amountField.addPropertyChangeListener("value", this);
- NumberFormatter percentEditFormatter =
- new NumberFormatter(percentEditFormat) {
- public String valueToString(Object o)
- throws ParseException {
- Number number = (Number)o;
- if (number != null) {
- double d = number.doubleValue() * 100.0;
- number = new Double(d);
- }
- return super.valueToString(number);
- }
- public Object stringToValue(String s)
- throws ParseException {
- Number number = (Number)super.stringToValue(s);
- if (number != null) {
- double d = number.doubleValue() / 100.0;
- number = new Double(d);
- }
- return number;
- }
- };
- rateField = new JFormattedTextField(
- new DefaultFormatterFactory(
- new NumberFormatter(percentDisplayFormat),
- new NumberFormatter(percentDisplayFormat),
- percentEditFormatter));
- rateField.setValue(new Double(rate));
- rateField.setColumns(10);
- rateField.addPropertyChangeListener("value", this);
- numPeriodsField = new JFormattedTextField();
- numPeriodsField.setValue(new Integer(numPeriods));
- numPeriodsField.setColumns(10);
- numPeriodsField.addPropertyChangeListener("value", this);
- paymentField = new JFormattedTextField(paymentFormat);
- paymentField.setValue(new Double(payment));
- paymentField.setColumns(10);
- paymentField.setEditable(false);
- paymentField.setForeground(Color.red);
-
- amountLabel.setLabelFor(amountField);
- rateLabel.setLabelFor(rateField);
- numPeriodsLabel.setLabelFor(numPeriodsField);
- paymentLabel.setLabelFor(paymentField);
-
- JPanel labelPane = new JPanel(new GridLayout(0,1));
- labelPane.add(amountLabel);
- labelPane.add(rateLabel);
- labelPane.add(numPeriodsLabel);
- labelPane.add(paymentLabel);
-
- JPanel fieldPane = new JPanel(new GridLayout(0,1));
- fieldPane.add(amountField);
- fieldPane.add(rateField);
- fieldPane.add(numPeriodsField);
- fieldPane.add(paymentField);
-
-
- setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
- add(labelPane, BorderLayout.CENTER);
- add(fieldPane, BorderLayout.LINE_END);
- }
-
- public void propertyChange(PropertyChangeEvent e) {
- Object source = e.getSource();
- if (source == amountField) {
- amount = ((Number)amountField.getValue()).doubleValue();
- } else if (source == rateField) {
- rate = ((Number)rateField.getValue()).doubleValue();
- } else if (source == numPeriodsField) {
- numPeriods = ((Number)numPeriodsField.getValue()).intValue();
- }
- double payment = computePayment(amount, rate, numPeriods);
- paymentField.setValue(new Double(payment));
- }
-
- private static void createAndShowGUI() {
-
- JFrame frame = new JFrame("FormatterFactoryDemo");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
-
- JComponent newContentPane = new FormatterFactoryDemo();
- newContentPane.setOpaque(true);
- frame.setContentPane(newContentPane);
-
- frame.pack();
- frame.setVisible(true);
- }
- public static void main(String[] args) {
-
-
- javax.swing.SwingUtilities.invokeLater(new Runnable() {
- public void run() {
- createAndShowGUI();
- }
- });
- }
-
-
- double computePayment(double loanAmt, double rate, int numPeriods) {
- double I, partial1, denominator, answer;
- numPeriods *= 12;
- if (rate > 0.001) {
- I = rate / 12.0;
- partial1 = Math.pow((1 + I), (0.0 - numPeriods));
- denominator = (1 - partial1) / I;
- } else {
- denominator = numPeriods;
- }
- answer = (-1 * loanAmt) / denominator;
- return answer;
- }
-
-
- private void setUpFormats() {
- amountDisplayFormat = NumberFormat.getCurrencyInstance();
- amountDisplayFormat.setMinimumFractionDigits(0);
- amountDisplayFormat.setMaximumFractionDigits(10);
- amountEditFormat = NumberFormat.getNumberInstance();
- amountEditFormat.setMinimumFractionDigits(0);
- amountEditFormat.setMaximumFractionDigits(10)
- percentDisplayFormat = NumberFormat.getPercentInstance();
- percentDisplayFormat.setMinimumFractionDigits(2);
- percentEditFormat = NumberFormat.getNumberInstance();
- percentEditFormat.setMinimumFractionDigits(2);
- paymentFormat = NumberFormat.getCurrencyInstance();
- }
- }
例2
- import java.awt.*;
- import javax.swing.*;
- import javax.swing.text.*;
- import java.util.*;
- import java.text.*;
- public class FormattedSample {
- public static void main (String args[]) throws ParseException {
- JFrame f = new JFrame("JFormattedTextField Sample");
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Container content = f.getContentPane();
- content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
-
-
- DateFormat format =
- new SimpleDateFormat("yyyy--MMMM--dd");
- DateFormatter df = new DateFormatter(format);
- JFormattedTextField ftf1 = new
- JFormattedTextField(df);
- ftf1.setValue(new Date());
- content.add(ftf1);
-
- MaskFormatter mf1 =
- new MaskFormatter("###-##-####");
- mf1.setPlaceholderCharacter('_');
- JFormattedTextField ftf2 = new
- JFormattedTextField(mf1);
- content.add(ftf2);
-
- MaskFormatter mf2 =
- new MaskFormatter("(###) ###-####");
- JFormattedTextField ftf3 = new
- JFormattedTextField(mf2);
- content.add(ftf3);
- f.setSize(300, 100);
- f.show();
- }
- }
发表于 @
2008年08月07日 16:17:00 | | 编辑|
举报| 收藏