简体   繁体   English

JButton中的图标

[英]Icon in JButton

I am trying to add in an icon to my JButton and so far nothing has worked. 我试图在我的JButton中添加一个图标,到目前为止没有任何效果。 I have pasted below my code for the button and also the location of where the image is stored. 我在代码下方粘贴了该按钮以及图像的存储位置。

  public static DRTextCode BUTTON_COLLAPSE_NESTED() {
    DRTextCode tc = new DRTextCode ("BUTTON_COLLAPSE_NESTED",         "Collapse");
    return tc;
}


    btn = new JButton(BUTTON_COLLAPSE_NESTED().toString());
    btn.setActionCommand(BUTTON_COLLAPSE_NESTED().toString());
    btn.addActionListener(btnListener);
    btnPnl.add(btn);
    _collapseBtn = btn;

This below is the images location 这是图像位置

"mz/images/svg/folder_collapse.svg" “ mz / images / svg / folder_collapse.svg”

I am not sure how to add this in 我不确定如何在其中添加

try to use .png icons because they are more common and easy to find them free, 尝试使用.png图标,因为它们更常见并且更容易免费找到,
you can find free flat icons here . 您可以在此处找到免费的平面图标。

  • first create a folder in this path src/resources 首先在此路径src/resources创建一个文件夹

在此处输入图片说明

  • put your icons that path and use them in this way 将图标放在该路径上并以这种方式使用它们

     JButton button = new JButton(); try { Image img = ImageIO.read(new File("src//resources//icon.png")); button.setIcon(new ImageIcon(img)); } catch (IOException ex) { } 

also watch these useful tutorials 也观看这些有用的教程

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

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