
1.首先,创建一个按钮控件,如下图。双击按钮进入其属性编辑器中,将其“string”属性中的“按钮”删掉;再将“Units”属性中默认的“characters”改成“Pixels”(像素)。
这个步骤不能缺少,因为待会要将背景图片的大小根据按钮的大小重新设定,而我们读取按钮大小的时候,给出的数值是以像素为单位的。
2.在OpeningFcn中,敲入如下的程序。
3.运行程序,就可以把背景图片显示在按钮控件上了。
给按钮添加背景图片可以给界面添加不少乐趣。比如可以做一个评价结果好坏的按钮组,给里面的按钮分别加上微笑、流汗、竖大拇指等表情包;也有人利用按钮来给界面加上自己学校的校徽。总之,看大家的创意了。
可以通过以下工具方法来实现图片的大小设置。public
void
setIcon(String
file,
JButton
iconButton)
{
ImageIcon
icon
=
new
ImageIcon(file)
Image
temp
=
icon.getImage().getScaledInstance(iconButton.getWidth(),
iconButton.getHeight(),
icon.getImage().SCALE_DEFAULT)
icon
=
new
ImageIcon(temp)
iconButton.setIcon(icon)
}
备注:file是图片的路径,iconButton是按钮的变量名。封装成一个函数就是可以实现了。
可以用Image1代替command1Image1.BorderStyle
=
1
'按下状态
Image1.BorderStyle
=
0
'松开状态
如果一定要用command
可以这样
Private
Sub
Command4_Click()
'用Picture2
,
Picture3加载图片
Picture2.AutoSize
=
True
Picture3.AutoSize
=
True
Picture1.AutoRedraw
=
True
Picture1.Move
Picture1.Left,
Picture1.Top,
Command4.Width,
Command4.Height
If
Command4.Tag
=
"2"
Then
Command4.Tag
=
"1"
'缩小图片
Picture1.PaintPicture
Form1.Picture2.Image,
0,
0,
Command4.Width,
Command4.Height,
0,
0,
Picture2.Width,
Picture2.Height
Command4.Picture
=
Picture1.Image
Else
Command4.Tag
=
"2"
'缩小图片
Picture1.PaintPicture
Form1.Picture3.Image,
0,
0,
Command4.Width,
Command4.Height,
0,
0,
Picture3.Width,
Picture3.Height
Command4.DownPicture
=
Picture1.Image
'这句是按下时显示的图片
End
If
End
Sub
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)