有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java限制jtextfield不接受空白作为第一个字符

我试图限制jtextfield不接受空白作为其第一个字符。 我的代码已经限制jtextfield不接受第一个输入作为空白,但是当我输入一个字母然后删除它时,我被卡住了不接受空白的字段限制不起作用

public class Restriction {

    public Restriction() {
        initComponents();
    }

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

    private void initComponents() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JTextField Field = new JTextField();
        //add filter to document
        ((AbstractDocument) Field.getDocument()).setDocumentFilter(new MyDocumentFilter());
        frame.add(Field);
        frame.pack();
        frame.setVisible(true);
    }
}

class MyDocumentFilter extends DocumentFilter {
    int z=0;
    @Override
    public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
        for (int n = string.length(); n > 0; n--) {//an inserted string may be more than a single character i.e a copy and paste of 'aaa123d', also we iterate from the back as super.XX implementation will put last insterted string first and so on thus 'aa123d' would be 'daa', but because we iterate from the back its 'aad' like we want
            char c = string.charAt(n - 1);//get a single character of the string
            System.out.println(c);
            if(z==0){//for first character
                  if (Character.isAlphabetic(c)) {//if its an alphabetic character or white space
                      super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character
                      z++;
                  } else {//it was not an alphabetic character or white space
                      System.out.println("Not allowed");
                  }
            }
            else if(z!=0){
              if (Character.isAlphabetic(c) || c == ' ' || Character.isDigit(c)) {//if its an alphabetic character or white space
                  super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character
                  z++;
              } else {//it was not an alphabetic character or white space
                  System.out.println("Not allowed");
              }
            }

        }
    }

    @Override
    public void remove(FilterBypass fb, int i, int i1) throws BadLocationException {
        super.remove(fb, i, i1);
    }

    @Override
    public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException {
        super.insertString(fb, i, string, as);

    }
}

共 (2) 个答案

  1. # 1 楼答案

    尝试查看formattedtextfieldjava文档。你可以做的不仅仅是限制空白

    MaskFormatter format = new MaskFormatter("A*********************");
    JFormattedTextField fTxtFeild = new JFormattedTextField(format);
    

    MaskFormatter中的第一个?指定任何字符或数字(character.isleter或character.isDigit)*指定任何

  2. # 2 楼答案

    我不确定这是否是您试图实现的目标,但是将replace方法更改为类似的方法怎么样

    @Override
    public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException {
        //we want standard behavior if we are not placing space at start of JTextField
        //or if we are placing text at start of JTextField but first character is not whitespace
        if ( i!=0 || ( i==0 && !Character.isWhitespace(string.charAt(0)) ) ){
            super.replace(fb, i, i1, string, as);
        }else{
            System.out.println("no spaces allowed");
        }
    }