当 继承JLabel

umbrellall1 2010-02-04 05:40:05
当 继承JLabel怎么把ImageIcon设置进JLabel里面

public class Aa extends JFrame{
Container con;
public Aa(){
con=this.getContentPane();
this.setSize(650,550);
this.setVisible(true);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
this.setResizable(false);
}
public static void main(String[] args) {
new Aa();
}
}
class Bb extends JLabel{
public Bb(String x,String y,String mapX,String mapY,String path,Container con){

ImageIcon img = new ImageIcon(path);

this.setBounds(x,y, img.getIconWidth(), img.getIconHeight());
}

}
...全文
165 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuyouyong 2010-02-05
  • 打赏
  • 举报
回复
好像是直接就能加上,看api吧JLabel.add();
tiamay 2010-02-05
  • 打赏
  • 举报
回复
swing的 忘了哦!
focusforce 2010-02-04
  • 打赏
  • 举报
回复
你的本意不是想在JLabel上用ImageIcon,而是想把ImageIcon作为JLabel的背景?
protected void paintComponent(Graphics g)
{
int cw = getWidth();
int ch = getHeight();

ImageIcon scaledSymbol = new ImageIcon(img .getImage()
.getScaledInstance(cw, ch, Image.SCALE_SMOOTH));
g.drawImage(scaledSymbol.getImage(), 0, 0, this);

// int iw = img.getWidth(this);
// int ih = img.getHeight(this);
// int x = 0;
// int y = 0;
// while (y <= ch)
// {
// g.drawImage(img, x, y, this);
// x += iw;
// if (x >= cw)
// {
// x = 0;
// y += ih;
// }
// }
}
stamp80 2010-02-04
  • 打赏
  • 举报
回复
继不继承没什么影响,多看看JLabel的api就能解决。我原来做过的,能实现。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧