开发者

not able to give image to button

开发者 https://www.devze.com 2023-03-15 23:20 出处:网络
I am new to Swing. I need to give a Jbutton an icon. I found ex开发者_StackOverflowample code so many places and tried like this

I am new to Swing. I need to give a Jbutton an icon. I found ex开发者_StackOverflowample code so many places and tried like this

ImageIcon cup = new ImageIcon("/home/Whether/Picture/start.png");
JButton startBut=new JButton(cup);
panel.add(startBut);

But button is showing without Icon.Image path is correct.

Pls coorect me if Im wrong?

Thnx in advance


The best way of adding images is to link image to an "anchor" java class

Create "Anchor" class as a source bound and place it into your project package which contains an "image.png" etc... So you can read image right from the Jar file as

Image image=ImageIO.read(Anchor.class.getClassLoader().getResourseAsStream("image.png"));
ImageIcon icon=new ImageIcon(image);

Rest things with JButton I guess you know :)

Good luck

0

精彩评论

暂无评论...
验证码 换一张
取 消