2014-10-18 61 views
-1

我想设置的图标为一个JLabel图标,可以有人给我举例语法我如何设置的JLabel

我已经试过这样:

JLabel icon = new JLabel(); 
ImageIcon chromo = createImageIcon("res/icon.png"); 
panel.add(icon); 
icon.setIcon(chromo); 

我想这个标签没后根本不会出现在面板上。

+0

凡在项目这一形象在什么位置?你是否检查过图像是否为空? 'createImageIcon'方法是什么样的? – 2014-10-18 01:30:05

+0

它位于一个名为res的包中,其名称为icon.png so res/icon.png – 2014-10-18 01:34:43

+0

请显示'createImageIcon'方法\ – 2014-10-18 01:35:31

回答

-1
JLabel icon = new JLabel(); 
ImageIcon chromo = createImageIcon("res/icon.png"); 
icon.setIcon(chromo); 
panel.add(icon); 

不要忘记检查你的布局....如果你绝对需要添加界

icon.setBounds(startX,startY,width,height); 
+2

_if其绝对值你需要添加bounds_ Nah,如果它是绝对的,你需要切换到'LayoutManager' – Robin 2014-10-18 08:57:42