Java 为什么赢了';我的图标不会显示在我的按钮上吗?

Java 为什么赢了';我的图标不会显示在我的按钮上吗?,java,swing,icons,jbutton,embedded-resource,Java,Swing,Icons,Jbutton,Embedded Resource,因此,我试图通过JButton构造函数在JButton上放置一个图像 JButton button = new JButton(ImageIcon image) 我有几个图标被定义为 ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg"); ImageIcon SaveIcon = new ImageIcon("C:\\

因此,我试图通过
JButton
构造函数在
JButton
上放置一个图像

JButton button = new JButton(ImageIcon image)
我有几个图标被定义为

ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
图标文件位于名为

C:\Users\Wonseok\Documents\CompSciClass\Homebook Files\icons

这些图标所在的按钮定义为

ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);
我把它们放在
JPanel
中。 我的问题是图标在
JButtons
上没有正确显示,我不知道为什么。按钮显示为小而薄的空白矩形,上面没有图像。如果你想要一张照片,请告诉我如何从我的电脑上传一张。这是我的电脑让人恼火和故障吗?还是我的编码有问题

相关代码是

JPanel ButtonBar = new JPanel(new FlowLayout());
JPanel FileActions = new JPanel(new GridLayout(1, 2));
ImageIcon OpenIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg");
ImageIcon SaveIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\save.jpeg");
JButton OpenButton = new JButton(OpenIcon);
JButton SaveButton = new JButton(SaveIcon);
JPanel TextActions = new JPanel(new GridLayout(1, 3));
ImageIcon CutIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\cut.jpeg");
ImageIcon CopyIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\copy.jpeg");
ImageIcon PasteIcon = new ImageIcon("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\paste.jpeg");
JButton CutButton = new JButton(CutIcon);
JButton CopyButton = new JButton(CopyIcon);
JButton PasteButton = new JButton(PasteIcon);

public basic_text_editor()
{

    super("Text Editor");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    getContentPane().setBackground(Color.WHITE);
    setLayout(new FlowLayout(FlowLayout.LEFT));

    FileActions.add(OpenButton);
    FileActions.add(SaveButton);

    TextActions.add(CutButton);
    TextActions.add(CopyButton);
    TextActions.add(PasteButton);

    ButtonBar.add(FileActions);
    ButtonBar.add(TextActions);

    add(ButtonBar);

}

将映像放在项目本身中,否则在另一台机器上提供此代码时,它将无法工作。避免绝对路径

从资源文件夹中查看图像
2.png

ImageIcon folderIcon = new ImageIcon("resources/2.png");

如果映像在类所在的包中,请尝试此操作

ImageIcon icon = new ImageIcon(ImageIO.read(getClass().getResourceAsStream("2.png")));

我从不建议您使用绝对路径,但您可以尝试

ImageIcon icon = new ImageIcon(ImageIO.read("C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons\\open.jpeg"));


如果要重新调整图像大小,请使用此方法

public BufferedImage resizeImage(BufferedImage originalImage, int width, int height)
        throws IOException {
    BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, width, height, null);
    g.dispose();
    return resizedImage;
}
如何将图像(BuffereImage)转换为ImageIcon


这是项目结构


谢谢大家,但我已经找到了问题的答案。
文件扩展名不应该是.jpeg(文件类型的名称),而应该是.jpg。
无论如何,谢谢你的帮助。我将在项目文件夹中复制该文件夹,并使用相对路径:D

在调试此类问题时,通常最好简化问题并尝试将其隔离。考虑创建一个只在一个图像文件中读取的小程序,并在JopTePANE中显示图像图标,然后尝试调试它。如果您仍然被卡住,那么您可以发布一个更简单的程序供我们查看。话虽如此,问题往往是路径不正确。我自己,我通常使用资源流将图像作为缓冲区读取。例如,请查看我发布的小程序,只有您的代码无法从在线URL获取图像。发布图像至少需要10个代表。你可以提供一个指向图像的链接,其他人会为你嵌入它,或者等待更多的投票(我认为这个问题绝对值得)。如果你在不同的机器上尝试这个程序会发生什么。将这些图像放在项目本身中。您看到这些图像的唯一原因(基于您的代码)可能是因为无法加载图像(不是图像或找不到图像)。尝试使用类似于
System.out.println(新文件(“C:\\Users\\Wonseok\\Documents\\CompSciClass\\HomebookFiles\\icons\\open.jpeg”).exits()
查看图像是否存在(如果路径/名称正确,则应显示
true
),然后尝试使用
ImageIO.read(文件(“C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homebook Files\\icons\\open.jpeg”)
查看图像是否可以正确加载。是否必须为ImageIO导入某些内容?@ws04:如果有可以查找此类内容的文档。
 ImageIcon icon = new ImageIcon(image);