我有一个JFormattedTextField,用来限制日期和时间的输入。我想使用MaskFormatter来显示占位符字符。当文本字段已经在使用MaskFormatter时,有没有办法在JFormattedTextField之上使用SimpleDateFormat
谢谢,杰夫
发布于 2010-11-23 10:53:49
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);
}
}

除非我误解了问题。
发布于 2010-11-23 10:42:20
或者,考虑一个InputVerifier,正如InputVerificationDemo和这个更复杂的example中所建议的那样。
发布于 2010-11-23 10:44:49
JFormattedTextField tft3 =
new JFormattedTextField(new SimpleDateFormat("yyyy-MM-dd"));
tft3.setValue(new Date());
Date date = (Date) tft3.getValue();https://stackoverflow.com/questions/4252257
复制相似问题