gpt4 book ai didi

java - JFormattedTextField 与 MaskFormatter

转载 作者:搜寻专家 更新时间:2023-10-30 21:00:27 24 4
gpt4 key购买 nike

我有一个 JFormattedTextField 用于限制日期和时间的输入。我想使用 MaskFormatter 来显示占位符字符。当文本字段已经在使用 SimpleDateFormat 时,是否可以在 JFormattedTextField 之上使用 MaskFormatter

谢谢,杰夫

最佳答案

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

除非我误解了这个问题。

关于java - JFormattedTextField 与 MaskFormatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4252257/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com