Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Re:怎样使用JFormattedTextField??????? [Re:华山一剑]
sc33



版主


发贴: 128
积分: 30
于 2003-10-31 09:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
虽然 JFormattedTextField 组件看起来与 JTextField 相似,但是它的行为与 JSpinner 完全不同。在最简单的情况下,您可以为电话号码提供一个类似“(###)###-####”的输入掩码,它不会接受任何不遵循那个格式的输入。在较为复杂的情况下,既有显示格式化器,也有输入格式化器。例如:编辑时,缺省日期格式化器允许根据光标的位置在可用的月或日之间滚动。

当使用 JFormattedTextField 时,可接受的输入或者是由掩码明确指定,或者是由组件的一个值指定。在后一种情况下,组件用工厂(Factory)设计模式来查找指定值类的缺省格式化器。DefaultFormatterFactory 组件提供预先安装的日期、数字、java.text.Format 子类的格式化器以及其他一切包罗万象的格式化器。

让我们看看如何使用组件。

配置可接受的输入
屏蔽输入一般是通过使用 MaskFormatter 类的一个实例配置的。在 javax.swing.text 包中发现,MaskFormatter 通过使用一系列字符指定可接受的输入来工作。该系列 8 个字符中的每一个都代表输入中的一个字符,下面的列表指出了这一点:

# 一个数字
? 一个字母
A 一个字母或数字
* 任意字符
U 一个字母,小写字符映射到与它们等效的大写字符上
L 一个字母,大写字符映射到与它们等效的小写字符上
H 一个十六进制数字(A-F、a-f、0-9)
' 用来转义另外一个掩码字符

除了 MaskFormatter 之外,您还可以用来自 java.text 软件包的 DateFormat 和 NumberFormat 类指定输入格式。清单 1 显示了一些可能的格式。

清单 1. 定义输入掩码
// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format =
new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
// US Social Security number
MaskFormatter mf1 =
new MaskFormatter("###-##-####");
// US telephone number
MaskFormatter mf2 =
new MaskFormatter("(###) ###-####");

一旦您指定了输入格式,您随后就要将格式化器传入 JFormattedTextField 构造器中,如下所示:

JFormattedTextField ftf1 = new
JFormattedTextField(df);

还有其它一些可配置的选项,它们取决于您使用的格式化器。例如:用 MaskFormatter,您能用 setPlaceholderCharacter(char) 设置占位符字符。另外,对于日期域,如果您将域初始化为某个值使一个用户知道什么样的输入格式是可接受的,这样将会有所帮助。

全部组合在一起
创建屏蔽输入域的一切都已就绪。清单 2 通过把以前的代码片断组合在一起,为您提供了一个用于检验新性能的完整示例。随便调整各个掩码来检验其他的掩码字符。

清单 2. JFormattedTextField 示例
import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;
import java.text.*;

public class FormattedSample {
public static void main (String args[]) throws ParseException {
JFrame f = new JFrame("JFormattedTextField Sample");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container content = f.getContentPane();
content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));
// Four-digit year, followed by month name and day of month,
// each separated by two dashes (--)
DateFormat format =
new SimpleDateFormat("yyyy--MMMM--dd");
DateFormatter df = new DateFormatter(format);
JFormattedTextField ftf1 = new
JFormattedTextField(df);
ftf1.setValue(new Date());
content.add(ftf1);
// US Social Security number
MaskFormatter mf1 =
new MaskFormatter("###-##-####");
mf1.setPlaceholderCharacter('_');
JFormattedTextField ftf2 = new
JFormattedTextField(mf1);
content.add(ftf2);
// US telephone number
MaskFormatter mf2 =
new MaskFormatter("(###) ###-####");
JFormattedTextField ftf3 = new
JFormattedTextField(mf2);
content.add(ftf3);
f.setSize(300, 100);
f.show();
}
}




话题树型展开
人气 标题 作者 字数 发贴时间
7455 怎样使用JFormattedTextField??????? 华山一剑 0 2003-10-30 17:07
5853 Re:怎样使用JFormattedTextField??????? sc33 2838 2003-10-31 09:33
5717 Re:sc33 华山一剑 83 2003-11-10 10:08

flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923