JFormattedTextField与MaskFormatter [英] JFormattedTextField with MaskFormatter

查看:198
本文介绍了JFormattedTextField与MaskFormatter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JFormattedTextField ,用于限制日期和时间的条目。我想使用 MaskFormatter 来显示占位符字符。当文本字段已经使用<$ c时,有没有办法在 JFormattedTextField 的顶部使用 MaskFormatter $ c> SimpleDateFormat ?

I have a JFormattedTextField that I use to restrict entries of a date and time. I want to use a MaskFormatter though to show the placeholder chars. Is there a way to use a MaskFormatter on top of the JFormattedTextField when the text field is already using a SimpleDateFormat?

谢谢,
Jeff

Thanks, Jeff

推荐答案

public class MaskFormatterTest {
    private static final DateFormat df = new SimpleDateFormat("yyyy/mm/dd");


    public static void main(String[] args) {
        JFrame frame = new JFrame("");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();

        JFormattedTextField tf = new JFormattedTextField(df);
        tf.setColumns(20);
        panel.add(tf);
        try {
            MaskFormatter dateMask = new MaskFormatter("####/##/##");
            dateMask.install(tf);
        } catch (ParseException ex) {
            Logger.getLogger(MaskFormatterTest.class.getName()).log(Level.SEVERE, null, ex);
        }

        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }
}

除非我误解了这个问题。

Unless I'm misunderstanding the question.

这篇关于JFormattedTextField与MaskFormatter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆