连接游戏选择卡在java中

时间:2016-05-08 16:28:05

标签: java javafx event-handling

我在尝试使其工作时遇到了麻烦。当用户抄一张卡时,它应该显示一张图片。这是游戏中与点击操作有关的部分:

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 - &gt; ...)是应该进行操作的地方。我应该采取什么方法?

1 个答案:

答案 0 :(得分:0)

您可以创建一个包含Stage的新ImageView来显示您的图片:

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);
    }
}