无法将图像提供给按钮 [英] not able to give image to button

查看:113
本文介绍了无法将图像提供给按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swing的新手。我需要给Jbutton一个图标。我找到了很多地方的示例代码并尝试过这样的

I am new to Swing. I need to give a Jbutton an icon. I found example 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);

但是按钮显示没有Icon.Image路径是正确的。

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

如果我错了,请帮我?

Thnx提前

推荐答案

添加图像的最佳方法是将图像链接到锚java类

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

创建Anchor类作为源边界并放置它进入你的项目包,其中包含一个image.png等...所以你可以直接从Jar文件中读取图像

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

使用JButton休息我猜你知道:)

Rest things with JButton I guess you know :)

祝你好运

这篇关于无法将图像提供给按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆