阅读(1239) (0)

Java Swing SpringLayout

2017-01-09 19:23:21 更新

Java Swing教程 - Java Swing JFormattedTextField


JFormattedTextField是一个具有格式化功能的JTextField。

我们可以指定编辑和显示文本的格式。在值为null的情况下,我们可以指定格式。

JFormattedTextField提供了两个新的方法getValue()和setValue()来接受任何类型的数据,而不仅仅是文本。

JFormattedTextField有三种预配置格式:

  • numbers
  • dates
  • strings

JFormattedTextField有三种预配置格式:...

下表列出了JFormattedTextField类的构造函数。

ID 构造函数/说明
1 JFormattedTextField()创建没有格式化程序的JFormattedTextField。 我们需要使用它的setFormatterFactory()或setValue()方法来设置一个格式化程序。
2 JFormattedTextField(格式格式)创建一个JFormattedTextField,它将使用指定的格式来格式化字段中的文本。
3 JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)使用指定的格式化程序创建JFormattedTextField。
4 JFormattedTextField(JFormattedTextField。AbstractFormatterFactory factory)使用指定的工厂创建JFormattedTextField。
5 JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory,Object initialValue)使用指定的工厂和指定的初始值创建JFormattedTextField。
6 JFormattedTextField(对象值)创建具有指定值的JFormattedTextField。

下表列出了JFormattedTextField类的构造函数。...

格式化程序由JFormattedTextField.AbstractFormatter对象表示,它使用java.text.Format对象格式化对象。

格式化程序工厂是格式化程序的集合。 格式化程序工厂对象由JFormattedTextField.AbstractFormatterFactory类的实例表示。

JFormattedTextField知道基于传递的值类型使用什么默认格式化程序。如果通过setValue()方法传递的值是一个Date时间,它将使用默认值日期格式化器。

以下代码使用当前语言环境格式将其中的文本格式化为日期:

JFormattedTextField  dobField = new JFormattedTextField();
dobField.setValue(new Date());

以下代码以当前语言环境格式格式化一个数字:

JFormattedTextField salaryField  = new JFormattedTextField();
salaryField.setValue(new  Double(12345.98));

我们可以使用格式器创建一个JFormattedTextField。 我们可以使用DateFormatter,NumberFormatter和MaskFormatter类来分别格式化日期,数字和字符串。

这些类位于javax.swing.text包中。

这些类位于javax.swing.text包中。...

DateFormat  dateFormat = new SimpleDateFormat("mm/dd/yyyy"); 
DateFormatter dateFormatter  = new DateFormatter(dateFormat); 
dobField = new JFormattedTextField(dateFormatter);

以下代码创建一个JFormattedTextField以格式化$#0,000.00格式的数字。

NumberFormat numFormat = new DecimalFormat("$#0,000.00"); 
NumberFormatter  numFormatter  = new NumberFormatter(numFormat); 
salaryField = new JFormattedTextField(numFormatter);


自定义格式

下表列出了可用于创建自定义格式的特殊字符。

字符 描述
# 一个号码
? 一封信
A 一封信...
* Anything
U 一个字母,小写字符映射到它们的大写等价物
L 一个字母,小写字符映射到它们的大写等价物...
H 十六进制数字(A-F,a-f,0-9)
" 十六进制数字(A-F,a-f,0-9)...

以下代码以### - ## - ####格式创建社会安全号码的格式。

MaskFormatter ssnFormatter  = null; 
JFormattedTextField ssnField  = null; 
try  {
    ssnFormatter = new MaskFormatter("###-##-####");
    ssnField = new JFormattedTextField(ssnFormatter);
}
catch (ParseException e)  {
    e.printStackTrace();
}

上面的代码不会显示任何占位符掩码。 它显示空间和“ - - "。

要在SNN字段中显示000-00-0000,我们需要使用“0"作为mast格式器的占位符字符。

ssnFormatter = new MaskFormatter("###-##-####");
ssnFormatter.setPlaceholderCharacter("0");


格式化

我们可以使用JFormattedTextField中的setFormatterFactory()方法安装格式化程序。

DateFormatter df  = new DateFormatter(new SimpleDateFormat("mm/dd/yyyy")); DefaultFormatterFactory dff  = new DefaultFormatterFactory(df, df, df, df); dobField.setFormatterFactory(dff);

JFormattedTextField可以接受四种类型的格式化程序:

  • A null formatter: used when the value in the field is null.
  • An edit Formatter: used when the field has focus.
  • A display Formatter: used when the field does not have focus with a non-null value.
  • A default Formatter: used in the absence of any of the above three formatters.

以下代码显示如何为a安装differents格式化程序JFormattedTextField。

DateFormatter df  = new DateFormatter(new SimpleDateFormat("mmmm dd, yyyy")); 
DateFormatter edf  = new DateFormatter(new SimpleDateFormat("mm/dd/yyyy")); 

DefaultFormatterFactory ddf  = new DefaultFormatterFactory(df, df, edf, df); dobField.setFormatterFactory(ddf);

如果我们已经配置了JFormattedTextField来设置日期格式,我们可以使用它的getValue()方法来获取一个Date对象。

要在输入时覆盖字段中的值,我们需要使用setOverwriteMode(true)方法在重写模式下设置格式化程序。

使用JFormattedTextField来限制字符数可以在字段中输入,用* mask设置掩码格式化程序。

要只接受两个字符,请使用以下代码

JFormattedTextField  twoCharField = new JFormattedTextField(new MaskFormatter("**"));