我很难让它正常工作。当用户点击一张卡片时,它应该显示一张图片。这是游戏中与点击动作有关的部分:
ArrayList<CardFace> aFaces = mod.getCards();
for (int i = 0; i < ConcentrationModel.BOARD_SIZE ; i++) {
for (int j = 0; j < ConcentrationModel.BOARD_SIZE ; j++) {
int index = (i * ConcentrationModel.BOARD_SIZE) + j;
Card fc = (Card)aFaces.get(index);
Button pic = new Button("",new ImageView(fc.getImage()));
grid.add(pic, i, j);
pic.setScaleX(1);
pic.setScaleY(1);
pic.setOnAction(event -> System.out.println("Image clicked!!!"));
}
}
方法pic.setOnAction(event->..)是应该执行操作的地方。我应该采取什么方法?
您可以创建一个包含ImageView
的新Stage
来显示您的图片:
public class App extends Application {
private Stage imageStage;
private ImageView imageView;
@Override
public void start(Stage primaryStage) throws IOException {
imageView = new ImageView();
imageStage = new Stage();
imageStage.setScene(new Scene(new StackPane(imageView)));
Button btn = createButton(yourImage);
VBox root = new VBox(btn);
Scene scene = new Scene(root, 800, 800);
primaryStage.setScene(scene);
primaryStage.show();
}
private Button createButton(Image image) {
Button button = new Button("", new ImageView(image));
button.setOnAction(e -> {
imageView.setImage(image);
imageStage.show();
});
return button;
}
public static void main(String[] args) {
launch(args);
}
}