简体   繁体   中英

Icon in JButton

I am trying to add in an icon to my JButton and so far nothing has worked. 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"

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,
you can find free flat icons here .

  • first create a folder in this path 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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