目前我正在嘗試製作視頻撲克。到目前爲止,它由2個類組成: 具有int值,char套裝和檢查布爾值的卡片。卡是JButton。然後用一堆卡片對象裝飾甲板。爲什麼我的JButton不更新imageicon?
將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"));
}
}
}
你有任何錯誤消息,堆棧跟蹤或任何? – mthmulders 2013-04-09 07:28:20
不,沒有。在添加(e.getSource()instanceof Card)之前,我得到一個ClassCastException。但我認爲這是無關緊要的,所以不是。 – Eric 2013-04-09 07:31:53
您是否真的到過卡片被「翻轉」的地方以顯示其原始圖像? – mthmulders 2013-04-09 07:35:49