使用Java中的Textfield中的首字母大写

时间:2017-04-03 22:17:10

标签: java swing jtextfield documentfilter

是否可以将Textfield

中的 FIRST 字母大写

E.g。用户可以输入“你好”字样。和你好'将出现在文本字段中。

我对此代码进行了罚款以使所有字母http://www.java2s.com/Tutorial/Java/0240__Swing/FormatJTextFieldstexttouppercase.htm

大写

我试着编辑它以大写只有FIRST lette r把它弄错了

这是我的编辑

public class UppercaseDocumentFilter extends DocumentFilter {

public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,AttributeSet attr) throws BadLocationException {
    fb.insertString(offset, text.substring(0, 1).toUpperCase() + text.substring(1), attr);
  }

  public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,AttributeSet attrs) throws BadLocationException {
    fb.replace(offset, length, text.substring(0, 1).toUpperCase() + text.substring(1), attrs);
  }

}

2 个答案:

答案 0 :(得分:2)

您的方向正确,您可以查看fb.getDocument().getLength()以确定Document的当前长度,当它为0时,更新text的第一个字符{1}}

然后您可以使用类似......

的内容
String text = "testing";
StringBuilder sb = new StringBuilder(text);
sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
text = sb.toString();
System.out.println(text);

将输入text的第一个字符大写。您可能想要做一些其他检查,但这是基本的想法

实施例

似乎对我有用

public class UppercaseDocumentFilter extends DocumentFilter {

    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException {
        if (fb.getDocument().getLength() == 0) {
            StringBuilder sb = new StringBuilder(text);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            text = sb.toString();
        }
        fb.insertString(offset, text, attr);
    }

    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException {
        if (fb.getDocument().getLength() == 0) {
            StringBuilder sb = new StringBuilder(text);
            sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
            text = sb.toString();
        }
        fb.replace(offset, length, text, attrs);
    }

}

答案 1 :(得分:0)

此代码示例对 ComboBox 编辑器中的第一个字符进行大写替换。它可用于任何文本编辑器,只需更改 comboTextEditor 变量以指向您的文本编辑器。在选择整个编辑文本的情况下(键入字符替换选择的情况),它也可以完成工作。代码还会检查最大长度,并且不允许超过最大尺寸允许的文本。

    class TextSizeFilter extends DocumentFilter {
        int maxCharacters;
        boolean capitalizeFirstLetter;

        public TextSizeFilter(int maxChars, boolean capitalizeFirstLetter) {
            this.maxCharacters = maxChars;
            this.capitalizeFirstLetter = capitalizeFirstLetter;
        }

        public void insertString(FilterBypass fb, int offs,  String str, AttributeSet a)  throws BadLocationException {

            //This rejects the entire insertion if it would make
            //the contents too long. Another option would be
            //to truncate the inserted string so the contents
            //would be exactly maxCharacters in length.
            
            // The insertString(...) method of the DocumentFilter is only called when you use the Document.insertString(...) method to directly update the Document.
            // We will not use that type of update. But anyway, the easiest way is to do a replace  
            replace(fb, offs, 0, str, a);

        }
        
        public void replace(FilterBypass fb, int offs,  int length,  String str, AttributeSet a)  throws BadLocationException {
            //This rejects the entire replacement if it would make
            //the contents too long. Another option would be
            //to truncate the replacement string so the contents
            //would be exactly maxCharacters in length.
            
            
            // if field content starts with ERROR_MESSAGE_START, do not count this error message as it comes from validation. so keep it.
            if ( str.startsWith(ERROR_MESSAGE_START)  ) {
                
                 String docText = fb.getDocument().getText(0, fb.getDocument().getLength());
                 String newStr = docText.substring(0, offs) +str +  docText.substring(offs + length);
                 String errorMessage = extractErrorMessage(newStr);
                 
                if (   (fb.getDocument().getLength() + str.length() - length  - errorMessage.length() ) <= maxCharacters  ) {
                    super.replace(fb, offs, length, str, a);
                }  else {
                    Toolkit.getDefaultToolkit().beep();
                }
                
            } else {
                    if (    (fb.getDocument().getLength() + str.length() - length) <= maxCharacters   ) {
                        
                        if (capitalizeFirstLetter) {
                             if (fb.getDocument().getLength() == 0) {
                                 StringBuilder sb = new StringBuilder(str);
                                 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
                                 str = sb.toString();
                                 // one char typed over a selcted text, where selected text represents the whole editor text
                             } else if (str!=null && str.length() == 1 && comboTextEditor.getSelectedText() != null  && comboTextEditor.getSelectedText().length() == fb.getDocument().getLength() ) {
                                 
                                 StringBuilder sb = new StringBuilder(str);
                                 sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
                                 str = sb.toString();
                                 
                             }
                        }
                        super.replace(fb, offs, length, str, a);
                    }  else {
                        Toolkit.getDefaultToolkit().beep();
                    }
            }
        }

    }