为二维区域数组中的每个区域分配一个 MouseListener
Assigning a MouseListener to each Region in a 2d array of Regions
几周前,我在这里发布了一个问题,寻求有关制作国际象棋游戏的帮助,我得到了很好的回应和一些代码,这些代码演示了我的问题的解决方案。我一直在尝试剖析代码并研究它,以便更好地理解它是如何工作的。在执行此操作时,我 运行 遇到了一个我似乎无法找到答案的问题。棋盘由二维区域数组组成,我试图向二维数组中的每个区域添加一个 MouseListener,这样当按下一个区域时,它将打印出被按下区域的行和列。现在,当我按下屏幕上的一个方块时,什么也没有发生,我无法弄清楚为什么我的 MouseListener 不工作。
public class Main extends Application {
GridPane root = new GridPane();
final int size = 8;
int col = 0;
int row = 0;
@Override
public void start(Stage primaryStage) {
try {
GridPane board = new GridPane();
Region[][] fields = new Region[8][8];
for (int i = 0; i < fields.length; i++) {
Region[] flds = fields[i];
for (int j = 0; j < flds.length; j++) {
Region field = new Region();
flds[j] = field;
field.setBackground(new Background(new BackgroundFill((i + j) % 2 == 0 ? Color.WHITE : Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY)));
}
board.addRow(i, flds);
}
for (int i = 0; i < fields.length; i++) {
col = i;
for (int j = 0; j < fields[i].length; j++) {
row = j;
fields[i][j].setOnMouseClicked(e->{
System.out.println("Col:" + col + ", Row" + row);
});
}
}
// use 1/8 of the size of the Grid for each field
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setPercentHeight(100d / 8);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(100d / 8);
for (int i = 0; i < 8; i++) {
board.getColumnConstraints().add(columnConstraints);
board.getRowConstraints().add(rowConstraints);
}
Pane piecePane = new Pane();
StackPane root = new StackPane(board, piecePane);
// NumberBinding boardSize = Bindings.min(root.widthProperty(), root.heightProperty());
NumberBinding boardSize = Bindings.min(root.widthProperty(), root.heightProperty());
// board size should be as large as possible but at most the min of the parent sizes
board.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE);
// same size for piecePane
piecePane.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE);
piecePane.maxWidthProperty().bind(boardSize);
piecePane.maxHeightProperty().bind(boardSize);
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我自己在 JavaFX 方面的工作不多,尽管它与 java swing 非常相似,关于基本 "collision"。
有一个 rectangle class with the methods contains
and intersects
. If you use MouseEvent 可以知道鼠标坐标以及何时单击鼠标。你可以这样做:
if(mouse.isClicked() && rect.contains(mouse.x, mouse.y) { // do stuff }
尽管如此,您需要为所有正方形创建矩形对象。希望一切顺利。
编辑: 注意到 Regions 还包含方法 contains
和 intersects
- 事先尝试这些方法,仅在必须时使用矩形跳过太多对象创建。
变化:
StackPane root = new StackPane(board,piecePane);
至:
StackPane root = new StackPane(piecePane,board);
?
因为 board
在 piecePane
之后,它无法接收事件。
在您使用 全局变量 col
和 row
之前,所以这些变量始终具有相同的值 7
和 7
. 运行 循环那些改变它们值的变量,但最后它们的值是 7
7
,这里我们需要使用 局部变量 col
和 row
,所以你需要添加这个修改:
for (int i = 0; i < fields.length; i++) {
int col = i;
for (int j = 0; j < fields[i].length; j++) {
int row = j;
fields[i][j].setOnMouseClicked(e -> System.out.println("Col:" + col + ", Row" + row));
}
}
几周前,我在这里发布了一个问题,寻求有关制作国际象棋游戏的帮助,我得到了很好的回应和一些代码,这些代码演示了我的问题的解决方案。我一直在尝试剖析代码并研究它,以便更好地理解它是如何工作的。在执行此操作时,我 运行 遇到了一个我似乎无法找到答案的问题。棋盘由二维区域数组组成,我试图向二维数组中的每个区域添加一个 MouseListener,这样当按下一个区域时,它将打印出被按下区域的行和列。现在,当我按下屏幕上的一个方块时,什么也没有发生,我无法弄清楚为什么我的 MouseListener 不工作。
public class Main extends Application {
GridPane root = new GridPane();
final int size = 8;
int col = 0;
int row = 0;
@Override
public void start(Stage primaryStage) {
try {
GridPane board = new GridPane();
Region[][] fields = new Region[8][8];
for (int i = 0; i < fields.length; i++) {
Region[] flds = fields[i];
for (int j = 0; j < flds.length; j++) {
Region field = new Region();
flds[j] = field;
field.setBackground(new Background(new BackgroundFill((i + j) % 2 == 0 ? Color.WHITE : Color.LIGHTBLUE, CornerRadii.EMPTY, Insets.EMPTY)));
}
board.addRow(i, flds);
}
for (int i = 0; i < fields.length; i++) {
col = i;
for (int j = 0; j < fields[i].length; j++) {
row = j;
fields[i][j].setOnMouseClicked(e->{
System.out.println("Col:" + col + ", Row" + row);
});
}
}
// use 1/8 of the size of the Grid for each field
RowConstraints rowConstraints = new RowConstraints();
rowConstraints.setPercentHeight(100d / 8);
ColumnConstraints columnConstraints = new ColumnConstraints();
columnConstraints.setPercentWidth(100d / 8);
for (int i = 0; i < 8; i++) {
board.getColumnConstraints().add(columnConstraints);
board.getRowConstraints().add(rowConstraints);
}
Pane piecePane = new Pane();
StackPane root = new StackPane(board, piecePane);
// NumberBinding boardSize = Bindings.min(root.widthProperty(), root.heightProperty());
NumberBinding boardSize = Bindings.min(root.widthProperty(), root.heightProperty());
// board size should be as large as possible but at most the min of the parent sizes
board.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE);
// same size for piecePane
piecePane.setPrefSize(Double.MAX_VALUE, Double.MAX_VALUE);
piecePane.maxWidthProperty().bind(boardSize);
piecePane.maxHeightProperty().bind(boardSize);
Scene scene = new Scene(root,400,400);
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
我自己在 JavaFX 方面的工作不多,尽管它与 java swing 非常相似,关于基本 "collision"。
有一个 rectangle class with the methods contains
and intersects
. If you use MouseEvent 可以知道鼠标坐标以及何时单击鼠标。你可以这样做:
if(mouse.isClicked() && rect.contains(mouse.x, mouse.y) { // do stuff }
尽管如此,您需要为所有正方形创建矩形对象。希望一切顺利。
编辑: 注意到 Regions 还包含方法 contains
和 intersects
- 事先尝试这些方法,仅在必须时使用矩形跳过太多对象创建。
变化:
StackPane root = new StackPane(board,piecePane);
至:
StackPane root = new StackPane(piecePane,board);
?
因为 board
在 piecePane
之后,它无法接收事件。
在您使用 全局变量 col
和 row
之前,所以这些变量始终具有相同的值 7
和 7
. 运行 循环那些改变它们值的变量,但最后它们的值是 7
7
,这里我们需要使用 局部变量 col
和 row
,所以你需要添加这个修改:
for (int i = 0; i < fields.length; i++) {
int col = i;
for (int j = 0; j < fields[i].length; j++) {
int row = j;
fields[i][j].setOnMouseClicked(e -> System.out.println("Col:" + col + ", Row" + row));
}
}