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

📄 inputverificationdemo.java

📁 java tutorial.sun公司官方出品。java入门书籍。最新版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                return true; //shouldn't happen            }        }                //Checks that the amount field is valid.  If it is valid,        //it returns true; otherwise, returns false.  If the        //change argument is true, this method reigns in the        //value if necessary and (even if not) sets it to the        //parsed number so that it looks good -- no letters,        //for example.                protected boolean checkAmountField(boolean change) {            boolean wasValid = true;            double amount = DEFAULT_AMOUNT;                        //Parse the value.            try {                amount = moneyFormat.parse(amountField.getText()).                        doubleValue();            } catch (ParseException pe) {                wasValid = false;            }                        //Value was invalid.            if ((amount < MIN_AMOUNT) || (amount > MAX_AMOUNT)) {                wasValid = false;                if (change) {                    if (amount < MIN_AMOUNT) {                        amount = MIN_AMOUNT;                    } else { // amount is greater than MAX_AMOUNT                        amount = MAX_AMOUNT;                    }                }            }                        //Whether value was valid or not, format it nicely.            if (change) {                amountField.setText(moneyFormat.format(amount));                amountField.selectAll();            }                        return wasValid;        }                //Checks that the rate field is valid.  If it is valid,        //it returns true; otherwise, returns false.  If the        //change argument is true, this method reigns in the        //value if necessary and (even if not) sets it to the        //parsed number so that it looks good -- no letters,        //for example.        protected boolean checkRateField(boolean change) {            boolean wasValid = true;            double rate = DEFAULT_RATE;                        //Parse the value.            try {                rate = percentFormat.parse(rateField.getText()).                        doubleValue();            } catch (ParseException pe) {                wasValid = false;                pe.printStackTrace();            }                        //Value was invalid.            if (rate < MIN_RATE) {                wasValid = false;                if (change) {                    rate = MIN_RATE;                }            }                        //Whether value was valid or not, format it nicely.            if (change) {                rateField.setText(percentFormat.format(rate));                rateField.selectAll();            }                        return wasValid;        }                //Checks that the numPeriods field is valid.  If it is valid,        //it returns true; otherwise, returns false.  If the        //change argument is true, this method reigns in the        //value if necessary and (even if not) sets it to the        //parsed number so that it looks good -- no letters,        //for example.        protected boolean checkNumPeriodsField(boolean change) {            boolean wasValid = true;            int numPeriods = DEFAULT_PERIOD;                        //Parse the value.            try {                numPeriods = decimalFormat.parse(numPeriodsField.getText()).                        intValue();            } catch (ParseException pe) {                wasValid = false;                pe.printStackTrace();            }                        //Value was invalid.            if ((numPeriods < MIN_PERIOD) || (numPeriods > MAX_PERIOD)) {                wasValid = false;                if (change) {                    if (numPeriods < MIN_PERIOD) {                        numPeriods = MIN_PERIOD;                    } else { // numPeriods is greater than MAX_PERIOD                        numPeriods = MAX_PERIOD;                    }                }            }                        //Whether value was valid or not, format it nicely.            if (change) {                numPeriodsField.setText(decimalFormat.format(numPeriods));                numPeriodsField.selectAll();            }                        return wasValid;        }                public void actionPerformed(ActionEvent e) {            JTextField source = (JTextField)e.getSource();            shouldYieldFocus(source); //ignore return value            source.selectAll();        }    }        public static void main(String[] args) {                /* Use an appropriate Look and Feel */        try {            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");            //UIManager.setLookAndFeel("com.sun.java.swing.plaf.gtk.GTKLookAndFeel");            UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");        } catch (UnsupportedLookAndFeelException ex) {            ex.printStackTrace();        } catch (IllegalAccessException ex) {            ex.printStackTrace();        } catch (InstantiationException ex) {            ex.printStackTrace();        } catch (ClassNotFoundException ex) {            ex.printStackTrace();        }        /* Turn off metal's use of bold fonts */        UIManager.put("swing.boldMetal", Boolean.FALSE);                //Schedule a job for the event-dispatching thread:        //creating and showing this application's GUI.        javax.swing.SwingUtilities.invokeLater(new Runnable() {            public void run() {                createAndShowGUI();            }        });    }        /**     * Create the GUI and show it.  For thread safety,     * this method should be invoked from the     * event-dispatching thread.     */    private static void createAndShowGUI() {        //Create and set up the window.        JFrame frame = new JFrame("InputVerificationDemo");        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);                //Create and set up the content pane.        JComponent newContentPane = new InputVerificationDemo();        newContentPane.setOpaque(true); //content panes must be opaque        frame.setContentPane(newContentPane);                //Display the window.        frame.pack();        frame.setVisible(true);    }        //Compute the monthly payment based on the loan amount,    //APR, and length of loan.    double computePayment(double loanAmt, double rate, int numPeriods) {        double I, partial1, denominator, answer;                numPeriods *= 12;        //get number of months        if (rate > 0.01) {            I = rate / 100.0 / 12.0;         //get monthly rate from annual            partial1 = Math.pow((1 + I), (0.0 - numPeriods));            denominator = (1 - partial1) / I;        } else { //rate ~= 0            denominator = numPeriods;        }                answer = (-1 * loanAmt) / denominator;        return answer;    }        //Create and set up number formats. These objects are used    //for both parsing input and formatting output.    private void setUpFormats() {        moneyFormat = (NumberFormat)NumberFormat.getNumberInstance();                percentFormat = NumberFormat.getNumberInstance();        percentFormat.setMinimumFractionDigits(3);                decimalFormat = (DecimalFormat)NumberFormat.getNumberInstance();        decimalFormat.setParseIntegerOnly(true);                paymentFormat = (DecimalFormat)NumberFormat.getNumberInstance();        paymentFormat.setMaximumFractionDigits(2);        paymentFormat.setNegativePrefix("(");        paymentFormat.setNegativeSuffix(")");    }}

⌨️ 快捷键说明

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