带有MaskFormatter的JFormattedTextField

Jef*_*rey 12 java swing datetime textfield

我有一个JFormattedTextField我用来限制日期和时间的条目.我想使用a MaskFormatter来显示占位符字符.有没有一种方法使用MaskFormatter上的顶部JFormattedTextField时,文本字段已在使用SimpleDateFormat

谢谢,杰夫

I82*_*uch 28

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

替代文字

除非我误解了这个问题.