简体   繁体   English

为什么我的图标没有显示在我的JButton上?

[英]Why won't my icon show up on my JButton?

So I am trying to put an image on a JButton by the means of the JButton constructor 所以我试图把在图像上JButton由手段JButton构造

JButton button = new JButton(ImageIcon image)

I have a few icons are defined as 我有几个图标定义为

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");

The icon files are in a folder called 图标文件位于一个名为

C:\\Users\\Wonseok\\Documents\\CompSciClass\\Homework Files\\icons. C:\\ Users \\ Wonseok \\ Documents \\ CompSciClass \\ Homework Files \\ icons。

The buttons that these icons are on are defined as 这些图标所在的按钮定义为

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);

I have these in a JPanel . 我在JPanel有这些。 My problem is that the icons aren't showing properly on the JButtons , and I have no idea why. 我的问题是图标没有在JButtons上正确显示,我也不知道为什么。 The buttons show up as small, thin, blank rectangles without an image on it. 这些按钮显示为小而细的空白矩形,上面没有图像。 If you want a picture, please tell me how to post one from my computer. 如果您想要一张照片,请告诉我如何从我的计算机发布照片。 Is this my computer being annoying and glitchy? 这是我的电脑烦人和小故障吗? Or is there something wrong with my coding? 还是我的编码有问题?

The pertinent code is 相关代码是

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);

}

Put the images in the project itself otherwise it will not work when this code is shipped on another machine. 将图像放在项目本身中,否则当此代码在另一台计算机上交付时将不起作用。 Avoid absolute path. 避免绝对路径。

Looking an image 2.png from resources folder 从资源文件夹中查找图像2.png

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

OR 要么

ImageIcon icon = new ImageIcon(ImageIO.read(new File("resources/2.png")));

OR 要么

Try this one if the image is in the package where the class is present 如果图像在存在该类的包中,请尝试此操作

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

OR 要么

I never suggest you to use absolute path but you can try it. 我从不建议您使用绝对路径,但是您可以尝试使用。

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

OR 要么

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

If you want to re-size the image then use this method. 如果要调整图像大小,请使用此方法。

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;
}

how to convert an Image(BufferedImage) into ImageIcon? 如何将Image(BufferedImage)转换为ImageIcon?

 ImageIcon icon = new ImageIcon(image);

Here is the project structure 这是项目结构

在此处输入图片说明

Thanks all, but I've found the answer to my problem. 谢谢,但是我找到了解决我问题的方法。
The file extension shouldn't have been .jpeg, which is the name for the type of file, but should've been .jpg. 文件扩展名不应为.jpeg,这是文件类型的名称,但应为.jpg。 Thanks for your help, anyways. 无论如何,感谢您的帮助。 I'll make a copy of the folder in my project folder and use relative paths. 我将在项目文件夹中复制该文件夹,并使用相对路径。 :D :d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM