java JButton setIcon不起作用

时间:2014-11-15 21:04:21

标签: java jbutton

我有一个JButtons的2D数组。我还有一个与类文件位于同一目录中的图像。此图像称为“Ship.bmp”。下面的方法不会将此图像设置为按钮,为什么?

public void placeShip(int x, int y) {
    buttons[x][y].setEnabled(false);
    ImageIcon icon = null;
    java.net.URL imgURL = getClass().getResource("Ship.bmp");
    if (imgURL != null) {
        icon = new ImageIcon(imgURL);
    } else {
        System.err.println("Couldn't find file: Ship.bmp");
        icon = null;
    }
    System.out.println(imgURL);
    buttons[x+1][y+1].setIcon(icon);
}

P.S。我不是在使用eclipse。

提前致谢

1 个答案:

答案 0 :(得分:0)

根据此Java Icon tutorial,ImageIcon仅支持GIF,JPEG和PNG图像格式。您可能需要更改图像的格式,或者首先使用ImageIO类加载imgae。

相关问题