-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文件中。
我怎樣才能讓圖像顯示在窗口中?
你測試,以查看是否的ImageIcon,cardIcon,爲空?如果是這樣,你可能會在錯誤的地方尋找圖像。我自己,我使用'ImageIO.read(...)'獲取我的圖像作爲BufferedImages,並且不傳入文件,而是從類方法getResource(...)'獲得的URL。 –
測試和工作,也許圖像的路徑是錯誤的。 – Christian
原始海報,您應該顯示您的cardImages文件夾與您的課程文件相關的位置。 –