?? inputverificationdialogdemo.java
字號:
} 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 { return true; //shouldn't happen } } //Checks that the amount field is valid. If it is valid, //it returns true, otherwise it sets the message field and //returns false. If the change argument is true, set //the textfield to the parsed number so that it looks //good -- no letters, for example. public boolean checkAmountField(boolean change) { boolean wasValid = true; double amount = DEFAULT_AMOUNT; //Parse the value. try { amount = moneyFormat.parse(amountField.getText()). doubleValue(); } catch (ParseException pe) { message = "Invalid money format in Loan Amount field"; return false; } //Value was invalid. if ((amount < MIN_AMOUNT) || (amount > MAX_AMOUNT)) { wasValid = false; if (amount < MIN_AMOUNT) { message = "Loan Amount was < " + integerFormat.format(MIN_AMOUNT); } else { //amount is greater than MAX_AMOUNT message = "Loan Amount was > " + integerFormat.format(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 it sets the message field and //returns false. If the change argument is true, set the //textfield to the parsed number so that it looks good -- no //letters, for example. public boolean checkRateField(boolean change) { boolean wasValid = true; double rate = DEFAULT_RATE; //Parse the value. try { rate = percentFormat.parse(rateField.getText()). doubleValue(); } catch (ParseException pe) { message = "Invalid percent format in APR field"; return false; } //Value was invalid. if (rate < MIN_RATE) { wasValid = false; message = "Bad value: APR was < " + 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 it sets the message field and //returns false. If the change argument is true, set the //textfield to the parsed number so that it looks good -- no //letters, for example. public boolean checkNumPeriodsField(boolean change) { boolean wasValid = true; int numPeriods = DEFAULT_PERIOD; //Parse the value. try { numPeriods = decimalFormat.parse(numPeriodsField.getText()). intValue(); } catch (ParseException pe) { message = "Invalid decimal format in Years field"; return false; } //Value was invalid. if (numPeriods < MIN_PERIOD) { wasValid = false; message = "Bad value: Number of years was < " + integerFormat.format(MIN_PERIOD); } else if (numPeriods > MAX_PERIOD) { wasValid = false; message = "Bad value: Number of years was > " + integerFormat.format(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(); } } /** * 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("InputVerificationDialogDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JComponent newContentPane = new InputVerificationDialogDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); //Display the window. frame.pack(); frame.setVisible(true); } 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(); } }); } //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 also //parse numbers input by user. 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(")"); integerFormat = NumberFormat.getIntegerInstance(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -