2014-01-19 162 views
-1
import javax.swing.*; 
import java.awt.*; 

public class Main 
{ 
    public static void main(String[] args) 
    { 
     //load the card image from the gif file. 
     final ImageIcon cardIcon = new ImageIcon("cardimages/tenClubs.gif"); 

     //create a panel displaying the card image 
     JPanel panel = new JPanel() 
     { 
      //paintComponent is called automatically by the JRE whenever 
      //the panel needs to be drawn or redrawn 
      public void paintComponent(Graphics g) { 
       super.paintComponent(g); 
       cardIcon.paintIcon(this, g, 20, 20); 
      } 
     }; 

     //create & make visible a JFrame to contain the panel 
     JFrame window = new JFrame("Title goes here"); 
     window.add(panel); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setBackground(new Color(100, 200, 102)); 
     window.setPreferredSize(new Dimension(200,200)); 
     window.pack(); 
     window.setVisible(true); 
    } 
} 

我想做一個java項目,將顯示窗口上的所有52卡。我有窗戶工作,但我不能讓一張卡片出現在窗戶上。爪哇Swing窗口

我使用OSX的eclipse項目src文件內我有一個(默認包)容器與我的Main.java文件。然後我將我的cardimages文件夾放在同一個src文件中。

我怎樣才能讓圖像顯示在窗口中?

My file system for project

+0

你測試,以查看是否的ImageIcon,cardIcon,爲空?如果是這樣,你可能會在錯誤的地方尋找圖像。我自己,我使用'ImageIO.read(...)'獲取我的圖像作爲BufferedImages,並且不傳入文件,而是從類方法getResource(...)'獲得的URL。 –

+0

測試和工作,也許圖像的路徑是錯誤的。 – Christian

+0

原始海報,您應該顯示您的cardImages文件夾與您的課程文件相關的位置。 –

回答

1

你應該嘗試的圖像作爲URL,作爲資源再次使用類方法getResource(...)。例如,測試:

import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.Icon; 
import javax.swing.ImageIcon; 
import javax.swing.JOptionPane; 


public class DefaultFoo { 
    public static void main(String[] args) throws IOException { 
     String resource = "/cardimages/tenClubs.gif"; 
     URL url = Class.class.getResource(resource); 
     BufferedImage img = ImageIO.read(url); 
     Icon icon = new ImageIcon(img); 
     JOptionPane.showMessageDialog(null, icon); 
    } 
} 

另外,請不要像使用默認包那樣使用默認包。把你的課程放入一個有效的包中。

然後嘗試這樣的事:

import java.awt.Dimension; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.net.URL; 

import javax.imageio.ImageIO; 
import javax.swing.*; 

@SuppressWarnings("serial") 
public class PlayWithImages extends JLayeredPane { 
    private static final String RESOURCE = "/cardimages/tenClubs.gif"; 
    private static final int PREF_W = 500; 
    private static final int PREF_H = PREF_W; 
    private static final int CARD_COUNT = 8; 

    public PlayWithImages() throws IOException { 
     URL url = getClass().getResource(RESOURCE); 
     BufferedImage img = ImageIO.read(url); 
     Icon icon = new ImageIcon(img); 

     for (int i = 0; i < CARD_COUNT; i++) { 
     JLabel label = new JLabel(icon); 
     label.setSize(label.getPreferredSize()); 
     int x = PREF_W - 20 - i * 40 - label.getWidth(); 
     int y = 20; 
     label.setLocation(x, y); 
     add(label); 
     } 
    } 

    @Override 
    public Dimension getPreferredSize() { 
     return new Dimension(PREF_W, PREF_H); 
    } 

    private static void createAndShowGui() { 
     PlayWithImages mainPanel = null; 
     try { 
     mainPanel = new PlayWithImages(); 
     } catch (IOException e) { 
     e.printStackTrace(); 
     System.exit(-1); 
     } 

     JFrame frame = new JFrame("PlayWithImages"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.getContentPane().add(mainPanel); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

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