2012-03-08 79 views
0

我正在嘗試顯示班主教對象的ImageIcon。使用getImage()檢索ImageIcon。返回的ImageIcon存儲在引用m中,但未顯示,並且正在顯示另一個直接加載的ImageIcon h。我正在犯什麼錯誤?ImageIcon未顯示

import javax.swing.*; 

//Game.java 

public class Game { 

    public static void main(String[] args) { 
     board b = new board(); 
     bishop bis1 = new bishop(); 
     bis1.setLocation(0, 0); 
     ImageIcon m = bis1.getImage(); 
     b.squares[0][1].add(new JLabel(m)); 
     ImageIcon h = new ImageIcon("rook.png"); 
     b.squares[0][0].add(new JLabel(h)); 
    } 
} 

//bishop.java 
import javax.swing.*; 
import java.awt.*; 

public class bishop { 
    private ImageIcon img; 
    private int row; 
    private int col; 

    public void bishop() { 
     img = new ImageIcon("bishop.png"); 
    } 

    public void setLocation(int i, int j) { 
     row = i; 
     col = j; 
    } 

    public int getX() { 
     return row; 
    } 

    public int getY() { 
     return col; 
    } 

    public ImageIcon getImage() { 
     return img; 
    } 
} 

// board.java 
import javax.swing.*; 
import java.awt.*; 

public class board { 
public JFrame frame; 
public JPanel squares[][] = new JPanel[3][3]; 

public board() { 
frame = new JFrame("Simplified Chess"); 
frame.setSize(900, 400); 
frame.setLayout(new GridLayout(2,3)); 

for (int i = 0; i < 2; i++) { 
    for (int j = 0; j < 3; j++) { 
     squares[i][j] = new JPanel(); 

     if ((i + j) % 2 == 0) { 
      squares[i][j].setBackground(Color.black); 
     } else { 
      squares[i][j].setBackground(Color.white); 
     } 
     frame.add(squares[i][j]); 
    } 
    } 

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setVisible(true); 
    } 

} 
+0

Java代碼約定規定類名稱應始終以大寫字母開頭。您應該將bishop.java重命名爲Bishop.java並將board.java重命名爲Board.java。這使得代碼更易於閱讀。 – 2012-03-08 14:30:40

+0

@SteveMcLeod我很後悔命名不好的做法。請記住。 – 2012-03-08 14:39:48

+0

@deporter這兩個圖像都在同一個文件夾 – 2012-03-08 14:40:13

回答

5

您定義構造函數錯誤的方法 - 用不必要的void。因此,Bishop類將調用默認的空構造函數,因此您的變量img從未正確設置。刪除它,這樣你的構造會被正確地稱爲:

取而代之的是:

public void bishop() { 
     img = new ImageIcon("bishop.png"); 
    } 

定義它沒有無效:

public bishop() { 
      img = new ImageIcon("bishop.png"); 
     } 
+0

很好的調試.. +1 – Juvanis 2012-03-08 14:37:34

+0

這是當場。對於尷尬的錯誤感到抱歉 – 2012-03-08 14:41:16

+0

我們都這麼做,我很高興看到我能夠提供幫助。 – 2012-03-08 14:56:56

1

沒有足夠的代碼顯示給我看,我需要看到董事會類(順便說一句:班級名稱應該在Java中使用大寫:Board.java)

但我猜測這是關於董事會類的董事會佈局的方式。

你可以加載並只顯示主教嗎?這將決定問題是否在尋找和裝載主教。下面的代碼將這樣做,這將有助於消除可能的原因:

public static void main(String[] args) { 
    JFrame frame = new JFrame(); 
    frame.getContentPane().add(new JLabel(new ImageIcon("bishop.png"))); 
    frame.pack(); 
    frame.setLocationRelativeTo(null); 
    frame.setVisible(true); 
} 
+0

+1(兩個)JLabel與圖標,ImageIcon – mKorbel 2012-03-08 14:50:05

1

板究竟是什麼?我假設它是可以擴展Swing組件(如JFrame)的東西?

所有與GUI相關的事件都應該發生在事件分派器線程(EDT)上。此線程負責更新GUI。在你需要從另一個類更新GUI的情況下,你需要使用SwingUtilities.invokeLater()

public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      @Override 
      public void run() 
      { 
       board b = new board(); 
       bishop bis1 = new bishop(); 
       bis1.setLocation(0, 0); 
       ImageIcon m = bis1.getImage(); 
       b.squares[0][1].add(new JLabel(m)); 
       ImageIcon h = new ImageIcon("rook.png"); 
       b.squares[0][0].add(new JLabel(h)); 
      } 
     }); 
    } 
0

最簡單的辦法: 上傳圖像到你的項目文件夾。 您可以使用JLabel來輸入圖像。 然後按照以下示例編寫代碼:

JLabel lblNewLabel = new JLabel("New Label"); 
lblNewLabel.setIcon(new ImageIcon("Name of your image")); 
panel.add(lblNewLabel);