怎样在java小程序中加入背景图片。连连看小游戏的代码请问怎样在里面插入背景图啊?

怎样在java小程序中加入背景图片。连连看小游戏的代码请问怎样在里面插入背景图啊?,第1张

背景图片有两种方式,可用jlabel的构造函数具体看api,另外一个方法就是重写paintComponent()函数,给你个例子,

Toolkit tk = Toolkit.getDefaultToolkit()

private Image bground = tk.createImage("D://workpalce//MarketManagement//src//images//bgd2.gif")

protected void paintComponent(Graphics g) {

g.drawImage(bground, 0, 0, this.getWidth(), this.getHeight(), this)

}

1)文件要有后缀名

2)要用Image或BufferedImage对象

3)因为你重写了paint()方法,所以不能在Label里面显示图片。你重写了paint()方法后,整个容器都会变成画布,所以看不到Label组件,自然也就看不到图片。应该在paint方法里面用g.drawImage方法把图片在画布中画出来。参考Java API,Graphics的drawImage方法。

那你要在控件所在的面板中插入这张图片当背景:

public void paint(Graphics g) {

g.drawImage(IMAGE_BG, 0, 0, this)

super.paint(g)

}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/bake/11950066.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-19
下一篇2023-05-19

发表评论

登录后才能评论

评论列表(0条)

    保存