Javafx按钮事件需要双击
我有一个javafx场景,里面有几个按钮。激活按钮事件的唯一方法是双击。在fxml中,按钮使用以下操作方法Javafx按钮事件需要双击,java,javafx,Java,Javafx,我有一个javafx场景,里面有几个按钮。激活按钮事件的唯一方法是双击。在fxml中,按钮使用以下操作方法onAction=“#Button1Action”。如何将button1Action事件的功能从双击更改为仅单击一次 函数onAction: @FXML private void Button1Action(ActionEvent event) { } 以及fxml代码: <Button id="button1" fx:id="button1" maxHeight="1.79.
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操作的主体,但很可能看起来像:
@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");
}
你可以发布一些片段吗onAction
不是通过双击触发的,而是通过任何操作触发的。我发布了该函数,该函数仅通过双击激活。它是fxml的gridpane的一部分。不确定应该发布什么,因为Button1Action中的代码不包含任何事件处理程序。创建一个并在问题中发布它。无法从这些代码片段中诊断问题。我的代码是您的第二个示例。这是奇怪的事情。代码中没有setOnAction。在这种情况下,请用一个例子更新您的问题。