Java Swing SpringLayout

2018-01-09 19:23 更新

Java Swing教程 - Java Swing JFormattedTextField


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

我們可以指定編輯和顯示文本的格式。在值為null的情況下,我們可以指定格式。

JFormattedTextField提供了兩個新的方法getValue()和setValue()來接受任何類型的數(shù)據(jù),而不僅僅是文本。

JFormattedTextField有三種預(yù)配置格式:

  • numbers
  • dates
  • strings

JFormattedTextField有三種預(yù)配置格式:...

下表列出了JFormattedTextField類的構(gòu)造函數(shù)。

ID 構(gòu)造函數(shù)/說明
1 JFormattedTextField()創(chuàng)建沒有格式化程序的JFormattedTextField。 我們需要使用它的setFormatterFactory()或setValue()方法來設(shè)置一個格式化程序。
2 JFormattedTextField(格式格式)創(chuàng)建一個JFormattedTextField,它將使用指定的格式來格式化字段中的文本。
3 JFormattedTextField(JFormattedTextField.AbstractFormatter formatter)使用指定的格式化程序創(chuàng)建JFormattedTextField。
4 JFormattedTextField(JFormattedTextField。AbstractFormatterFactory factory)使用指定的工廠創(chuàng)建JFormattedTextField。
5 JFormattedTextField(JFormattedTextField.AbstractFormatterFactory factory,Object initialValue)使用指定的工廠和指定的初始值創(chuàng)建JFormattedTextField。
6 JFormattedTextField(對象值)創(chuàng)建具有指定值的JFormattedTextField。

下表列出了JFormattedTextField類的構(gòu)造函數(shù)。...

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

格式化程序工廠是格式化程序的集合。 格式化程序工廠對象由JFormattedTextField.AbstractFormatterFactory類的實例表示。

JFormattedTextField知道基于傳遞的值類型使用什么默認格式化程序。如果通過setValue()方法傳遞的值是一個Date時間,它將使用默認值日期格式化器。

以下代碼使用當前語言環(huán)境格式將其中的文本格式化為日期:

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

以下代碼以當前語言環(huán)境格式格式化一個數(shù)字:

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

我們可以使用格式器創(chuàng)建一個JFormattedTextField。 我們可以使用DateFormatter,NumberFormatter和MaskFormatter類來分別格式化日期,數(shù)字和字符串。

這些類位于javax.swing.text包中。

這些類位于javax.swing.text包中。...

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

以下代碼創(chuàng)建一個JFormattedTextField以格式化$#0,000.00格式的數(shù)字。

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


自定義格式

下表列出了可用于創(chuàng)建自定義格式的特殊字符。

字符 描述
# 一個號碼
? 一封信
A 一封信...
* Anything
U 一個字母,小寫字符映射到它們的大寫等價物
L 一個字母,小寫字符映射到它們的大寫等價物...
H 十六進制數(shù)字(A-F,a-f,0-9)
" 十六進制數(shù)字(A-F,a-f,0-9)...

以下代碼以### - ## - ####格式創(chuàng)建社會安全號碼的格式。

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);

如果我們已經(jīng)配置了JFormattedTextField來設(shè)置日期格式,我們可以使用它的getValue()方法來獲取一個Date對象。

要在輸入時覆蓋字段中的值,我們需要使用setOverwriteMode(true)方法在重寫模式下設(shè)置格式化程序。

使用JFormattedTextField來限制字符數(shù)可以在字段中輸入,用* mask設(shè)置掩碼格式化程序。

要只接受兩個字符,請使用以下代碼

JFormattedTextField  twoCharField = new JFormattedTextField(new MaskFormatter("**"));
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號