【问题标题】:Javafx button event needs double clickingJavafx 按钮事件需要双击
【发布时间】:2017-06-01 08:36:10
【问题描述】:

我有一个 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>

【问题讨论】:

  • 你能发一些sn-p吗? onAction 不是通过双击触发的,而是通过任何操作触发的。
  • 我发布了这个功能,只有双击才能激活。它是 fxml 网格窗格的一部分。
  • 不确定我应该发布什么,因为 Button1Action 中的代码不包含任何事件处理程序。
  • 创建一个minimal reproducible example 并将其发布在问题中。无法从这些 sn-ps 诊断问题。

标签: java javafx


【解决方案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");
}

【讨论】:

  • 我的代码是你的第二个例子。这是奇怪的事情。我在代码中没有 setOnAction。
  • 在这种情况下,请使用示例更新您的问题。
猜你喜欢
  • 1970-01-01
  • 2015-11-18
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多