?
??1
import
?java.awt.event.KeyEvent;
??2
import
?java.awt.event.KeyListener;
??3
??4
import
?javax.swing.JTextField;
??5
import
?javax.swing.event.CaretEvent;
??6
import
?javax.swing.event.CaretListener;
??7
import
?javax.swing.text.AttributeSet;
??8
import
?javax.swing.text.BadLocationException;
??9
import
?javax.swing.text.PlainDocument;
?10
?11
public
?
class
?NumberTextFieldDocument?
extends
?PlainDocument?
implements
?KeyListener,?CaretListener
{
?12
????
?13
????
int
?integerLength?
=
?
-
1
;
?14
????
int
?fractionLength?
=
?
-
1
;
?15
????
double
?MaxValue,?MinValue;
?16
????
boolean
?useBound?
=
?
false
;
?17
????String?correctValue?
=
?
""
;
?18
????
private
?
int
?caretposold;
?19
????
private
?
int
?caretpos;
?20
????
?21
????
/**?*/
/**
?22
?????*?
?23
?????*
?24
?????
*/
?25
????
public
?NumberTextFieldDocument()?
{
?26
????????
super
();
?27
????}
?28
????
?29
????
/**?*/
/**
?30
?????*?
?31
?????*?
@param
?maxValue
?32
?????*?
@param
?minValue
?33
?????
*/
?34
????
public
?NumberTextFieldDocument(
double
?maxValue,?
double
?minValue)?
{
?35
????????
super
();
?36
????????setBound(maxValue,?minValue);
?37
????}
?38
????
?39
????
/**?*/
/**
?40
?????*?
?41
?????*?
@param
?integerLength
?42
?????*?
@param
?fractionLength
?43
?????
*/
?44
????
public
?NumberTextFieldDocument(
int
?integerLength,?
int
?fractionLength)?
{
?45
????????
super
();
?46
????????setLength(integerLength,?fractionLength);
?47
????}
?48
????
/**?*/
/**
?49
?????*?
?50
?????*?
@param
?integerLength
?51
?????*?
@param
?fractionLength
?52
?????
*/
?53
????
public
?
void
?setLength(
int
?integerLength,?
int
?fractionLength)?
{
?54
????????
if
?(integerLength?
<
?
1
?
||
?fractionLength?
<
?
0
)?
{
?55
//
????????????Error
?56
????????}
?
else
?
{
?57
????????????
this
.integerLength?
=
?integerLength;
?58
????????????
this
.fractionLength?
=
?fractionLength;
?59
????????}
?60
????}
?61
????
?62
????
/**?*/
/**
?63
?????*?
?64
?????*?
@param
?maxValue
?65
?????*?
@param
?minValue
?66
?????
*/
?67
????
public
?
void
?setBound(
double
?maxValue,?
double
?minValue)?
{
?68
????????
if
?(maxValue?
<
?minValue)?
?69
????????????
return
;
?70
????????MaxValue?
=
?maxValue;
?71
????????MinValue?
=
?minValue;
?72
????????useBound?
=
?
true
;
?73
????}
?74
?75
????
?76
????
public
?
void
?insertString(
int
?offs,?String?str,?AttributeSet?a)?
throws
?BadLocationException?
{
?77
????????
?78
????????String?oldtxt?
=
?getText(
0
,?getLength());
?79
????????String?newtxt?
=
?oldtxt.substring(
0
,?offs)?
+
?str?
+
?oldtxt.substring(offs);
?80
????????
boolean
?strRight?
=
?testString(oldtxt);
?81
????????
boolean
?newtxtRight?
=
?testString(newtxt);
?82
????????
if
?(strRight?
&&
?
!
newtxtRight)
{
?83
????????????
return
;
?84
????????}
?
else
?
if
?(
!
strRight?
&&
?
!
newtxtRight)?
{
?85
????????????
int
?rlen?
=
?correctValue.length();
?86
????????????
int
?flen?
=
?oldtxt.length();
?87
????????????
int
?sublen?
=
?rlen?
-
?flen;
?88
????????????
for
?(
int
?i?
=
?
0
;?i?
<
?flen;?i
++
)?
{
?89
????????????????String?substr?
=
?correctValue.substring(
0
,?i)?
+
?correctValue.substring(i?
+
?sublen);
?90
????????????????
if
?(substr.equals(oldtxt))?
{
?91
????????????????????
super
.insertString(i,?correctValue.substring(i,?i?
+
?sublen),?a);
?92
????????????????????correctValue?
=
?String.copyValueOf(getText(
0
,?getLength()).toCharArray());
?93
????????????????????
return
;
?94
????????????????}
?95
????????????}
?96
????????????
return
;
?97
????????}
?98
????????
?99
????????
super
.insertString(offs,?str,?a);
100
????????correctValue?
=
?String.copyValueOf(getText(
0
,?getLength()).toCharArray());
101
????????
102
????}
103
????
104
????
public
?
void
?caretUpdate(CaretEvent?e)?
{
105
????????caretposold?
=
?caretpos;
106
????????caretpos?
=
?e.getDot();
107
//
????????System.out.print("?"?+?caretpos);
108
????}
109
????
110
????
public
?
void
?keyTyped(KeyEvent?e)?
{
111
????????
112
????}
113
114
????
public
?
void
?keyPressed(KeyEvent?e)?
{
115
????????
if
?(e.getKeyCode()?
==
?KeyEvent.VK_BACK_SPACE?
||
?e.getKeyCode()?
==
?KeyEvent.VK_DELETE)?
{
116
????????????JTextField?field?
=
?(JTextField)?e.getSource();
117
????????????
try
?
{
118
????????????????String?nowtxt?
=
?getText(
0
,?getLength());
119
????????????????String?selecttxt?
=
?field.getSelectedText();
120
????????????????
if
?(correctValue?
==
?
null
?
||
?(selecttxt?
!=
?
null
?
&&
?selecttxt.equals(correctValue)))?
{
121
????????????????????correctValue?
=
?
null
;
122
????????????????????
return
;
123
????????????????}
124
????????????????
if
?(
!
testString(nowtxt))?
{
125
????????????????????
int
?nowpos?
=
?
126
????????????????????caretposold?
-
?caretpos?
==
?correctValue.length()?
-
?nowtxt.length()?
?
?caretposold?:?caretpos;
127
????????????????????remove(
0
,?getLength());
128
????????????????????insertString(
0
,?correctValue,?
null
);
129
????????????????????field.setCaretPosition(nowpos);
130
????????????????}
?
else
?
{
131
????????????????????correctValue?
=
?nowtxt;
132
????????????????}
133
????????????}
?
catch
?(BadLocationException?e1)?
{
134
????????????????System.out.println(e1);
135
????????????}
136
????????}
137
????}
138
139
????
public
?
void
?keyReleased(KeyEvent?e)?
{
140
????????
if
?(getLength()?
==
?
0
)?
{
141
????????????
return
;
142
????????}
143
????????
if
?(e.getKeyCode()?
==
?KeyEvent.VK_BACK_SPACE?
||
?e.getKeyCode()?
==
?KeyEvent.VK_DELETE)?
{
144
????????????JTextField?field?
=
?(JTextField)?e.getSource();
145
????????????
try
?
{
146
????????????????String?nowtxt?
=
?getText(
0
,?getLength());
147
????????????????String?selecttxt?
=
?field.getSelectedText();
148
????????????????
if
?(correctValue?
==
?
null
?
||
?(selecttxt?
!=
?
null
?
&&
?selecttxt.equals(correctValue)))?
{
149
????????????????????correctValue?
=
?
null
;
150
????????????????????
return
;
151
????????????????}
152
????????????????
if
?(
!
testString(nowtxt))?
{
153
????????????????????
int
?nowpos?
=
?
154
????????????????????caretposold?
-
?caretpos?
==
?correctValue.length()?
-
?nowtxt.length()?
?
?caretposold?:?caretpos;
155
????????????????????remove(
0
,?getLength());
156
????????????????????insertString(
0
,?correctValue,?
null
);
157
????????????????????field.setCaretPosition(nowpos);
158
????????????????}
?
else
?
{
159
????????????????????correctValue?
=
?nowtxt;
160
????????????????}
161
????????????}
?
catch
?(BadLocationException?e1)?
{
162
????????????????System.out.println(e1);
163
????????????}
164
????????}
165
????}
166
????
167
????
private
?
boolean
?testString(String?waitfortest)?
{
168
????????
int
?dotindex?
=
?waitfortest.indexOf(
"
.
"
);
169
????????
int
?strLength?
=
?waitfortest.length();
170
????????
if
?(integerLength?
!=
?
-
1
?
&&
?fractionLength?
!=
?
-
1
)?
{
171
????????????
if
?(fractionLength?
==
?
0
?
&&
?dotindex?
>=
?
0
)?
{
172
????????????????
return
?
false
;
173
????????????}
174
????????????
if
?(dotindex?
>
?
0
?
&&
?dotindex?
!=
?strLength?
-
?
1
)?
{
175
????????????????String[]?splitstrs?
=
?waitfortest.split(
"
\\.
"
);
176
????????????????
if
?(splitstrs.length?
!=
?
2
)?
{
//
impossible
177
????????????????????
return
?
false
;
178
????????????????}
?
else
?
if
?(splitstrs[
0
].length()?
>
?integerLength?
||
?splitstrs[
1
].length()?
>
?fractionLength)?
{
179
????????????????????
return
?
false
;
180
????????????????}
181
????????????}
?
else
?
if
?((dotindex?
==
?
0
?
&&
?strLength?
>
?fractionLength?
+
?
1
)?
182
????????????????????
||
?(dotindex?
==
?strLength?
-
?
1
?
&&
?strLength?
>
?integerLength?
+
?
1
)
183
????????????????????
||
?(dotindex?
<
?
0
?
&&
?strLength?
>
?integerLength))?
{
184
????????????????
return
?
false
;
185
????????????}
186
????????}
187
188
????????waitfortest?
=
?waitfortest.toLowerCase();
189
????????
if
?(waitfortest.indexOf(
"
?
"
)?
>=
?
0
?
||
?waitfortest.indexOf(
"
d
"
)?
>=
?
0
?
||
?waitfortest.indexOf(
"
f
"
)?
>=
?
0
?)?
{
190
????????????
return
?
false
;
191
????????}
192
????????
try
?
{
193
????????????
if
?(dotindex?
==
?
0
)?
{
194
????????????????waitfortest?
=
?
"
0
"
?
+
?waitfortest;
195
????????????}
196
????????????
double
?num?
=
?Double.parseDouble(waitfortest);
197
????????????
if
?(useBound?
&&
?(num?
>
?MaxValue?
||
?num?
<
?MinValue))?
{
198
????????????????
return
?
false
;
199
????????????}
200
????????}
?
catch
?(Exception?e)?
{
201
????????????
return
?
false
;
202
????????}
203
????????
return
?
true
;
204
????}
205
????
206
}
207

??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



?55

?56



?57

?58

?59

?60

?61

?62


?63

?64

?65

?66

?67



?68

?69

?70

?71

?72

?73

?74

?75

?76



?77

?78

?79

?80

?81

?82



?83

?84



?85

?86

?87

?88



?89

?90



?91

?92

?93

?94

?95

?96

?97

?98

?99

100

101

102

103

104



105

106

107

108

109

110



111

112

113

114



115



116

117



118

119

120



121

122

123

124



125

126

127

128

129

130



131

132

133



134

135

136

137

138

139



140



141

142

143



144

145



146

147

148



149

150

151

152



153

154

155

156

157

158



159

160

161



162

163

164

165

166

167



168

169

170



171



172

173

174



175

176



177

178



179

180

181

182

183



184

185

186

187

188

189



190

191

192



193



194

195

196

197



198

199

200



201

202

203

204

205

206

207

下面是Test
?1
import?javax.swing.*;
?2
?3
public?class?NumberTextFieldDocumentTest?
{
?4
????public?static?void?main(String[]?args)?
{
?5
????????JFrame?f?=?new?JFrame("TextDocumentTest");
?6
????????JPanel?panel?=?new?JPanel();
?7
????????f.getContentPane().add(panel);
?8
????????JTextField?txt?=?new?JTextField();
?9
????????txt.setColumns(20);
10
????????
11
????????NumberTextFieldDocument?doc?=?new?NumberTextFieldDocument();
12
????????doc.setBound(99999,?0.00001);
13
????????doc.setLength(5,?5);
14
????????txt.setDocument(doc);
15
????????txt.addKeyListener(doc);
16
????????txt.addCaretListener(doc);
17
????????
18
????????panel.add(txt);
19
????????f.pack();
20
????????f.setVisible(true);
21
????????f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
22
????????txt.setText("123.123");
23
????????
24
????}
25
}
26

?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
