我正在尝试用javafx制作一个记忆游戏,它工作得很好,当你发现两张相同的图片时,它会告诉你,但是我怎样才能让它在整个游戏中一直显示匹配的图片呢?因为当你不匹配的时候,如果你匹配的话,图片就会隐藏起来。我想不出一个办法,让按钮在你得到匹配后继续显示图片。
public void start(Stage primaryStage) {
ArrayList<String> list = new ArrayList<>();
int userInput = 4;
imageWithLoc = new String[userInput][userInput];
btnArr = new Button[userInput][userInput];
int targetNum = ((userInput * userInput) / 2);
int count = 0;
int count2 = 0;
while (count < targetNum) {
int numC = (int) (Math.random() * userInput);
int numR = (int) (Math.random() * userInput);
if (imageWithLoc[numC][numR] == null) {
if (!list.contains(images[count])) {
imageWithLoc[numC][numR] = images[count];
list.add(images[count]);
count++;
}
}
}
for (int i = 0; i < userInput; i++) {
for (int j = 0; j < userInput; j++) {
if (imageWithLoc[i][j] == null) {
imageWithLoc[i][j] = images[count2];
count2++;
}
}
}
for (int i = 0; i < userInput; i++) {
for (int j = 0; j < userInput; j++) {
System.out.println(imageWithLoc[i][j] + " ");
}
}
for (int i = 0; i < userInput; i++) {
for (int j = 0; j < userInput; j++) {
Button btn = new Button();
btn.setPrefSize(250, 250);
btnArr[i][j] = btn;
Image img1 = new Image(imageWithLoc[i][j], 250, 250, true, true);
String strImg = imageWithLoc[i][j];
btn.setOnAction(e -> {
ImageView iv1 = new ImageView();
iv1.setImage(img1);
btn.setGraphic(iv1);
if (firstImg == null) {
firstImg = strImg;
firstBtn = btn;
} else if (secondImg == null) {
secondImg = strImg;
secondBtn = btn;
} else if (firstImg != null && secondImg != null) {
firstImg = strImg;
secondImg = null;
secondBtn.setGraphic(null);
firstBtn.setGraphic(null);
firstBtn = btn;
secondBtn = null;
}
if (firstImg.equals(secondImg)) {
System.out.println("match");
}
});
btnGp.add(btn, j, i);
btnGp.setHgap(10);
btnGp.setVgap(10);
btnGp.setPadding(new Insets(10, 10, 10, 10));
}
}
bp.setCenter(btnGp);
Scene scene = new Scene(bp, 1000, 1000);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!