我无法告诉您确切的原因,但
setText似乎让您
JFormattedTextField发疯了,因为它
""是一个字符串,并且与当前掩码相反。
请尝试
setValue(null)改用。
我只是确保此方法有效。下一段代码证明了这一点:
public class Two extends Jframe { public static void main(String[] args) throws Exception { new Two().a(); } void a() throws Exception { this.setLayout(new GridLayout(2, 1)); MaskFormatter formatter = new MaskFormatter("#"); formatter.setValidCharacters("123456789"); final JFormattedTextField field = new JFormattedTextField(formatter); JButton b = new JButton("null!"); b.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { field.setValue(null); } }); this.add(field); this.add(b); this.setSize(100, 100); this.setVisible(true); }}单击 空后! 按钮格式化程序将继续正常工作。



