Javafx按钮事件需要双击

时间:2017-06-01 08:36:10

标签: java javafx

我有一个带有几个按钮的javafx场景。触发按钮事件的唯一方法是双击。在fxml中,该按钮使用以下操作方法onAction="#Button1Action"。如何将button1Action事件的功能从双击更改为只需单击一次?

onAction函数:

 @FXML
 private void Button1Action(ActionEvent event) {
 }

和fxml代码:

<Button id="button1" fx:id="button1" maxHeight="1.79.." maxWidth="1.79.." mnemonicParsing="false" onAction="#Button1Action" text="Answer" GridPane.columnIndex="3" GridPane.columnSpan="3" GridPane.halignment="CENTER" GridPane.rowIndex="7" GridPane.valignment="CENTER">
        <GridPane.margin>
            <Insets bottom="30.0" left="30.0" right="30.0" top="30.0" />
        </GridPane.margin>
</Button>

1 个答案:

答案 0 :(得分:1)

您没有发布Button1Action的正文,但很可能看起来像是:

@FXML
private void Button1Action(ActionEvent event) {
    button1.setOnAction(e -> System.out.println("Button clicked"));
}

这里发生的事情是,您在侦听器中分配侦听器,因此实际的侦听器主体将在第二次单击时执行。

琐碎的修复是:

@FXML
private void Button1Action(ActionEvent event) {
    System.out.println("Button clicked");
}