【问题标题】:JTextField: How to limit the number of characters?JTextField:如何限制字符数?
【发布时间】:2012-05-22 02:50:22
【问题描述】:

请看下面的代码。

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 时停止插入更多字符,但情况是,它也不允许删除插入的字符、替换或任何事物。如何解决这个问题?

【问题讨论】:

    标签: java swing jtextfield


    【解决方案1】:

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

     @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);
     }
    

    它现在应该可以工作了。

    【讨论】:

    • 确实非常好的解决方案! +1
    • 这仍然是一个非常复杂的方法,我的意思是FieldListener
    【解决方案2】:

    您应该创建自己的类来检查您是否提供了超过最大允许长度的输入:请参阅http://www.java2s.com/Tutorial/Java/0240__Swing/LimitJTextFieldinputtoamaximumlength.htm 上的示例。

    【讨论】:

    • 非常感谢 Hiddle 的回复。我真的很感激:)
    猜你喜欢
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 2014-10-14
    • 2015-02-14
    相关资源
    最近更新 更多