在NetBeans中,如何为JFormattedTextField设置无效字符,以使其不允许:|[]等?

没关系,我想出了解决方案:

MaskFormatter formatter = null;

        try {
            //# - Any valid number, uses Character.isDigit.
            //' - Escape character, used to escape any of the special formatting characters.
            //U - Any character (Character.isLetter). All lowercase letters are mapped to upper case.
            //L - Any character (Character.isLetter). All upper case letters are mapped to lower case.
            //A - Any character or number (Character.isLetter or Character.isDigit)
            //? - Any character (Character.isLetter).
            //* - Anything.
            //H - Any hex character (0-9, a-f or A-F).
            formatter = new MaskFormatter("******************************");
            formatter.setInvalidCharacters("|[]");
        } catch (java.text.ParseException ex) {
        }


然后,在GUI上右键单击JFormattedTextField,单击“自定义代码”,对于myFormattedTextField = new javax.swing.JFormattedTextField();旁边的下拉框,选择自定义创建并将格式化程序添加到JFormattedTextField,如下所示:myFormattedTextField = new javax.swing.JFormattedTextField(formatter);

希望有一天能对别人有所帮助。

最佳答案

最舒适的方法是添加DocumentListener,然后在Pattern中,JFormattedTextField的值可以插入(字符块)或按字符添加字符,我建议覆盖这两种方法

insertUpdate()

changedUpdate()//Plain text components don't fire these events.


对于插入的字符块将更好地直接实现Pattern p = Pattern.compile("decision char filtering")

关于java - 如何在JFormattedTextField中禁止字符?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8434731/

10-11 10:43