1

对于我正在进行的一个项目,我制作了一张宾果卡。基本功能是随机生成卡片并在 STD 输出以及我手工制作的 Swing GUI 中显示。此应用程序中有多个 java 文件。

BingoFinal.java - 主文件。这是程序运行时运行的内容。Bingo_Card.java - 创建 Bingo 卡,将其打印到 STD,并检查 bingo BingoBG.java - 使用 2D 图形绘制 GUI 背景 DrawBingoCard.java - 调用 BingoBG 并创建 25 个带有棋盘值的标签。当 Bingo_Card 找到匹配的数字(由 STD IN 输入)时,它不再打印该数字,而是打印 Chip.png(与 java 文件在同一包文件夹中),一个宾果芯片图像,使其看起来被覆盖。

当我通过 NetBeans 运行它时,它可以完美运行,但是当我清理并构建它,然后在终端中运行 jar 时,除了显示宾果芯片图像之外,一切正常。有人知道为什么会这样吗?

编辑:这是我加载图像的方式

ImageIcon chip; //declare ImageIcon
chip = new ImageIcon("chip75.png"); //define it as chip75.png. It is stored in package folder
JLabel B1Chip; //declare empty Label
B1Chip = new JLabel(chip); //define the Label with just the ImageIcon
B1Chip.setBounds(22, 112, chip.getIconWidth(), chip.getIconHeight()); //place at (22,112)
frame.add(B1Chip, null); //Add to frame
4

2 回答 2

3

You should be accessing the images, by using :

ImageIcon chip = new ImageIcon(ClassName.class.getResource("/chip75.png"))

More information can be found on the info page of tag: embedded-resource A detailed answer regarding adding images to the NetBeans Project is mentioned on the info-page link.

More importantly, it would be wise, that you use ImageIO.read, since atleast it will let you know, if something goes wrong, by throwing IOException

Throws:

IllegalArgumentException - if input is null.
IOException - if an error occurs during reading.

ImageIcon on the other hand will hide the exception if any :-)

于 2013-08-29T09:23:53.987 回答
0

将图像文件复制到 jar 中或给出图像文件的绝对路径

于 2013-08-29T08:52:11.543 回答