?? inputverificationdemo.java
字號:
/* * Copyright (c) 1995 - 2008 Sun Microsystems, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * - Neither the name of Sun Microsystems nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package misc;import java.util.Set;import java.util.HashSet;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.*;/** * InputVerificationDemo.java requires no other files. * * Yet another mortgage calculator. * However, instead of using a formatted text field, * as shown in FormattedTextFieldDemo, this example * uses input verification to validate user input. */public class InputVerificationDemo extends JPanel { //Default values private static double DEFAULT_AMOUNT = 100000; private static double DEFAULT_RATE = 7.5; //7.5% private static int DEFAULT_PERIOD = 30; //Labels to identify the text fields private JLabel amountLabel; private JLabel rateLabel; private JLabel numPeriodsLabel; private JLabel paymentLabel; //Strings for the labels private static String amountString = "Loan Amount (10,000-10,000,000): "; private static String rateString = "APR (>= 0%): "; private static String numPeriodsString = "Years (1-40): "; private static String paymentString = "Monthly Payment: "; //Text fields for data entry private JTextField amountField; private JTextField rateField; private JTextField numPeriodsField; private JTextField paymentField; //Formats to format and parse numbers private NumberFormat moneyFormat; private NumberFormat percentFormat; private DecimalFormat decimalFormat; private DecimalFormat paymentFormat; private MyVerifier verifier = new MyVerifier(); public InputVerificationDemo() { super(new BorderLayout()); setUpFormats(); double payment = computePayment(DEFAULT_AMOUNT, DEFAULT_RATE, DEFAULT_PERIOD); //Create the labels. amountLabel = new JLabel(amountString); rateLabel = new JLabel(rateString); numPeriodsLabel = new JLabel(numPeriodsString); paymentLabel = new JLabel(paymentString); //Create the text fields and set them up. amountField = new JTextField(moneyFormat.format(DEFAULT_AMOUNT), 10); amountField.setInputVerifier(verifier); rateField = new JTextField(percentFormat.format(DEFAULT_RATE), 10); rateField.setInputVerifier(verifier); numPeriodsField = new JTextField(decimalFormat.format(DEFAULT_PERIOD), 10); numPeriodsField.setInputVerifier(verifier); paymentField = new JTextField(paymentFormat.format(payment), 10); paymentField.setInputVerifier(verifier); paymentField.setEditable(false); //Remove this component from the focus cycle. paymentField.setFocusable(false); paymentField.setForeground(Color.red); //Register an action listener to handle Return. amountField.addActionListener(verifier); rateField.addActionListener(verifier); numPeriodsField.addActionListener(verifier); //Tell accessibility tools about label/textfield pairs. amountLabel.setLabelFor(amountField); rateLabel.setLabelFor(rateField); numPeriodsLabel.setLabelFor(numPeriodsField); paymentLabel.setLabelFor(paymentField); //Lay out the labels in a panel. JPanel labelPane = new JPanel(new GridLayout(0,1)); labelPane.add(amountLabel); labelPane.add(rateLabel); labelPane.add(numPeriodsLabel); labelPane.add(paymentLabel); //Layout the text fields in a panel. JPanel fieldPane = new JPanel(new GridLayout(0,1)); fieldPane.add(amountField); fieldPane.add(rateField); fieldPane.add(numPeriodsField); fieldPane.add(paymentField); //Put the panels in this panel, labels on left, //text fields on right. setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20)); add(labelPane, BorderLayout.CENTER); add(fieldPane, BorderLayout.LINE_END); } class MyVerifier extends InputVerifier implements ActionListener { double MIN_AMOUNT = 10000.0; double MAX_AMOUNT = 10000000.0; double MIN_RATE = 0.0; int MIN_PERIOD = 1; int MAX_PERIOD = 40; public boolean shouldYieldFocus(JComponent input) { boolean inputOK = verify(input); makeItPretty(input); updatePayment(); if (inputOK) { return true; } else { Toolkit.getDefaultToolkit().beep(); return false; } } protected void updatePayment() { double amount = DEFAULT_AMOUNT; double rate = DEFAULT_RATE; int numPeriods = DEFAULT_PERIOD; double payment = 0.0; //Parse the values. try { amount = moneyFormat.parse(amountField.getText()). doubleValue(); } catch (ParseException pe) {pe.printStackTrace();} try { rate = percentFormat.parse(rateField.getText()). doubleValue(); } catch (ParseException pe) {pe.printStackTrace();} try { numPeriods = decimalFormat.parse(numPeriodsField.getText()). intValue(); } catch (ParseException pe) {pe.printStackTrace();} //Calculate the result and update the GUI. payment = computePayment(amount, rate, numPeriods); paymentField.setText(paymentFormat.format(payment)); } //This method checks input, but should cause no side effects. public boolean verify(JComponent input) { return checkField(input, false); } protected void makeItPretty(JComponent input) { checkField(input, true); } protected boolean checkField(JComponent input, boolean changeIt) { if (input == amountField) { return checkAmountField(changeIt); } else if (input == rateField) { return checkRateField(changeIt); } else if (input == numPeriodsField) { return checkNumPeriodsField(changeIt); } else {
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -