xiejun
2025-01-23 9ac3bd680a350c1cc4baad082d92cd2c5f158f3e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.vci.rmip.code.client.codeapply.Apply410.swing;
 
import com.vci.base.ui.swing.components.VCIJTextField;
 
import javax.swing.text.Document;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
 
public class RealTextField extends VCIJTextField {
    /**
     *
     */
    private static final long serialVersionUID = -2711989168936034987L;
 
    /**
     *
     */
 
    public RealTextField(String text) {
        super(text);
        addKeyListener(new KeyAdapter() {
            public void keyReleased(KeyEvent e) {
                this_keyReleased(e);
            }
        });
    }
 
    private void this_keyReleased(KeyEvent e) {
        String text = getText();
        if (text.equals(""))
            return;
        // 数值不能以-.或.开头
        if (text.startsWith("-.")) {
            text = "-0." + text.substring(2);
        } else if (text.startsWith(".")) {
            text = "0." + text.substring(1);
        }
        String str = text.substring(0, text.length() - 1);
        String last = text.substring(text.length() - 1);
        // Real类型的输入时,只能出现一个.
        if (last.equals(".")) {
            if (str.indexOf(".") > 0) {
                text = str;
            }
        }
        setText(text);
    }
 
    @Override
    protected Document createDefaultModel() {
        return new NumberPlainDocument(true);
    }
}