[英]ImageIcon won't display
Mac OSX Netbeans JAVA
目標:BlackJack程序...我試圖獲取紙牌的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無法顯示? (我沒有收到任何錯誤消息)
好的,所以我發現了問題。 我沒有使用imageIcons正確初始化數組imageArray []。 愚蠢的錯誤,但我想如果其他人也犯了這個愚蠢的錯誤,我將不予理會。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.