java button 里的图片怎么适应按钮大小?

Python017

java button 里的图片怎么适应按钮大小?,第1张

可以通过以下工具方法来实现图片的大小设置。\x0d\x0apublic void setIcon(String file, JButton iconButton) { \x0d\x0aImageIcon icon = new ImageIcon(file) \x0d\x0aImage temp = icon.getImage().getScaledInstance(iconButton.getWidth(), \x0d\x0aiconButton.getHeight(), icon.getImage().SCALE_DEFAULT) \x0d\x0aicon = new ImageIcon(temp) \x0d\x0aiconButton.setIcon(icon) \x0d\x0a} \x0d\x0a备注:file是图片的路径,iconButton是按钮的变量名。封装成一个函数就是可以实现了。

如果是做网页,你可以把图片设置成背景,就OK了撒,

如:

<form

action="some.do"

method="post">

用户:<input

name="username"

type="text">

密码:<input

name="password"

type="password">

<input

type="submit"

value="提交"

style="background-image:url(bg.gif)">

</form>

bg.gif就是你的图片,如果图片上有登录两个字,value属性就不需要了,设置一下button的高宽就可以实现图片按钮的效果了。

下面一种也可以

<form

action="example.cgi"

method="post">

<input

type="text"

name="login">

<br>

<input

type="image"

src="submit.gif">

<input

type="image"

src="reset.gif">

</form>

Icon icon=new ImageIcon("image/inter.jpg")

Button1.setIcon(icon)

("image/inter.jpg")是文件地址,一般是在你的工程的文件下面新建个文件夹专门放图片,我的文件夹是image