2012-12-27 56 views
2

我希望用戶只輸入字母或空格 如果用戶輸入其他字符,我想給人以JOptionPane的 消息我已經搜查,我想下面的代碼的JTextField只接受字母和空格

if (!(Pattern.matches("^[a-zA-Z]+$", answerField1.getText()))) 
     JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); 

但現在無論我進入它給人的錯誤

現在我改變了代碼

Pattern letterPattern = Pattern.compile("^[a-zA-Z]+$"); 

if (!(letterPattern.matcher(answerField1.getText()).matches())) 
     { 
    JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); 
} 

現在它給消息○只有第一次用戶輸入號碼。 我該如何解決這個問題

+1

看到這個答案:http://stackoverflow.com/a/8017847/597657 –

+0

感謝但所有關於數字的例子都有字母表的例子嗎?因爲我無法理解它如何將字母代碼轉換爲 – mxyz

+0

你也可以使用JFormattedTextField – exexzian

回答

8

使用DocumentFilter,這裏是我做了一個例子,它只能接受字母字符和空格:

import javax.swing.JFrame; 
import javax.swing.JTextField; 
import javax.swing.SwingUtilities; 
import javax.swing.text.AbstractDocument; 
import javax.swing.text.AttributeSet; 
import javax.swing.text.BadLocationException; 
import javax.swing.text.DocumentFilter; 
import javax.swing.text.DocumentFilter.FilterBypass; 

public class Test { 

    public Test() { 
     initComponents(); 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new Test(); 
      } 
     }); 
    } 

    private void initComponents() { 
     JFrame frame = new JFrame(); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     JTextField jtf = new JTextField(); 
     //add filter to document 
     ((AbstractDocument) jtf.getDocument()).setDocumentFilter(new MyDocumentFilter()); 

     frame.add(jtf); 

     frame.pack(); 
     frame.setVisible(true); 
    } 
} 

class MyDocumentFilter extends DocumentFilter { 

    @Override 
    public void replace(FilterBypass fb, int i, int i1, String string, AttributeSet as) throws BadLocationException { 
     for (int n = string.length(); n > 0; n--) {//an inserted string may be more than a single character i.e a copy and paste of 'aaa123d', also we iterate from the back as super.XX implementation will put last insterted string first and so on thus 'aa123d' would be 'daa', but because we iterate from the back its 'aad' like we want 
      char c = string.charAt(n - 1);//get a single character of the string 
      System.out.println(c); 
      if (Character.isAlphabetic(c) || c == ' ') {//if its an alphabetic character or white space 
       super.replace(fb, i, i1, String.valueOf(c), as);//allow update to take place for the given character 
      } else {//it was not an alphabetic character or white space 
       System.out.println("Not allowed"); 
      } 
     } 
    } 

    @Override 
    public void remove(FilterBypass fb, int i, int i1) throws BadLocationException { 
     super.remove(fb, i, i1); 
    } 

    @Override 
    public void insertString(FilterBypass fb, int i, String string, AttributeSet as) throws BadLocationException { 
     super.insertString(fb, i, string, as); 

    } 
} 
1

你在第一行的末尾有分號。所以它不是真的測試正確。

像這樣的聲明(這是你所擁有的):

if (condition) ; 

將執行空語句(;)如果條件爲真,然後進入下一行。如果條件不成立,它將轉到下一行。這兩個操作具有相同的結果。

您可能會嘗試在所有「if」語句中使用大括號。有時很繁瑣,但很難搞砸。

if (!(Pattern.matches("^[a-zA-Z]+$", answerField1.getText()))) { 
    JOptionPane.showMessageDialog(null, "Please enter a valid character", "Error", JOptionPane.ERROR_MESSAGE); 
} 

這就是我要做的。你可以擦掉分號。

+0

哦,是的。您在正則表達式的方括號中沒有空格,因此不允許有空格。這可能是一個複製問題(或不)。 –

+0

沒有什麼變化。還有另一種方法可以做到嗎? – mxyz

+0

1)嘗試使用調試器進行單步調試。如果您爲文本值和match()調用的結果創建了一些局部變量,則更容易看到發生了什麼。 –

相關問題