Swing_格式文本框和密码框

Java1.4 引入了JFormattedTextField,这个组件对于编辑诸如数字和日期等复杂的格式化值提供了显式的支持。 JFormattedTextField的表现有些类似与JTextField,不过在其中要接受一个指定格式的对象,而且要通过器setValue() 和getValue()方法来管理一个复杂的对象类型(如Date或Integer)。

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Box;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

public class Text  {
   
    public static void main(String args[]) throws Exception {
        Box form = Box.createVerticalBox();
        form.add(new JLabel("Name;"));
        form.add(new JTextField("Joe User"));
       
        form.add(new JLabel("Birthiday:"));
        JFormattedTextField birthdayField =
                                new JFormattedTextField(new SimpleDateFormat("mm/dd/yy"));
        birthdayField.setValue(new Date());
        form.add(birthdayField);
       
        form.add(new JLabel("Age:"));
        form.add(new JFormattedTextField(new Integer(32)));
       
        form.add(new JLabel("Hairs on Body:"));
        JFormattedTextField hairsField =
                                new JFormattedTextField(new DecimalFormat("###,###"));
        hairsField.setValue(new Integer(100000));
        form.add(hairsField);
       
        form.add(new JLabel("Phone Number:"));
        JFormattedTextField phoneField =
                                new JFormattedTextField(new MaskFormatter("(###)###-####"));
        phoneField.setValue("(314)555-1212");
        form.add(phoneField);
       
        JFrame frame = new JFrame("User Information");
        frame.getContentPane().add(form);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

    JFormattedTextField可以使用setValue()设置一个有效值,也可以用getValue()来获取上一个有效值,为此,必须基于所用的格式将值强制转换回正确的类型,例如:

    Date bday = (Date)birthdayField.getValue();

    对于一个JFormattedTextField,当用户试图将焦点由此域转到另一个域时,该JFormattedTextField将对文本进行有效性验证。默认情况下,还原为上一个有效值。如果希望保留无效值,可以调用 setFocusLostBehavior()方法来设置JFormattedTextField.COMMIT(默认为 COMMIT_OR_REVERT)。

过滤输入
    JFormattedTextField本身对于各种格式类型一无所知,它使用了AbstractFormatter对象,此对象才了解特定的格式类型。AbstractFormatter提供了两个接口的实现:DocumentFilter和NavigationFilter。DocumentFilter将关联至Document的实现,并允许截取编辑命令从而根据需要加以修改。NavigationFilter可以关联至JTextComponent,从而用来控制光标的移动。

    以下例子为JTextField应用了一个文档过滤器,将所有输入都改为大写。代码如下:

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class DocFilter {
   
    public static void main(String[] args) {
        JTextField field = new JTextField(30);
        //设置过滤器
        ((AbstractDocument)(field.getDocument())).setDocumentFilter(
                new DocumentFilter(){
                    //填写数据时,所有字母改为大写
                    public void insertString(FilterBypass fb,
                                              int offset,
                                              String string,
                                              AttributeSet attr)throws BadLocationException{
                        fb.insertString(offset,string.toUpperCase(),attr);
                    }
                    //修改数据时,所有字母改为大写
                    public void replace(DocumentFilter.FilterBypass fb,
                                         int offset,
                                         int length,
                                         String string,
                                         AttributeSet attr) throws BadLocationException{
                        fb.replace(offset,length,string.toUpperCase(), attr);
                    }
                });
       
        JFrame frame = new JFrame("User Information");
        frame.getContentPane().add(field);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

    当文本增加到文档或有所修改时,将调用DocumentFilter的方法insertString()和replace()。在这些方法中则有机会在继续传递文本前对其过滤。当已经准备好应用文本时,则使用FilterBypass引用。FilterBypass有同样的方法,它将把修改直接应用到文档中。DocumentFilter remove()方法可以用于截取删除文档字符的编辑命令。
    对于这个例子,有一点需要指出,即并非所有Document都有一个setDocumentFilter()方法。我们必须将文档类型强制转换为AbstractDocument。只有扩展了AbstractDocument的文档实现才接受过滤器。这是因为在Java1.4中增加了文档过滤API,但认为不能对原来的Document接口做出修改。

密码输入框
    JPasswordField仅用于输入密码,它是JTextField的子类,使用与JTextField完全相同,只不过所键入的每个字符都显示为用一个字符,通常是星号,可以使用setEchoChar()方法来设置JPasswordField使用另一个字符。
    可以使用getText()来得到所键入的密码,但已被废弃。取而代之的是getPassword()方法。getPassword()方法返回一个字符数组,而不是一个String对象。因为对于窥探内存的密码窃听器,字符数组没有String那么脆弱。要注意,在Java密码类中,其方法均接受字符数组类型的密码而不是String,因此可以直接将getPassword()调用结果传递给密码类而无需创建一个String。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值