2015-04-29 187 views
3

嗨我想使用setDocument方法來限制用戶可以在文本字段中輸入的字符數。但不知何故,它不會限制輸入字符的數量。下面的代碼JTextField設置輸入限制

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


public class JTextFieldCharLimit extends PlainDocument { 


    private int limit; 

    public JTextFieldCharLimit(int limit) 
    { 
     super(); 
     this.limit = limit; 
    } 
    public void inserString(int offset, String str, AttributeSet set) throws BadLocationException 
    { 
     if(str == null) 
     { 
      return; 
     } else if((getLength() + str.length()) <= limit) 
     { 
      str = str.toUpperCase(); 
      super.insertString(offset, str, set); 
     } 
    } 




} 

我在我宣佈我的文本字段,如下所示另一個類使用這個類:

void playerInfoScreen(JFrame mainFrame, JPanel menuPanel) 
    { 
     final ScreenConstructor playerName = new ScreenConstructor(); 
     final JFrame frame = mainFrame; 
     final JPanel returnPanel = menuPanel; 

     final JPanel panel = playerName.createPanel("menu panel"); 

     final JButton returnButton = playerName.createButton("MAIN MENU"); 
     final JTextField textEntry = playerName.createTextField(10); 
       // text field length needs to be set to prevent long texts 
     final JLabel label = playerName.createLabel("Enter Player Name:"); 

     playerName.addButtonToPanel(panel, returnButton); 
     playerName.addLabelToPanel(panel, label); 
     playerName.addJTextFieldToPanel(panel, textEntry); 

     textEntry.setDocument(new JTextFieldCharLimit(5)); 
     playerName.displayScreen(frame, panel); 
      // check for esc button to let user return back to main menu 




     textEntry.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       String player = textEntry.getText(); // save entered player name 
       storedPlayerName = player; // store player in order to use it in highscores and display on game screen 
       GameScreen game = new GameScreen(frame, panel); // go to game screen 
      } 
     }); 


     returnButton.addActionListener(new ActionListener() 
     { 
      public void actionPerformed(ActionEvent e) 
      { 
       frame.setContentPane(returnPanel); // go back to previous panel 
      } 
     }); 




    } 
+0

什麼是'playerName.addJTextFieldToPanel(面板,文本輸入);' – Sanjeev

+0

我有一個文本字段添加到一個單獨的類做面板的方法。 playerName是該類的一個對象。 –

回答

2

使用DocumentFilter。有關更多詳細信息,請參閱Implementing a Document FilterDocumentFilter Examples

public class SizeFilter extends DocumentFilter { 

    private int maxCharacters;  

    public SizeFilter(int maxChars) { 
     maxCharacters = maxChars; 
    } 

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

     if ((fb.getDocument().getLength() + str.length()) <= maxCharacters) 
      super.insertString(fb, offs, str, a); 
     else 
      Toolkit.getDefaultToolkit().beep(); 
    } 

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

     if ((fb.getDocument().getLength() + str.length() 
       - length) <= maxCharacters) 
      super.replace(fb, offs, length, str, a); 
     else 
      Toolkit.getDefaultToolkit().beep(); 
    } 
} 

感謝MDP

((AbstractDocument)textEntry.getDocument()).setDocumentFilter(new SizeFilter(5)); 
+0

那工作:)。 –