e997. creating a text field to display and edit a phone number.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 26 行

TXT
26
字号
This example uses a JFormattedTextField to allow the display and editing of certain fixed-string patterns. By default, when the component loses the focus and the modified value is valid, the modified value is saved. Otherwise, if the modified value is not valid, the modified value is discarded and the old value is displayed. 
The pattern is specified using one of the following characters: # represents a decimal digit, H represents a hex digit, U represents an uppercase letter, L represents a lowercase letter, A represents a number or letter, ? represents a letter in any case, and * represents any character. Any other character in the pattern represents itself. If it is necessary to use one of the special characters, it can be escaped by preceding it with a quote ('). 

    MaskFormatter fmt = null;
    
    // A phone number
    try {
        fmt = new MaskFormatter("###-###-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft1 = new JFormattedTextField(fmt);
    
    
    // A social security number
    try {
        fmt = new MaskFormatter("###-##-####");
    } catch (java.text.ParseException e) {
    }
    JFormattedTextField tft2 = new JFormattedTextField(fmt);

The spot where a character or digit is expected is called a placeholder. By default, a placeholder is represented with a space character. The space is automatically replaced as the user fills in the field. This example demonstrates how to use an asterisk as the placeholder character. 
    // A social security number
    fmt.setPlaceholderCharacter('*');
    JFormattedTextField tft3 = new JFormattedTextField(fmt);

⌨️ 快捷键说明

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