2012-05-14 34 views
6

Vui lòng xem mã sau đây.JTextField: Làm thế nào để hạn chế số lượng charaters?

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

} 

Ở đây, tôi cố gắng giới hạn số ký tự xuống 5. OK, nó dừng chèn ký tự nữa khi nó đạt đến 5, nhưng trường hợp là, nó cũng không cho phép xóa ký tự được chèn , thay thế hoặc bất cứ thứ gì. Xin hãy giúp tôi sửa vấn đề này. Cảm ơn

Trả lời

8

chỉ cần thay đổi phương pháp loại bỏ hiện tại của bạn:

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

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

cho một này:

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

nó bây giờ nên làm việc.

+1

Wow! Kinh ngạc! Cảm ơn rất nhiều! –

+1

Thực sự là giải pháp rất tốt đẹp! +1 – Hidde

+0

Đây vẫn là một cách tiếp cận rất phức tạp, ý tôi là 'FieldListener'. – TiMr

Các vấn đề liên quan