带有 MaskFormatter 的 JFormattedTextField

发布于 2024-10-04 01:48:16 字数 227 浏览 0 评论 0原文

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

谢谢, 杰夫

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?

Thanks,
Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮年 2024-10-11 01:48:16
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);
    }
}

alt text

除非我误解了这个问题。

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

alt text

Unless I'm misunderstanding the question.

脸赞 2024-10-11 01:48:16

或者,考虑InputVerifier,如 InputVerificationDemo 中建议的 以及这个更详细的示例

Alternatively, consider anInputVerifier, as suggested in InputVerificationDemo and this more elaborate example.

欲拥i 2024-10-11 01:48:16
JFormattedTextField tft3 = 
    new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
    tft3.setValue(new Date());

    Date date = (Date) tft3.getValue();
JFormattedTextField tft3 = 
    new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
    tft3.setValue(new Date());

    Date date = (Date) tft3.getValue();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文