[英]Set the Jlabel to respond to the Jbuttons, but when I click on the button nothing is responding
I set the button according to the 1-13 numbers on game card. 我根据游戏卡上的1-13数字设置按钮。 When I click on a button I want it to randomly draw a card from the deck.
当我单击一个按钮时,我希望它从卡组中随机抽取一张牌。 If the card doesn't match the button # then I can try another button, if the card match the button # then the game ends.
如果卡与按钮#不匹配,那么我可以尝试另一个按钮;如果卡与按钮#不匹配,则游戏结束。 The program will keep running through 52 cards as long as the button and the card doesn't match.
只要按钮和卡片不匹配,该程序将继续运行52张卡片。 lblpic is where the card image suppose to appear when the user click one of the number button.
lblpic是用户单击数字按钮之一时应该显示的名片图像的位置。 I can't get the buttons to work properly.
我无法使按钮正常工作。 I fixed some codes in my Card game, but it's still not working.
我在纸牌游戏中修复了一些代码,但仍然无法使用。 I also can't get the card image to show up when the user click on a number button.
当用户单击数字按钮时,我也无法显示名片图像。 I tried linking the ImageLoader class to the CardGame Jframe, but it's not wokring.
我尝试将ImageLoader类链接到CardGame Jframe,但它没有问题。
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!");
}
}
}
}
First, I would put aside Window Builder, it's not doing you any favors. 首先,我将Window Builder放在一旁,它并没有给您任何帮助。
Next, you need to attach an ActionListener
to EACH button, in your case, you can use the same instance of ActionListener
for each button as it will basically be doing the same thing. 接下来,您需要将一个
ActionListener
附加到每个按钮,在这种情况下,您可以为每个按钮使用相同的ActionListener
实例,因为它基本上会执行相同的操作。
Next, when the ActionListener
is triggered, you need to ascertain which button was clicked. 接下来,触发
ActionListener
时,您需要确定单击了哪个按钮。 There's are number of ways to do this, but in your case, the actionCommand
of the ActionEvent
will be the text of the button, which is a number (in String
format), so we can use that. 有很多方法可以执行此操作,但是在您的情况下,
ActionEvent
的actionCommand
将是按钮的文本,它是一个数字(采用String
格式),因此我们可以使用它。 Next, we need to calculate the card that the button represents, this is harder, as there are 52 cards, but only 13 cards. 接下来,我们需要计算按钮代表的卡,这比较困难,因为有52张卡,但只有13张卡。 Assuming we only care about the value of the card and not it's face, we can use the
Card
from the deck to determine the suit. 假设我们只关心卡的价值,而不是它的脸,我们可以使用的
Card
从甲板,以确定诉讼。
Once we have both Card
s, we can compare them, for that, you could simply use the equals
method of the Card
一旦拥有两个
Card
,就可以对其进行比较,为此,您可以简单地使用Card
的equals
方法
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;
}
}
private class BtnNewButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card card = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (card.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + card);
if (card.equals(match)) {
System.out.println("Winner");
} else {
System.out.println("Loser");
}
}
}
And finally, a runnable example, because that's a lot of information to take in... 最后,是一个可运行的示例,因为要吸收很多信息...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Stack;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class CardGame {
private JFrame frame;
private JLabel lblA;
private Deck B = new Deck(); //B have 52 cards, and shuffled
/**
* Launch the application.
*/
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();
}
}
});
}
/**
* Create the application.
*/
public CardGame() {
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);
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(4, 4, 4, 4);
gbc.ipadx = 15;
gbc.fill = gbc.HORIZONTAL;
for (int index = 0; index < 13; index++) {
System.out.println(index + " - " + (index % 3));
gbc.gridx = index / 6;
gbc.gridy = index % 6;
JButton btn1 = new JButton(Integer.toString(index + 1));
btn1.addActionListener(new BtnNewButtonActionListener());
frame.getContentPane().add(btn1, gbc);
}
}
private class BtnNewButtonActionListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
Card card = B.pop(); //Card one is from Deck B, need to compare to the button
int actionValue = Integer.parseInt(e.getActionCommand());
int cardValue = (actionValue - 1) + (card.getSuitNumber() * 13);
Card match = new Card(cardValue);
System.out.println("Match " + match + " to " + card);
if (card.equals(match)) {
System.out.println("Winner");
} else {
System.out.println("Loser");
}
}
}
public static 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;
}
@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;
}
}
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());
}
}
}
}
Have a look at How to Use Buttons, Check Boxes, and Radio Buttons , How to Write an Action Listeners , Laying Out Components Within a Container and How to Use GridBagLayout for more details 看看如何使用按钮,复选框和单选按钮 , 如何编写动作侦听器 , 在容器内布置组件以及如何使用GridBagLayout了解更多详细信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.