需要JavaFx Textfield Focus属性中关于在调出焦点后更改文本的帮助吗

需要JavaFx Textfield Focus属性中关于在调出焦点后更改文本的帮助吗,java,javafx-2,Java,Javafx 2,需要Javafx文本字段焦点属性的帮助,我想更改文本字段中的文本,如果在我聚焦文本字段时使用焦点属性输入大于12的数字,则必须将内部文本更改为12 我使用的代码是 NumberTextField numberTextField = new NumberTextField(); numberTextField.setLayoutX(280); numberTextField.setLayoutY(280); //Textfield1 working numberTex

需要Javafx文本字段焦点属性的帮助,我想更改文本字段中的文本,如果在我聚焦文本字段时使用焦点属性输入大于12的数字,则必须将内部文本更改为12 我使用的代码是

NumberTextField numberTextField = new NumberTextField();
    numberTextField.setLayoutX(280);
    numberTextField.setLayoutY(280);
    //Textfield1 working
   numberTextField.focusedProperty().addListener((arg0, oldPropertyValue, newPropertyValue) -> {
       if (newPropertyValue)
        {

        }
        else
        {
            if(numberTextField.getText() == "" && Integer.parseInt(numberTextField.getText()) > 12)
            {

            }
            numberTextField.setText("12");
            System.out.println("Textfield 1 out focus");
        }


    });
numberTextfield类是

    public class NumberTextField extends TextField
{
 @Override
    public void replaceText(int start, int end, String text)
    {
        if (validate(text))
        {
            super.replaceText(start, end, text);
        }
    }

    @Override
    public void replaceSelection(String text)
    {
        if (validate(text))
        {
            super.replaceSelection(text);
        }
    }

    private boolean validate(String text)
    {

       return ("".equals(text) || text.matches("[0-9]"));
    }
}

因此,它工作正常,每当我调出焦点时,它都会更改文本字段的文本,而不是在输入任何文本后,或者当我输入的文本小于12时。

您将条件写错了。要编写适当的条件,请学习Trueth表


感谢它的工作,是我的错,我写错了,并为糟糕的英语抱歉。。
    NumberTextField numberTextField = new NumberTextField();
    numberTextField.setLayoutX(280);
    numberTextField.setLayoutY(280);
    // Textfield1 working
    numberTextField.focusedProperty().addListener((arg0, oldPropertyValue, newPropertyValue) -> {
        if (newPropertyValue) {

        } else {
            if (numberTextField.getText().isEmpty() || numberTextField.getText() == null
                    || Integer.parseInt(numberTextField.getText()) > 12) {
                numberTextField.setText("12");
            }
            System.out.println("Textfield 1 out focus");
        }

    });