⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inputverificationdialogdemo.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * 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. */ /* * InputVerificationDialogDemo.java requires no other files. */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.*;/** * InputVerificationDialogDemo.java is a 1.4 example that * 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. * This one uses a dialog to warn people when their input is bad. */public class InputVerificationDialogDemo 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 NumberFormat integerFormat;    private MyVerifier verifier = new MyVerifier();    public InputVerificationDialogDemo() {        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;        String message = null;        public boolean shouldYieldFocus(JComponent input) {            boolean inputOK = verify(input);            makeItPretty(input);            updatePayment();            if (inputOK) {                return true;            }            //Pop up the message dialog.            message += ".\nPlease try again.";            JOptionPane.showMessageDialog(null, //no owner frame                                          message, //text to display                                          "Invalid Value", //title                                          JOptionPane.WARNING_MESSAGE);            //Reinstall the input verifier.            input.setInputVerifier(this);            //Beep and then tell whoever called us that we don't            //want to yield focus.            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) {}            try {                rate = percentFormat.parse(rateField.getText()).                                 doubleValue();            } catch (ParseException pe) {}            try {                numPeriods = decimalFormat.parse(numPeriodsField.getText()).                    intValue();            } catch (ParseException pe) {}            //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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -