JTextField:如何限制字符数? [英] JTextField: How to limit the number of charaters?

查看:100
本文介绍了JTextField:如何限制字符数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请查看以下代码。

import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.text.AbstractDocument;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;


public class Bean extends JFrame
{
    private JTextField field1, field2, field3, field4;
    private JLabel text;
    private JButton ok, cancel;
    private JPanel centerPanel,southPanel, textPanel;
    private GridLayout grid;
    private FlowLayout flow1, flow2;

    public Bean()
    {
        //Declaring instance Variables

        field1 = new JTextField(10);

        field2 = new JTextField(5);
        field3 = new JTextField(5);
        field4 = new JTextField(5);        
        text = new JLabel("Insert Your Numbers Here");

      AbstractDocument d = (AbstractDocument) field1.getDocument();
      d.setDocumentFilter(new Bean.Field1Listener());


        ok = new JButton("OK");
        cancel = new JButton("Cancel");


        /***********************Creating the main view*************************/
        centerPanel = new JPanel();
        grid = new GridLayout(2,1,1,1);



        //Adding TextFields
        textPanel = new JPanel();
        flow1 = new FlowLayout(FlowLayout.CENTER);
        textPanel.setLayout(flow1);

        textPanel.add(field1);




        //Adding Buttons
        southPanel = new JPanel();
        southPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        southPanel.add(ok);
        southPanel.add(cancel);


        //Creating Center View
        centerPanel.setLayout(grid);
        centerPanel.add(text);
        centerPanel.add(textPanel);


        //Gathering everything together
        getContentPane().add(centerPanel,"Center");
        getContentPane().add(southPanel,"South");


        this.setSize(500,200);
        this.validate();
        this.setVisible(true);
        this.setLocationRelativeTo(null);

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

    private class Field1Listener extends DocumentFilter
    {

       @Override  
        public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException  
        {
            if(fb.getDocument().getLength()+string.length()>5)
            {
                return;
            }

            fb.insertString(offset, string, attr);

        }  


        @Override  
        public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException 
        {  

            fb.insertString(offset, "", null);
        }  



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



                 if(fb.getDocument().getLength()+text.length()>5)
                 {
                    System.out.println("OK");
                    return;
                }

                fb.insertString(offset, text, attrs);
        }
    }

    public static void main(String[]args)
    {
        new Bean();
    }

}

在这里,我正在尝试将字符数限制为5.好吧,当它达到5时,它会停止插入任何字符,但是大小写是,它也不允许删除插入的字符,替换或任何东西。请帮我纠正这个问题。谢谢

In here, I am trying to limit the number of character to 5. OK, it stops inserting anymore characters when it reaches 5, but the case is, it also doesn't allow to delete the inserted characters, replace, or anything. Please help me to correct this issue. Thanks

推荐答案

只需更改当前的删除方法:

simply change your current remove method:

 @Override  
 public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException 
 {  

     fb.insertString(offset, "", null);
 } 

这个:

 @Override  
 public void remove(DocumentFilter.FilterBypass fb, int offset, int length) throws BadLocationException 
 {  
     fb.remove(offset, length);
 }

现在应该可以使用。

这篇关于JTextField:如何限制字符数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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