【发布时间】:2011-05-14 05:28:39
【问题描述】:
我有一个JFormattedTextField,我用它来限制日期和时间的输入。我想使用MaskFormatter 来显示占位符字符。当文本字段已经使用SimpleDateFormat 时,有没有办法在JFormattedTextField 之上使用MaskFormatter?
谢谢, 杰夫
【问题讨论】:
标签: java swing datetime textfield
我有一个JFormattedTextField,我用它来限制日期和时间的输入。我想使用MaskFormatter 来显示占位符字符。当文本字段已经使用SimpleDateFormat 时,有没有办法在JFormattedTextField 之上使用MaskFormatter?
谢谢, 杰夫
【问题讨论】:
标签: java swing datetime textfield
或者,考虑InputVerifier,如InputVerificationDemo 中所建议的那样,以及更详细的example。
【讨论】:
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);
}
}
除非我误解了这个问题。
【讨论】:
JFormattedTextField tft3 =
new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
tft3.setValue(new Date());
Date date = (Date) tft3.getValue();
【讨论】: