同时设置jtextfield textlimit和大写 [英] set jtextfield textlimit and uppercase at same time

查看:115
本文介绍了同时设置jtextfield textlimit和大写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中有几个jtextfield,我想输入其中一个允许大写和小写,还限制可以引入jtextfield的字符数限制.我必须分隔课程,一个放置上限,另一个放置大写或小写.

I Have a couple jtextfield in my app and I want to put one of them that allow upper and lowcase and also put the limit of number of character that can be introduce into the jtextfield. I have to separe class, one to put the limit and the other to put the upper or lowcase.

代码到jtextfield的限制:

code to the limit of jtextfield:

package tester;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

public class TextLimiter extends PlainDocument {

    private Integer limit;

    public TextLimiter(Integer limit) {
        super();
        this.limit = limit;
    }

    public void insertString(int offs, String str, AttributeSet a) throws BadLocationException {
        if (str == null) {
            return;
        }
        if (limit == null || limit < 1 || ((getLength() + str.length()) <= limit)) {
            super.insertString(offs, str, a);
        } else if ((getLength() + str.length()) > limit) {
            String insertsub = str.substring(0, (limit - getLength()));
            super.insertString(offs, insertsub, a);
        }
    }
}

这是设置大写或反之的代码:

and here is the code to set uppercase or vice versa:

package classes;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;

public class upperCASEJTEXTFIELD extends DocumentFilter {

    @Override
    public void insertString(DocumentFilter.FilterBypass fb, int offset, String text,
            AttributeSet attr) throws BadLocationException {
        fb.insertString(offset, text.toUpperCase(), attr);
    }

    @Override
    public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text,
            AttributeSet attrs) throws BadLocationException {

        fb.replace(offset, length, text.toUpperCase(), attrs);
    }
}

要继续我的问题,我想设置一个jtextfield限制= 11和大写.

to resume my question, I want to set a jtextfield limit = 11 and uppercase.

推荐答案

PlainDocument doc = new TextLimiter();
doc.setDocumentFiletr(new upperCASEJTEXTFIELD());
JTextField textField = new JTextField();
textField.setDocument(doc);

这篇关于同时设置jtextfield textlimit和大写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆