我正在开发Javafx Minesweeper游戏,目前仅使用鼠标左键输入。我也想使用鼠标右键,以便用户可以标记可能的炸弹。我查看了Oracle网站上的Button类,它说:
“当按下按钮并释放时,将发送一个ActionEvent。您的应用程序可以通过实现EventHandler处理ActionEvent来基于此事件执行某些操作。按钮还可以通过实现EventHandler处理MouseEvent来响应鼠标事件。”
https://docs.oracle.com/javafx/2/api/javafx/scene/control/Button.html
我已经尝试了几种不同的方法,但都没有成功。
包括我当前的EventHandler代码。如果有人能解释处理鼠标右键/鼠标单击的最佳方法,或者向我指出在哪里可以找到该信息的正确方向,我们将不胜感激。
MineButton是扩展Button的自定义类。我想右键单击以标记为“ m”并更改单元格颜色,而单击鼠标左键将保持不变。
for (int row = 0; row < 8; row++){
for (int col = 0; col <8; col++){
MineButton button = new MineButton(row, col);
button.setPrefSize(100, 100);
button.setText("?");
button.setStyle("-fx-font: 22 arial; -fx-base:#dcdcdc;");
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
if ( button.isAMine() == true){
button.setText("B");
for( int row1 = 0; row1 < 8; row1++){
for ( int col1 = 0; col1 < 8; col1++){
if (mineButtons[row1][col1].isAMine() == true){
mineButtons[row1][col1].setText("B");
mineButtons[row1][col1].setStyle("-fx- font: 22 arial; -fx-base: #dc143c;");
}
}
}
}
else{
recursion(mineButtons, button.getX(), button.getY());
}
}
});
如果您想处理MouseEvent,请使用此代码。它将起作用。
button.addEventFilter(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
if(event.getButton() == MouseButton.SECONDARY){
// Type code to set flag here
}
}
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句