【发布时间】: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。有效!让我将此帖子标记为已回答
-
继续吧,祝你好运……我不在乎名誉。