wangting
2024-09-27 a3e87f78ee262ca9bb7d9b0c997639d5f3295890
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
54
package com.vci.client.ui.swing.components;
 
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.Serializable;
 
import javax.swing.text.Document;
 
import com.vci.client.ui.swing.components.VCIJTextField;
 
public class VCIJRealTextField extends VCIJTextField implements Serializable {
 
    /**
     * 
     */
    private static final long serialVersionUID = -2711989168936034987L;
    /**
     * 
     */
    
    public VCIJRealTextField(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);
    }
}