【问题标题】:Pressing Enter Key Randomly on my JavaFx application Triggers wrong Button action在我的 JavaFx 应用程序上随机按下 Enter 键会触发错误的按钮操作
【发布时间】:2018-06-23 14:05:01
【问题描述】:

在我的 javaFx 应用程序上按 Enter 键会触发错误的按钮, 在这种情况下,它会触发注销按钮 究竟可能是什么问题。

FXML 摘录

<VBox layoutX="7.0" layoutY="160.0" prefHeight="407.0" prefWidth="174.0" AnchorPane.bottomAnchor="0.0" AnchorPane.topAnchor="160.0">
  <children>
    <Button mnemonicParsing="false" prefHeight="26.0" prefWidth="180.0" text="Add Account" textFill="#c94949" underline="true" />
    <Button defaultButton="true" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="180.0" text="Account List" textAlignment="CENTER" underline="true" wrapText="true">
    <VBox.margin>
    <Insets top="10.0" />
     </VBox.margin>
    </Button>
    <Button fx:id="logoutButton" onAction="#logoutButtonAccount" defaultButton="true" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="180.0" text="Logout" textAlignment="CENTER" underline="true" wrapText="true">
    <VBox.margin>
   <Insets top="10.0" />
  </VBox.margin>
  </Button>
  </children>
  </VBox>

调用的函数

    public void logoutButtonAccount(ActionEvent actionEvent) {

        loginManager.logout();
        println(actionEvent.getSource().toString());

    }

但在我的应用程序上随机按 Enter 会触发此操作并注销应用程序。非常感谢任何帮助或建议。

我试图打印出事件源它仍然说它来自注销按钮

输出:

Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'

这些是在不点击按钮的情况下多次按下回车后的结果。 提前感谢您的建议或建议。

【问题讨论】:

  • TL;DR 在我看来,您的按钮已设置为默认值,因此每当您按 Enter 键时,都会调用 logoutButtonAccount
  • 谢谢@zlakad。有效!让我将此帖子标记为已回答
  • 继续吧,祝你好运……我不在乎名誉。

标签: java javafx javafx-8


【解决方案1】:

属性值 defaultButton="true" 是导致它的原因。

改成false或者去掉属性就解决了。

    <Button fx:id="logoutButton" onAction="#logoutButtonAccount" defaultButton="false" layoutX="10.0" layoutY="10.0" mnemonicParsing="false" prefHeight="26.0" prefWidth="180.0" text="Logout" textAlignment="CENTER" underline="true" wrapText="true">

 <VBox.margin>
    <Insets top="10.0" />
 </VBox.margin>

</Button>

正如@zlakad 注意到的那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-02-09
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 2016-10-25
    相关资源
    最近更新 更多