简体   繁体   English

解析异常MaskFormatter

[英]Parse Exception MaskFormatter

I'm trying to use MaskFormatter with JFormattedTextField . 我正在尝试将MaskFormatterJFormattedTextField一起使用。

Inside my class I have this line of code 在我的课堂中,我有这行代码

JFormattedTextField jtfCNP=new JFormattedTextField(new MaskFormatter(formatCNP));

When I compile, I get the ParseException . 编译时,我得到ParseException The only way around it I found to create the instance of the MaskFormatter inside the constructor of my GUI panel inside a try block like this 围绕它的唯一方法是,我发现在这样的try块内的GUI面板的构造函数内创建MaskFormatter的实例

    try { format_cnp=new MaskFormatter("# ###### ######");
    jtf_cnp=new JFormattedTextField(format_cnp);
    } catch (ParseException pex) {}
    jtf_cnp.setColumns(20);

I'd like the functionality of the MaskFormatter, but isn't there any way to instantiate it outside a method? 我想要MaskFormatter的功能,但是没有方法在方法之外实例化它吗? Also, is there a better alternative to MaskFormatter ? 此外,还有更好的替代MaskFormatter吗?

Edit: the sample code that doesn't work: 编辑:无效的示例代码:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.text.*;
    import javax.swing.text.*;

    public class test extends JFrame{

        String formatCNP="# ###### ######";
        //components
        JFormattedTextField jtfCNP=new JFormattedTextField(new MaskFormatter(formatCNP));

        public test(){

        super("MaskFormatter Test");
        setSize(300,300);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        jtfCNP.setColumns(20);
        add(jtfCNP);

        setVisible(true);

        }

        public static void main(String[] args){

            test tf=new test();
        }

    }
  1. MaskFormatter should be created as method, void. MaskFormatter应该作为方法创建,无效。 etc 等等

  2. read Initial Thread 读取Initial Thread

  3. create an local variable for JFrame , don't to extend whatever in Swing, nor for Top-Level Containers , JFrame创建局部变量,不要扩展Swing中的任何内容,也不要扩展Top-Level Containers

  4. search for Java Naming Convention , eg class & constructor name should be Test instead of test 搜索Java Naming Convention ,例如,类和构造函数名称应为Test而不是test

.

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.text.*;

public class Test {

    private String formatString = "##/######";
    private MaskFormatter formatCNP = createFormatter(formatString);
    private JFormattedTextField jtfCNP = new JFormattedTextField(formatCNP);
    private JFrame frame = new JFrame("MaskFormatter Test");

    public Test() {
        jtfCNP.setColumns(20);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(jtfCNP);
        frame.pack();
        frame.setVisible(true);
    }

    MaskFormatter createFormatter(String format) {
        MaskFormatter formatter = null;
        try {
            formatter = new MaskFormatter(format);
            formatter.setPlaceholderCharacter('.'/*or '0' etc*/);
            formatter.setAllowsInvalid(false); // if needed
            formatter.setOverwriteMode(true); // if needed
        } catch (java.text.ParseException exc) {
            System.err.println("formatter is bad: " + exc.getMessage());
        }
        return formatter;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Test tf = new Test();
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM