2017-06-01 120 views
2

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

功能的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> 
+0

可能您张贴一些片断? 'onAction'不是双击而是任何动作。 – TomN

+0

我发布了该功能,该功能仅通过双击激活。它是fxml gridpane的一部分。 – konstantin

+0

不确定究竟应该发布什么内容,因为Button1Action中的代码不包含任何事件处理程序。 – konstantin

回答

1

你没有张贴Button1Action身体,但最有可能它看起来像:

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

这里发生了什么,您将监听器分配给监听器,所以实际的监听器体w第二次点击就会被执行。

平凡的解决方法是:

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

我的代码是你的第二个例子。这是奇怪的事情。代码里面没有setOnAction。 – konstantin

+1

在这种情况下,请用示例更新您的问题。 – DVarga