gpt4 book ai didi

java - 内存游戏: disable JToggleButton after a match

转载 作者:行者123 更新时间:2023-12-02 09:06:01 25 4
gpt4 key购买 nike

我目前正在尝试编写一个内存/配对游戏。我在尝试弄清楚如何在用户找到匹配的卡片后禁用该按钮时遇到问题。我知道我需要存储第一次点击的值并将其与第二次点击进行比较,但仍然不确定它是如何不起作用的...如果有人可以提供帮助,我们将非常感激。谢谢。

考虑这段代码,我重新创建了一个更简单的游戏版本,您可以在下面实际运行它:

主要:

public static void main(String[] args) {
start start = new start();
start.main();
}

}

框架类:

class start {

JToggleButton DisplayCards[][] = new JToggleButton[4][4];
Shuffle shuffle = new Shuffle();

void main() {
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
int y = 20;
int x = 60;

for (int i = 0; i < DisplayCards.length; ++i) {
for (int j = 0; j < DisplayCards[i].length; ++j) {
DisplayCards[i][j] = new JToggleButton("Click Me!");

DisplayCards[i][j].setBounds(x, y, 90, 126);
y = y + 135;
if (y >= 540) {
y = 20;
x = x + 120;
}
frame.add(DisplayCards[i][j]);
DisplayCards[i][j].addActionListener(new Clicked(i, j, shuffle));
}
}

frame.setLayout(null);
frame.setVisible(true);
}
}

打乱数组的类:

class Shuffle {

String[][] cards = {
{"A", "B", "C", "D"},
{"E", "F", "G", "H"},
{"A", "B", "C", "D"},
{"E", "F", "G", "H"}

};

public void random() {
for (int i = 0; i < cards.length; i++) {
for (int j = 0; j < cards[i].length; j++) {
int i1 = (int) (Math.random() * cards.length);
int j1 = (int) (Math.random() * cards[i].length);

String temp = cards[i][j];
cards[i][j] = cards[i1][j1];
cards[i1][j1] = temp;
}

}
}
}

ActionListener 类:

class Clicked implements ActionListener {

String first;
start matching = new start();
boolean state = false;
Shuffle shuffle;
JToggleButton tBtn;
private int i;
private int j;
int posI;
int posJ;

public Clicked(int i, int j, Shuffle shuffle) {
this.i = i;
this.j = j;
this.shuffle = shuffle;
}

public void actionPerformed(ActionEvent e) {
tBtn = (JToggleButton) e.getSource();
if (tBtn.isSelected()) {
tBtn.setText(shuffle.cards[i][j]);
if (!state) {
first = shuffle.cards[i][j];
posI = i;
posJ = j;
System.out.println(first);
state = true;

} else {
if (first.equals(shuffle.cards[i][j])) {
tBtn.setEnabled(false);
System.out.println("Correct!");
} else if (!first.equals(shuffle.cards[i][j])) {
System.out.println("Not it");
tBtn.setText("Click Me!");
matching.DisplayCards[posI][posJ].setText("Click Me!");
first = null;
posI = 0;
posJ = 0;
state = false;
}
}
} else {
tBtn.setText("Click Me!");
}
}
}

最佳答案

以下是单文件 mre(将整个代码复制粘贴到“Start.java”中并运行)。
请注意评论:

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JToggleButton;

//Java Naming Conventions: https://www.geeksforgeeks.org/java-naming-conventions/
public class Start {

JToggleButton displayCards[][] = new JToggleButton[4][4];
Shuffle shuffle = new Shuffle();
private static final int NO_PREVIOUS = -1;
//static fileds for i,j of previous button clicked
private int previousI = NO_PREVIOUS, previousJ = NO_PREVIOUS;

void main() {
JFrame frame = new JFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 600);
int y = 20, x = 60;

for (int i = 0; i < displayCards.length; ++i) {
for (int j = 0; j < displayCards[i].length; ++j) {
displayCards[i][j] = new JToggleButton("Click Me!");

displayCards[i][j].setBounds(x, y, 90, 126);
y = y + 135;
if (y >= 540) {
y = 20;
x = x + 120;
}
frame.add(displayCards[i][j]);
displayCards[i][j].addActionListener(new Clicked(i, j, shuffle));
}
}

frame.setLayout(null); //avoid null layout :https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html
frame.setVisible(true);
}

public static void main(String[] args) {
Start Start = new Start();
Start.main();
}

//to have easy access to Start members make the ActionListener an inner class
class Clicked implements ActionListener {

private static final String HIDE_TEXT = "Click Me!";
private final Shuffle shuffle;
private final int i, j;

public Clicked(int i, int j, Shuffle shuffle) {
this.i = i;
this.j = j;
this.shuffle = shuffle;
}

@Override
public void actionPerformed(ActionEvent e) {

JToggleButton tBtn = (JToggleButton) e.getSource();
showButtonText(tBtn, i, j);

if( previousI == NO_PREVIOUS || previousJ == NO_PREVIOUS){ //no previous selected btn
previousI = i;
previousJ = j;
return;
}

JToggleButton previous = displayCards[previousI][previousJ]; //reference to previous button clicked

if(match(tBtn, previous)){
matchFound(tBtn, previous);
}else{
matchNotFound(tBtn, previous);
}
}

private void showButtonText(JToggleButton btn, int i, int j) {
btn.setText(shuffle.cards[i][j]);
btn.setEnabled(false);
}

private void resetButton(JToggleButton btn) {
btn.setForeground(Color.BLACK);
btn.setText(HIDE_TEXT);
btn.setSelected(false);
btn.setEnabled(true);
}

private boolean match(JToggleButton btn1, JToggleButton btn2) {
return btn1.getText().equals(btn2.getText());
}

private void matchFound(JToggleButton btn1, JToggleButton btn2) {
System.out.println("Correct!");
btn1.setEnabled(false);
btn2.setEnabled(false);
previousI = NO_PREVIOUS ; previousJ = NO_PREVIOUS;
}

private void matchNotFound(JToggleButton tBtn, JToggleButton previous) {
System.out.println("No match");
resetButton(previous);
previousI = i ; previousJ = j;
}
}
}

class Shuffle {

String[][] cards = {
{"A", "B", "C", "D"},
{"E", "F", "G", "H"},
{"A", "B", "C", "D"},
{"E", "F", "G", "H"}

};

public void random() {
for (int i = 0; i < cards.length; i++) {
for (int j = 0; j < cards[i].length; j++) {
int i1 = (int) (Math.random() * cards.length);
int j1 = (int) (Math.random() * cards[i].length);

String temp = cards[i][j];
cards[i][j] = cards[i1][j1];
cards[i1][j1] = temp;
}

}
}
}

<小时/>使用 GridLayout 代替空布局的更好实现:

public class Start {

//static fileds for i,j of previous button clicked
private int previousI = NO_PREVIOUS, previousJ = NO_PREVIOUS;
private static final String HIDE_TEXT = "Click Me!";
private static final int W = 96, H = 126, NO_PREVIOUS = -1, SIZE = 4, GAP = 5;

JToggleButton displayCards[][] = new JToggleButton[SIZE][SIZE];
Shuffle shuffle = new Shuffle();

Start() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel board = new JPanel();
board.setLayout(new GridLayout(displayCards.length, displayCards[0].length, GAP, GAP));

for (int i = 0; i < displayCards.length; ++i) {
for (int j = 0; j < displayCards[i].length; ++j) {
JToggleButton btn = new JToggleButton(HIDE_TEXT);
btn.setPreferredSize(new Dimension(W,H));
btn.addActionListener(new Clicked(i, j, shuffle));
displayCards[i][j] = btn;
board.add(btn);
}
}

frame.add(board);
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) {
new Start();
}

//to have easy access to Start members make the ActionListener an inner class
class Clicked implements ActionListener {

private final Shuffle shuffle;
private final int i, j;

public Clicked(int i, int j, Shuffle shuffle) {
this.i = i;
this.j = j;
this.shuffle = shuffle;
}

@Override
public void actionPerformed(ActionEvent e) {

JToggleButton tBtn = (JToggleButton) e.getSource();
showButtonText(tBtn, i, j);

if( previousI == NO_PREVIOUS || previousJ == NO_PREVIOUS){ //no previous selected btn
previousI = i;
previousJ = j;
return;
}

JToggleButton previous = displayCards[previousI][previousJ]; //reference to previous button clicked

if(match(tBtn, previous)){
matchFound(tBtn, previous);
}else{
matchNotFound(tBtn, previous);
}
}

private void showButtonText(JToggleButton btn, int i, int j) {
btn.setText(shuffle.cards[i][j]);
btn.setEnabled(false);
}

private void resetButton(JToggleButton btn) {
btn.setForeground(Color.BLACK);
btn.setText(HIDE_TEXT);
btn.setSelected(false);
btn.setEnabled(true);
}

private boolean match(JToggleButton btn1, JToggleButton btn2) {
return btn1.getText().equals(btn2.getText());
}

private void matchFound(JToggleButton btn1, JToggleButton btn2) {
System.out.println("Correct!");
btn1.setEnabled(false);
btn2.setEnabled(false);
previousI = NO_PREVIOUS ; previousJ = NO_PREVIOUS;
}

private void matchNotFound(JToggleButton tBtn, JToggleButton previous) {
System.out.println("No match");
resetButton(previous);
previousI = i ; previousJ = j;
}
}
}

关于java - 内存游戏: disable JToggleButton after a match,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59806175/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com