在我的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会触发此操作并注销该应用程序。任何帮助或建议都将受到高度赞赏。
我试图打印出事件源,但仍然说它是来自logoutButton
输出:
Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'
Button[id=logoutButton, styleClass=button]'Logout'
这些是多次按Enter键而不单击按钮后的结果。 谢谢您的建议或建议。
答案 0 :(得分:2)
导致它的原因是属性值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注意到的那样。