我根据游戏卡上的1-13号码设置了按钮。当我点击一个按钮时,我希望它从卡座上随机抽取一张卡片。如果卡与#按钮不匹配,那么我可以尝试另一个按钮,如果卡与按钮#匹配则游戏结束。只要按钮和卡不匹配,程序将持续运行52张卡。 lblpic是用户单击其中一个数字按钮时出现的卡片图像。我无法让按钮正常工作。我在我的纸牌游戏中修改了一些代码,但它仍然无效。当用户点击数字按钮时,我也无法显示卡片图像。我尝试将ImageLoader类链接到CardGame Jframe,但它没有w。
ImageLoader的
import javax.swing.ImageIcon;
public class ImageLoader {
public final ImageIcon BACK = new ImageIcon("img/backbluepattern.gif");
public ImageIcon[][] cardImg = new ImageIcon[4][13];
public final String[] SUITS = {"clubs", "hearts", "spades", "diamonds"};
public ImageLoader() {
for (int i = 0; i < 4; i++) {
for (int j = 1; j <= 13; j++) {
String strBuf = "img/" + SUITS[i] + j + ".gif";
cardImg[i][j-1] = new ImageIcon(strBuf);
}
}
}
}
Card
public class Card {
//Numerical equivalent of the suit and face
private int suitNum; // valid range is 0 - 3
private int faceNum; // valid range is 0 - 12
//For converting between names and numbers
public static final String[] SUITS = {"Clubs", "Hearts", "Spades", "Diamonds"};
public static final String[] FACES = {"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};
//Constructor takes combined number and splits it to suit and face numbers
public Card(int num) {
if (num > 51) {
System.out.println("Input number is larger than 51.");
System.exit(0);
}
suitNum = num / 13;
faceNum = num % 13;
}
// Return a calculated value that combines suit and face numbers
public int getTotalNumber() {
return (this.suitNum * 13 + this.faceNum);
}
public int getFaceNumber() {
return faceNum;
}
public int getSuitNumber() {
return suitNum;
}
public String toString() {
int num = getTotalNumber();
String outStr = FACES[num % 13];
outStr += " of ";
outStr += SUITS[num / 13];
return outStr;
}
}
Deck
import java.util.Collections;
import java.util.Stack;
public class Deck extends Stack<Card>{
//pop method already in the stack
// Create a new shuffled deck
public Deck() {
for (int ii = 0; ii < 52; ii++) {
push(new Card(ii));
}
Collections.shuffle(this);
}
// For debug purposes
public void printDeck() {
for (int ii = 0; ii < 52; ii++) {
System.out.println(get(ii).toString());
}
}
}
CardGame
import java.awt.EventQueue;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;
public CardGame() {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
CardGame window = new CardGame();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
lblpic = new JLabel("");
Image img = new ImageIcon BACK(this getClass().getResource("img/backbluepattern.gif")).getImage();
lblpic.getIcon(new ImageIcon(img));
JButton btn1 = new JButton("1");
btn1.setBounds(4, 13, 97, 25);
btn1.addActionListener(new Btn1ActionListener());
frame.getContentPane().setLayout(null);
frame.getContentPane().add(btn1);
JButton btn2 = new JButton("2");
btn2.setBounds(4, 51, 97, 25);
btn2.addActionListener(new Btn2ActionListener());
frame.getContentPane().add(btn2);
JButton btn3 = new JButton("3");
btn3.setBounds(4, 89, 97, 25);
btn3.addActionListener(new Btn3ActionListener());
frame.getContentPane().add(btn3);
JButton btn4 = new JButton("4");
btn4.setBounds(4, 127, 97, 25);
btn4.addActionListener(new Btn4ActionListener());
frame.getContentPane().add(btn4);
JButton btn5 = new JButton("5");
btn5.setBounds(4, 165, 97, 25);
btn5.addActionListener(new Btn5ActionListener());
frame.getContentPane().add(btn5);
JButton btn6 = new JButton("6");
btn6.setBounds(4, 203, 97, 25);
btn6.addActionListener(new Btn6ActionListener());
frame.getContentPane().add(btn6);
JButton btn7 = new JButton("7");
btn7.setBounds(113, 13, 97, 25);
btn7.addActionListener(new Btn7ActionListener());
frame.getContentPane().add(btn7);
JButton btn8 = new JButton("8");
btn8.setBounds(113, 51, 97, 25);
btn8.addActionListener(new Btn8ActionListener());
frame.getContentPane().add(btn8);
JButton btn9 = new JButton("9");
btn9.setBounds(113, 89, 97, 25);
btn9.addActionListener(new Btn9ActionListener());
frame.getContentPane().add(btn9);
JButton btn10 = new JButton("10");
btn10.setBounds(113, 127, 97, 25);
btn10.addActionListener(new Btn10ActionListener());
frame.getContentPane().add(btn10);
JButton btn11 = new JButton("11");
btn11.setBounds(113, 165, 97, 25);
btn11.addActionListener(new Btn11ActionListener());
frame.getContentPane().add(btn11);
JButton btn12 = new JButton("12");
btn12.setBounds(113, 203, 97, 25);
btn12.addActionListener(new Btn12ActionListener());
frame.getContentPane().add(btn12);
JButton btn13 = new JButton("13");
btn13.setBounds(222, 13, 97, 25);
btn13.addActionListener(new Btn13ActionListener());
frame.getContentPane().add(btn13);
lblpic = new JLabel("");
lblpic.setBounds(222, 51, 115, 177);
frame.getContentPane().add(lblpic);
}
private class Btn1ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn2ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn3ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn4ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn5ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn6ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn7ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn8ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn9ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn10ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn11ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn12ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
private class Btn13ActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card One = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (One.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + One);
if (One.equals(match)) {
System.out.println("Game Over!");
} else {
System.out.println("Try Again!");
}
}
}
}
答案 0 :(得分:1)
首先,我会抛开Window Builder,它没有给你任何好处。
接下来,您需要将ActionListener
附加到EACH按钮,在您的情况下,您可以为每个按钮使用相同的ActionListener
实例,因为它基本上会做同样的事情。
接下来,当触发ActionListener
时,您需要确定单击了哪个按钮。有多种方法可以执行此操作,但在您的情况下,actionCommand
的{{1}}将是按钮的文本,这是一个数字(在ActionEvent
中格式),所以我们可以使用它。接下来,我们需要计算按钮代表的卡,这更难,因为有52张卡,但只有13张卡。假设我们只关心卡片的价值而不关心卡片的价值,我们可以使用卡片中的String
来确定卡片。
一旦我们同时拥有Card
个,我们就可以对它们进行比较,为此,您只需使用Card
的{{1}}方法
equals
Card
最后,这是一个可运行的例子,因为这需要很多信息...
public class Card {
//...
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.suitNum;
hash = 97 * hash + this.faceNum;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Card other = (Card) obj;
if (this.suitNum != other.suitNum) {
return false;
}
if (this.faceNum != other.faceNum) {
return false;
}
return true;
}
}
有关详细信息,请查看How to Use Buttons, Check Boxes, and Radio Buttons,How to Write an Action Listeners,Laying Out Components Within a Container和How to Use GridBagLayout