2017-02-15 23 views
0

所以我想验证输入到我的文本字段中的内容只有数字而不是字母数字。我研究过格式化的文本框,但由于缺乏对Oracle在其网站上的教程的理解,所以无法实现它们。我做了更多的搜索并找到了关键的监听器类,它似乎是最好的选择。如何使用按键侦听器来验证输入到文本字段

因此,当我运行程序并键入到文本字段而不是消耗字符事件时,它只是发出哔哔声。下面的代码:

private void buildPanel() 
{ 
    // Create a label to display instructions. 
    messageLabel = new JLabel("Enter a time in seconds the object has fallen"); 

    // Create a text field 10 characters wide 
    fallingTextField = new JTextField(10); 

    // Add a keylistener to the text field 
    fallingTextField.addKeyListener(new TextFieldListener()); 

    // Create a button with the caption "Calculate" 
    calcButton = new JButton("Calcualte"); 

    // Add an action listener to the button 
    calcButton.addActionListener(new CalcButtonListener()); 

    //Create a JPanel object and let the panel field reference it 
    panel = new JPanel(); 

    // Add the label, text field, and button components to the panel 
    panel.add(messageLabel); 
    panel.add(fallingTextField); 
    panel.add(calcButton); 
} 

/** 
    The TextFieldListener class checks to see if a valid key input is typed into 
    the text field. 
*/ 
private class TextFieldListener implements KeyListener 
{ 
    /** 
     The keyPressed method 
     @param evt is a key event that verifies a number is inputed otherwise 
     the event is consumed. 
    */ 
    public void keyPressed(KeyEvent evt) 
    { 
     char c = evt.getKeyChar(); 
     if(Character.isAlphabetic(c)) 
     { 
      getToolkit().beep(); 
      evt.consume(); 
     } 
    } 
    public void keyReleased(KeyEvent e) 
    { 

    } 
    public void keyTyped(KeyEvent ev) 
    { 

    } 
} 
+1

唐为了爱的理智,不要使用'KeyListener',而是使用'DocumentListener',而不是使用'DocumentFilter',它就是它的设计目的 – MadProgrammer

+0

另一个注意事项:当格式化工具不起作用...那么不要放弃它。只要继续阅读并学习如何使用那个东西。你看,你遇到了一个问题,并决定放弃。不要这样做。你通过**解决**难题学习编程;而不是通过交换另一个...... – GhostCat

+0

当用户将文本粘贴到字段中时会发生什么? (暗示没有) – MadProgrammer

回答

2

这里:

if(Character.isAlphabetic(c)) 
    { 
     getToolkit().beep(); 

你告诉你的听众发出蜂鸣声,每次输入一个字母的时间。所以当你这样做时会发出哔哔声。

一切正如代码所暗示的那样工作。

对于您的问题的其他部分;查看Javadoc的consume()(它从InputEvent继承)。

消耗此事件,以便它不会由源自它的源按默认方式处理。

您不在源码方面的东西。该事件已经创建,并派发给听众。在您的环境中调用consume() ...根本不会再做任何事情。这或多或少是“不行”。

再说一遍:你写的代码等待键盘输入,而输入是字母时,会发出哔声。那就是你的代码所做的全部。我假设你提供一个“/”例如......什么都不应该发生。如果你想验证,那么你需要一个“反馈”循环;例如,您的侦听器在检测到错误输入时可能会覆盖文本字段内容。

+0

所以,即使我有evt.consume();在getToolKit()下面。beep()它只会发出哔声? 我想它会发出蜂鸣声,让用户知道键输入不正确,然后消耗事件。 –

+0

查看我的更新。 – GhostCat

1

我认为问题在于事件处理完成时已经添加了角色。您需要设置字段的值,而最后一个字符,如:

fallingTextField.setValue(fallingTextField.getValue().substring(0, fallingTextField.getValue().length - 1)) 

或者,您也可以在Oracle网站上使用的例子,使用面膜的输入,如下所示:

zipField = new JFormattedTextField(
        createFormatter("#####")); 
... 
protected MaskFormatter createFormatter(String s) { 
    MaskFormatter formatter = null; 
    try { 
     formatter = new MaskFormatter(s); 
    } catch (java.text.ParseException exc) { 
     System.err.println("formatter is bad: " + exc.getMessage()); 
     System.exit(-1); 
    } 
    return formatter; 
} 
+0

谢谢我将重新阅读甲骨文的屏蔽教程,看看我是否能够更好地理解如何实现您所展示的内容。 –

+0

(1+)为教程链接。这不仅有助于解决这个问题,而且本教程可以用作所有Swing基础的参考。 – camickr

相关问题