2012-10-24 83 views
1

我是一名大學生,這是我第一次創建Java中的gui。現在我看着這個答案GUI in Java using Swing並按照指示,仍然沒有任何反應。這是代碼。我剪掉了所有不相關的垃圾。無法在擺動中顯示圖像

import javax.imageio.ImageIO; 
import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 

public class Lab4Shell 
{ 
    // this holds the current game board 
     private char[][] gameBoard = new char[7][8]; 
     private JButton[][] gameButtons = new JButton[7][8]; 

     private ImageIcon red = new ImageIcon("Red.jpg"); 
     private ImageIcon black = new ImageIcon("Black.jpg"); 
     private ImageIcon empty = new ImageIcon("Empty.jpg"); 

     private JPanel panel = new JPanel(); 

     private int currentPlayer = 1; 
     private int numMoves = 0; 
     //Why do you want everything in constructor? 
     public Lab4Shell() 
     { 
      CreateWindow(); 
      ResetGame(); 



      // set layout 
      // loop through buttons array creating buttons, registering event handlers and adding buttons to panel 
      // add panel to frame 
      // do other initialization of applet 
     } 

     public static void CreateWindow() 
     { 
      //Sets window title and create window object. 
      JFrame aWindow = new JFrame("Connect Four"); 
      //Set window position and size 
      aWindow.setBounds(500,100,400,400); 
      //What close button does. 
      aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      //Make window visible. 
      aWindow.setVisible(true); 


     } 

     public static void main(String args[]) 
     { 
      SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       { 

        Lab4Shell game = new Lab4Shell(); 

       } 
      }); 


     } 
void ResetGame() 
     { 



      JLabel label = new JLabel(); 
      label.setIcon(empty); 
      for(int r=0;r<gameBoard.length;r++) 
      { 
       java.util.Arrays.fill(gameBoard[r],0,gameBoard[r].length,'0'); 



       //loop through board columns 
       for(int c=0;c<gameBoard[r].length;c++) 
       { 

       } 

      } 
      // loop through array setting char array back to ' ' and buttons array back to empty pic 
      // reset currentPlayer and numMoves variables 
     } 
+0

你的圖像存儲在哪裏? – MadProgrammer

+0

使用Eclipse的Im,圖像在程序的bin文件夾中。 – DrinkJavaCodeJava

+0

對於實驗,您可以使用UI圖標,如[此處]所示(http://stackoverflow.com/a/12228640/230513)。 – trashgod

回答

1

你從來沒有添加任何你的框架 - 這是造成你的問題。因此,在createWindow你的方法,你需要調用:

aWindow.setContentPane(panel); 

再後來就(像你resetGame方法),您需要添加的內容(如JLabel)到面板:

panel.add(empty); 

當它添加到您的面板由面板的LayoutManager確定(其中還有不少的 - 默認值是BorderLayout

其他有用的東西:

  • 通常,當它有意義時,在構造函數中創建/初始化對象,並在運行時添加/刪除/更新它們。
  • 對於故障排除,請使用.setOpaque(true).setBackground(Color.blue)方法來查看您想要查看的內容。如果你沒有看到它,或者有東西在掩蓋它,或者它從未被添加過

祝你好運。

+0

非常感謝!你是英雄。 – DrinkJavaCodeJava

2

你可以試試這個

BufferedImage myPicture = ImageIO.read(new File("path-to-file")); 
JLabel picLabel = new JLabel(new ImageIcon(myPicture)); 
add(picLabel); 
+0

修訂版+1;另請參閱此[示例](http://stackoverflow.com/a/3078354/230513)。 – trashgod

+0

添加哪些類來自?我的編譯器不喜歡它。 – DrinkJavaCodeJava

2

你必須創建ImageIcons添加到面板馬諾斯說,並且是在Eclipse項目的src文件夾中的圖片,這樣做:

java.net.URL url = getClass().getResource("red.JPEG"); 
ImageIcon red = new ImageIcon(url); 
+0

那麼那個ImageSource實例變量呢?我的教授要我用這些。我應該把它放在構造函數中嗎?但我會試一試你的想法 – DrinkJavaCodeJava

+0

是的男人,試一試,你會發現它會起作用。你可以像這樣放入構造函數中:ImageIcon image = new ImageIcon(getClass()。getResource(「/ red.JPEG」)); – Drugo

2

如果資源與應用程序一起嵌入(在jar中),則需要使用Class#getResource來加載它們。

加載圖像的首選機制是通過ImageIO API。它支持多種圖像格式(以及提供可插入式架構),並保證它的形象是隨時可以顯示一旦read方法返回

BufferedImage redImage; 

// ... 

URL url = getClass().getResource("red.JPEG"); 
if (url != null) { 
    redImage = ImageIO.read(url); 
} else { 
    throw new NullPointerException("Unable to locate red image resource"); 
}