2012-10-24 46 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"); 
}