原子一号
The number one of atom
posts - 6, comments - 0, trackbacks - 0, articles - 0
BlogJava
::
首页
::
新随笔
::
联系
::
聚合
::
管理
自扩展JTextField-2
Posted on 2006-10-19 15:03
迟到的鱼
阅读(208)
评论(0)
编辑
收藏
1
import
java.awt.
*
;
2
import
javax.swing.
*
;
3
import
javax.swing.text.
*
;
4
import
java.awt.event.
*
;
5
6
public
class
TextFieldDemo
extends
JFrame
7
{
8
public
TextFieldDemo()
9
{
10
11
Container contentPane
=
this
.getContentPane();
12
13
contentPane.setLayout(
new
FlowLayout());
14
//
内容窗格容器中加入两个文本组件,第一个只能输入数字
15
DigitalText txtDigitalText
=
new
DigitalText(
10
);
16
JTextField txtNormal
=
new
JTextField(
10
);
17
//
JTextArea txtNormal=new JTextArea();
18
contentPane.add(
new
JLabel(
"
数字文本框
"
));
19
contentPane.add(txtDigitalText);
20
contentPane.add(
new
JLabel(
"
任意文本框
"
));
21
contentPane.add(txtNormal);
22
23
pack();
24
setVisible(
true
);
25
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
26
}
27
28
public
static
void
main(String args[])
29
{
30
TextFieldDemo frame
=
new
TextFieldDemo();
31
}
32
}
33
//
该类从JTextField中派生,只允许用户输入数字
34
class
DigitalText
extends
JTextField
35
{
36
public
DigitalText()
37
{
38
}
39
public
DigitalText(
int
column)
40
{
41
super
(column);
42
}
43
public
DigitalText(String strText,
int
column)
44
{
45
super
(strText,column);
46
}
47
protected
Document createDefaultModel()
48
{
49
return
new
DigitalTextDocument();
50
}
51
52
class
DigitalTextDocument
extends
PlainDocument
53
{
54
public
void
insertString(
int
offs, String str, AttributeSet a)
throws
BadLocationException
55
{
56
if
( str
==
null
)
return
;
57
try
58
{
59
Integer.parseInt(str);
60
super
.insertString(offs,str,a);
61
}
62
catch
( NumberFormatException e)
63
{
64
JOptionPane.showMessageDialog(
null
,
"
请输入数字。
"
,
"
验证错误
"
,JOptionPane.ERROR_MESSAGE);
65
}
66
}
67
}
68
}
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
Powered by:
BlogJava
Copyright © 迟到的鱼
日历
<
2006年10月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
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
1
2
3
4
常用链接
我的随笔
我的评论
我的参与
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔档案
2006年10月 (6)
相册
Java文件流
搜索
最新评论
阅读排行榜
1. Java文件流的封装(450)
2. 观感(212)
3. 自扩展JTextField-2(208)
4. 自扩展JTextField-1(200)
5. 向量-2(199)
评论排行榜
1. Java文件流的封装(0)
2. 观感(0)
3. 自扩展JTextField-2(0)
4. 自扩展JTextField-1(0)
5. 向量-2(0)