2009-12-16 139 views

回答

-2

我假设你知道如何访问onKeyPress事件...你去!

+0

-1。这对任何人都没有帮助。 – pvorb 2013-01-22 11:21:59

4

JFormattedTextField带有用于文本字段的NumberFormatter。我并没有真正看到在文本区域使用这个功能 - 您希望浮点数有多大?

0

取决于你需要什么,沿这个东西线可能为你工作:

myTextField.setInputVerifier(new InputVerifier() { 

      @Override 
      public boolean verify(JComponent input) { 
       JTextField textField = ((JTextField) input); 
       try { 

        Float isFloat = Float.valueOf(textField.getText()); 
        textField.setBackground(Color.WHITE); 
        return true; 
       } catch (NumberFormatException e) { 
        textField.setBackground(Color.RED); 
        return false; 
       } 

      } 
     });