我已经使用Netbeans的GUI生成器为我的应用程序创建了GUI。我正在尝试显示一个JFrame,其中包含一个带有图像的JLabel,但无法使Image显示。
我生成的代码:
private void initComponents() {
//...
jLabel1 = new JLabel(new ImageIcon(myPicture));
}我的班级代码是:
public class GUIWindow extends javax.swing.JFrame {
BufferedImage myPicture;
/** Creates new form GUIWindow */
public GUIWindow() throws IOException {
myPicture = ImageIO.read(new File("images/logo.png"));
initComponents();
this.add(jLabel1);
}
}但是我仍然没有看到一个图像.(图像文件的路径很好)是这样的:
my-project :
/build
/dist
/images/logo.png
/nbproject
/src (here I have all my source files)
/build.xml
/manifest.mf发布于 2012-07-30 15:33:52
你可以这样用
URL imgSmartURL = this.getClass().getResource("your image path");
jLabel1 = new JLabel(new ImageIcon(imgSmartURL), JLabel.CENTER);发布于 2012-07-30 16:03:27
我会做这样的事。
JLabel dice1 = new JLabel();
ImageIcon one = new ImageIcon("dice/1.png");
//set dice1 position
dice1.setLocation(20, 100);
dice1.setSize(115, 115);
dice1.setIcon(one);
gamepanel.add(dice1);发布于 2012-07-31 07:32:08
如果使用netbeans,可以通过设置属性直接向jLabel添加图像。Right click on the jLabel -> properties -> icon -> (if it's external image) import to project(upload your image) -> ok。我被添加到你的jLabel中。
https://stackoverflow.com/questions/11724564
复制相似问题