Mac OSX Netbeans JAVA
目标:二十一点计划...我试图让扑克牌的 imageIcon 显示在 JLabel 中
逻辑:我创建了一些 CARD 对象,其中包含返回与其关联的 imageIcon 的方法。在我的主要 GUI 类中,如果我创建指定文件位置的新 imageIcon,它就可以工作 -
private void newGame(){
String temp1, temp2, temp3, temp4;
card1 = hand.dealHand();
card2 = hand.dealHand();
card3 = hand.dealHand();
card4 = hand.dealHand();
image1 = new ImageIcon();
image1 = card1.getImage();
//Creates DeckImage and Logo as JLabel and adds it to userPanel
//image1 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/1.png");
card1Label = new JLabel(image1, JLabel.LEFT);
userPanel.add(card1Label);
card1Label.setText("");
}
注释掉的行可以正常工作并显示 imageIcon 图像,但是当我使用 card1.getImage() 方法时,图像不会显示。方法很简单——
public ImageIcon getImage(){
return this.image;
}
此外,如果您需要它,这里是从 DECK 类创建 CARD 的方法 -
private ImageIcon C1,C2, ... ,C52;
private ImageIcon[] imageArray= { C1,C2,...,C52 };
C1 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/1.png");
...
C52 = new ImageIcon("/Users/philhunter/NetBeansProjects/PractingProgramming/src/Resources/52.png");
int SUITS = suit.length;
int RANKS = rank.length;
int N = SUITS * RANKS;
//Creates a deck of 52 CARD objects
theDeck = new CARD[N];
for (int i = 0; i < RANKS; i++) {
for (int j = 0; j < SUITS; j++) {
//deck[SUITS*i + j] = rank[i] + " of " + suit[j];
card = new CARD(suit[j], rank[i], value[i], imageArray[SUITS*i + j]);
theDeck[SUITS*i + j] = card;
}
}
所以我的问题是为什么卡片 imageIcon 不显示?(我没有收到错误消息)