【发布时间】:2018-08-16 15:47:47
【问题描述】:
我正在用 javaFX 制作一个简单的国际象棋游戏。 我使用 fxml 文件创建了我的棋盘,并在每个单元格内使用了一个带有按钮的 gridPane。 当用户按下按钮时,我想获取按下按钮的列和行索引。 有没有办法做到这一点? 我正在考虑使用这样的功能,但它不起作用。
private void buttonClicked(MouseEvent event) {
Button btn = (Button) event.getSource();
String nome = btn.getId();
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(nome);
alert.setHeaderText(null);
alert.setContentText(nome);
alert.showAndWait();
}
我也做了同样的功能,试图打印父母的 id,但仍然无法正常工作。在 fxml 文件中,对该函数的调用如下:
<AnchorPane prefHeight="200.0" prefWidth="200.0" GridPane.columnIndex="1">
<children>
<Button layoutX="14.0" layoutY="19.0" mnemonicParsing="false" onAction="#buttonClicked" prefHeight="63.0" prefWidth="94.0" styleClass="blackButton" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
</children>
</AnchorPane>
【问题讨论】:
-
您不能使用将
MouseEvent作为EventHandler<ActionEvent>参数的方法。参数类型必须是与EventHandler类型参数或超类型相同类型的Event。