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

📄 e995. creating a text field to display and edit a number.txt

📁 这里面包含了一百多个JAVA源文件
💻 TXT
字号:
This example uses a JFormattedTextField to allow the display and editing of a number. By default, when the component loses the focus and the modified value is a valid date, the modified value is saved. Otherwise, if the modified value is not a valid date, the modified value is discarded and the old value is displayed. 
    // Support an integer number; if a decimal point is typed,
    // the decimal point and all following characters are discarded
    JFormattedTextField tft1 = new JFormattedTextField(NumberFormat.getIntegerInstance());
    tft1.setValue(new Integer(123));
    
    // Retrieve the value from the text field
    Integer intValue = (Integer)tft1.getValue();
    
    
    // Support a decimal number with one digit following the decimal point;
    // if more digits after the decimal point is typed, the value is rounded to one decimal place
    JFormattedTextField tft2 = new JFormattedTextField(new DecimalFormat("#.0"));
    tft2.setValue(new Float(123.4F));
    
    // Retrieve the value from the text field
    Float floatValue = (Float)tft2.getValue();

By default, if the type of the value is not one of Byte, Short, Integer, Long , Float, or Double, the object is automatically converted to a Double after it has been edited. To prevent this, a custom formatter must be used. This example demonstrates this by using a BigDecimal object. 
    // Support a decimal number with arbitrary number of decimal digits.
    // Actually, this isn't technically possible using DecimalFormat;
    // the best that we can do is to specify lots of #'s
    JFormattedTextField tft3 = new JFormattedTextField(new BigDecimal("123.4567"));
    DefaultFormatter fmt = new NumberFormatter(new DecimalFormat("#.0###############"));
    fmt.setValueClass(tft3.getValue().getClass());
    DefaultFormatterFactory fmtFactory = new DefaultFormatterFactory(fmt, fmt, fmt);
    tft3.setFormatterFactory(fmtFactory);
    
    // Retrieve the value from the text field
    BigDecimal bigValue = (BigDecimal)tft3.getValue();

⌨️ 快捷键说明

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