java JavaFX 如何在单击鼠标时清除 TextField

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28516654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:44:03  来源:igfitidea点击:

JavaFX how to clear TextField when mouse is clicked on it

javajavafx

提问by RichardK

I have a problem with chosing correct action on TextField. On Java class:

我在 TextField 上选择正确的操作时遇到问题。在 Java 类上:

@FXML
    private TextField projectNameInput;

@FXML
    private void clearProjectName(MouseEvent event) {
// some if - else statements
            projectNameInput.clear();
    }

On FXML it looks like this:

在 FXML 上,它看起来像这样:

  <TextField fx:id="projectNameInput" onMouseClicked="#clearProjectName" GridPane.columnIndex="1">

I tried most options, in Java I tried (as arg) Event, MouseEvent, ActionEvent, combined with FXML onAction, etc but no luck. What kind of action should I choose that when I click mouse button on TextField it will be automaticly cleared ?

我尝试了大多数选项,在 Java 中我尝试过(作为 arg)Event、MouseEvent、ActionEvent,结合 FXML onAction 等,但没有运气。我应该选择什么样的操作,当我在 TextField 上单击鼠标按钮时它会自动清除?

采纳答案by kintu david

TextField1.addEventFilter(KeyEvent.KEY_TYPED , numeric_Validation(5));

AutoControl.setOnMousePressed(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {

            TextField1.clear();    
}