JFormattedTextField的应用

来源:互联网 发布:linux性能监控命令 编辑:程序博客网 时间:2024/04/27 16:37

JFormattedTextField添加了对格式化的任意的值,当使用 JFormattedTextField 时,可接受的输入或者是由掩码明确指定,或者是由组件的一个值指定。在后一种情况下,组件用工厂(Factory)设计模式来查找指定值类的缺省格式化器。比如Java.text.format的子类,或者DefaultFormatterFactory组件提供的格式化器。

以下的demo用SimpleDateFormat和配置JFormattedTextField.

SimpleDateFormat(String pattern)
          用给定的模式和默认语言环境的日期格式符号构造 SimpleDateFormat。参数pattern是一个有特殊意义的字符串。比如,y表示年份,M表示月份,d表示这一个月的一天,h代表小时,m代表分钟,s代表秒,a代表AM/PM标记。

以下程序动态的显示系统时间:

@SuppressWarnings("serial")public class JFormattedTextFieldDemo extends JFrame{private final JFormattedTextField currentTimeText;/** * @param args */public JFormattedTextFieldDemo(){GridLayout gridLayout = new GridLayout(0,2);JPanel panel = new JPanel(gridLayout);JLabel currentTime = new JLabel("当  前  时  间:");currentTime.setHorizontalAlignment(SwingConstants.CENTER);panel.add(currentTime);SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");currentTimeText = new JFormattedTextField(format);currentTimeText.setEditable(false);currentTimeText.addActionListener(new TimerListener());panel.add(currentTimeText);this.getContentPane().add(panel,BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);this.setSize(480,300);this.setVisible(true);}private class TimerListener implements ActionListener{public TimerListener(){Timer timer = new Timer(1000,this);timer.start();}public void actionPerformed(ActionEvent e){currentTimeText.setValue(new java.util.Date());}}public static void main(String[] args) {// TODO Auto-generated method stubnew JFormattedTextFieldDemo();}}



 

用MaskFormatte实例配置JFormattedTextField:

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

配置可接受的输入:

#

一个数字

?

一个字母

A

一个字母或数字

*

任意字符

U

一个字母,小写字符映射到与它们等效的大写字符上

L

一个字母,大写字符映射到与它们等效的小写字符上

H

一个十六进制数字(A-Fa-f0-9

'

用来转义另外一个掩码字符

 

以下demo是输入11位电话号码

public class JFormattedTextFieldDemo extends JFrame{private JFormattedTextField telText;private MaskFormatter maskFormatter;/** * @param args */public JFormattedTextFieldDemo(){GridLayout gridLayout = new GridLayout(0,2);JPanel panel = new JPanel(gridLayout);JLabel tel = new JLabel("电话");tel.setHorizontalAlignment(SwingConstants.CENTER);panel.add(tel);try {maskFormatter = new MaskFormatter("###########");} catch (ParseException e) {// TODO Auto-generated catch blocke.printStackTrace();}telText = new JFormattedTextField(maskFormatter);panel.add(telText);this.getContentPane().add(panel,BorderLayout.CENTER);this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);this.setLocationRelativeTo(null);this.setSize(480,300);this.setVisible(true);}public static void main(String[] args) {// TODO Auto-generated method stubnew JFormattedTextFieldDemo();}}

另外当该文本框失去郊区时,检测用户的输入是否符合格式,若不符合格式,则有几种方式来提示用户。默认的方式是丢弃用户的输入,文本框内显示的内容恢复到用户输入前的状态。我个人认为这种行为不是一个很好的用户体验,用户辛辛苦苦的输入了很长一串内容,如果仅仅是不小心错了一点,你就把所有内容都变没了,不给用户修改的机会,这是不合适的,无论用户输入是对是错,你只能给出相应的提示,而不能擅自清除用户的输入。java也考虑到了一点,setFocusLostBehavior方法能够保证用户的错误不会丢失。但也没有任何提示性信息来告知用户输入错误。

JFormattedTextField提供了另外一种机制来提示用户输入错误,“如果输入的格式错误,则不允许焦点离开文本框”。换句话说,如果你输入的是“abc”,那么你就别想在其他的文本框里输入内容,焦点会一直停留在此文本框内,直到你输入了正确的格式。

代码如下:

JFormattedTextField intField = new JFormattedTextField(NumberFormat.getIntegerInstance());intField.setInputVerifier(new FormattedTextFieldVerifier());public class FormattedTextFieldVerifier extends InputVerifier{public boolean verify(JComponent component){    JFormattedTextField field = (JFormattedTextField) component;    //若用户的输入符合格式,则返回true,否则返回false    return field.isEditValid();    }}

上述代码不光为JFormattedTextField添加了一个整数格式,还添加了一个验证器类。验证器类中有一个verify方法,此方法若返回true,则焦点可以离开,此方法若返回false,则焦点不能离开。在验证器重,调用JFormattedTextField中的isEditValid来验证格式的正确与否。

 

原创粉丝点击