我是 JavaFX 新手,在使用 setOnMouseClicked 方法时遇到问题。在下面的代码中,我尝试在嵌套循环中添加一个事件处理程序,以分配每个创建的矩形事件处理程序,单击鼠标,将矩形变为灰色。它似乎根本不起作用,我不知道为什么。我是否缺少需要添加的内容?
public class Cinema extends Application {
@Override
public void start(Stage primaryStage) {
try {
Screen screen1 = new Screen(8, 14);
Pane root = FXMLLoader.load(getClass().getResource("Cinema.fxml"));
Scene scene = new Scene(root, 400, 400);
VBox seatHolder = new VBox(5);
seatHolder.setPrefWidth(root.getWidth());
seatHolder.setPrefHeight(root.getHeight());
seatHolder.prefWidthProperty().bind(root.widthProperty());
seatHolder.prefHeightProperty().bind(root.heightProperty());
seatHolder.setAlignment(Pos.CENTER);
for (int i = 0; i < screen1.getSeats().length; i++) {
HBox hbox = new HBox();
hbox.setSpacing(1);
hbox.setAlignment(Pos.CENTER);
for (int j = 0; j < screen1.getSeats()[i].length; j++) {
Rectangle r = new Rectangle(30, 40);
r.setOnMouseClicked(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
r.setFill(Color.GREEN);
}
});
hbox.getChildren().add(r);
r.widthProperty().bind(root.heightProperty().divide(15));
r.heightProperty().bind(root.widthProperty().divide(30));
if (screen1.getSeats()[i][j] != null) {
r.setFill(Color.RED);
}
else {
r.setFill(Color.BLUE);
}
}
seatHolder.getChildren().add(hbox);
}
}
}
30秒到达战场
RISEBY
相关分类