我想在Frame上面添加图片,请问一下怎么做??我是想让个图片做背景用的?
答案:2 悬赏:20 手机版
解决时间 2021-02-14 23:14
- 提问者网友:做自己de王妃
- 2021-02-14 03:44
我想在Frame上面添加图片,请问一下怎么做??我是想让个图片做背景用的?
最佳答案
- 五星知识达人网友:上分大魔王
- 2021-02-14 04:04
File file = new File("F:/tupian/Blue hills.jpg");
try {
Image image = ImageIO.read(file);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
JFrame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
((JPanel)JFrame.getContentPane()).setOpaque(false);
} catch (IOException ex) {
ex.printStackTrace();
}
try {
Image image = ImageIO.read(file);
ImageIcon icon = new ImageIcon(image);
JLabel label = new JLabel(icon);
label.setBounds(0,0,icon.getIconWidth(),icon.getIconHeight());
JFrame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));
((JPanel)JFrame.getContentPane()).setOpaque(false);
} catch (IOException ex) {
ex.printStackTrace();
}
全部回答
- 1楼网友:春色三分
- 2021-02-14 04:37
'frame本身不具备背景图片功能
'以下是我用api做的一个例子
'本来打算用loadimage做,不过loadpicture支持的图片格式多一点。
'frame控件没有autoredraw属性,所以我在form_paint里加了一些代码,解决了最小化窗体后图片消失的问题。(还有个问题:改变窗体大小的时候如果窗体覆盖图片区域会使图片消失,解决办法明天附上,太晚了,睡觉。)
private declare function selectobject lib "gdi32" (byval hdc as long, byval hobject as long) as long
private declare function createcompatibledc lib "gdi32" (byval hdc as long) as long
private declare function deletedc lib "gdi32" (byval hdc as long) as long
private declare function bitblt lib "gdi32" (byval hdestdc as long, byval x as long, byval y as long, byval nwidth as long, byval nheight as long, byval hsrcdc as long, byval xsrc as long, byval ysrc as long, byval dwrop as long) as long
private declare function getdc lib "user32" (byval hwnd as long) as long
private declare function releasedc lib "user32" (byval hwnd as long, byval hdc as long) as long
dim fwidth as integer, fheight as integer
private sub form_load()
me.show
doevents
call framedimg(frame1, "d:\壁纸\080505081302_5.jpg")
end sub
private sub form_paint()
if me.width = fwidth and me.height = fheight then
doevents
call framedimg(frame1, "d:\壁纸\080505081302_5.jpg")
else
fwidth = me.width
fheight = me.height
end if
end sub
private sub framedimg(oframe as object, imgfilepath as string) 'framedimg(控件名称,图片文件的路径)
dim frmdc as long, imgdc as long
dim pbmp as ipicturedisp
set pbmp = loadpicture(imgfilepath)
imgdc = createcompatibledc(0)
selectobject imgdc, pbmp.handle
frmdc = getdc(oframe.hwnd)
bitblt frmdc, 0, 0, oframe.width, oframe.height, imgdc, 0, 0, vbsrccopy
set pbmp = nothing
deletedc imgdc
releasedc oframe.hwnd, frmdc
end sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯