目前我正在尝试制作视频扑克。到目前为止,它由 2 个类组成:具有 int 值的卡片、字符套装和布尔检查。卡片是一个 JButton。然后用一堆卡片对象进行甲板。
VideoPoker 类中的 JButton 在我绘制卡片时不会更新 ImageIcon ,而且我一生都无法弄清楚为什么。当我想要背景图像时它会更新它,然后当我想要原始图像时它会再次更新,那为什么不换一张新卡呢?
下面是我的代码
import javax.swing.*;
import java.awt.event.*;
public class VideoPoker extends JPanel implements ActionListener {
private Deck deck;
private Card[] cards;
private JButton draw;
private final int MAXCARDS = 5;
public VideoPoker() {
deck = new Deck();
cards = new Card[MAXCARDS];
for(int i = 0; i < MAXCARDS; i++) {
cards[i] = deck.Draw();
cards[i].addActionListener(this);
add(cards[i]);
cards[i].setIcon(new ImageIcon
("Cards/" + cards[i].getValue() +
cards[i].getSuit() + ".png"));
}
draw = new JButton("Draw");
draw.addActionListener(this);
add(draw);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == draw) {
int checked = 0;
for(int i = 0; i < MAXCARDS; i++) {
if(cards[i].getChecked()) {
cards[i] = deck.Draw();
cards[i].setIcon(new ImageIcon
("Cards/" + cards[i].getValue() +
cards[i].getSuit() + ".png"));
checked++;
}
}
}
if(e.getSource() instanceof Card) {
Card source = (Card)e.getSource();
if(!source.getChecked()) {
source.setChecked(true);
source.setIcon(new ImageIcon("Cards/back.png"));
}
else {
source.setChecked(false);
source.setIcon(new ImageIcon
("Cards/" + source.getValue() +
source.getSuit() + ".png"));
}
}
}