如何用JAVA写一个简单的JButton,然后再JButton里面插入图片。并且设置图片的大小??

如题!!java.SWING编程。如何用JAVA写一个简单的JButton,然后再JButton里面插入图片。并且设置图片的大小??P.S我用的软件是eclipse
如果可以的话,最好举个简单的例子,谢谢哈~等待大家的消息

Swing 的JFrame 是可以直接拽下按钮来的,在左下角设置属性就可以啊,JButton的图片,直接做成图片按钮多好,实在不行就把按钮的style里设上img咯
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-06-04
import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {
public static void main(String[] args) throws Exception {
JFrame f=new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel p=new JPanel();
f.add(p);

JButton tb=new JButton();

ImageIcon icon=new ImageIcon(new URL("http://ico.ooopic.com/iconset02/2/gif/62717.gif"));
icon=new ImageIcon(icon.getImage().getScaledInstance(100, 32, Image.SCALE_SMOOTH));

tb.setIcon(icon);
p.add(tb);

f.setSize(400,300);
f.setVisible(true);
}
}

上代码

第2个回答  2013-06-04
jButton1.setIcon(new javax.swing.ImageIcon("地址"));本回答被提问者采纳
第3个回答  2018-05-15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

public class ButtonImage
{
public ButtonImage()
{
JFrame frame = new JFrame();
JButton button = new JButton("confirm");
ImageIcon icon = new ImageIcon("image.jpg");
button.setIcon(icon);
frame.add(button);
frame.setLayout(new FlowLayout());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
frame.setVisible(true);
}
public static void main(String[] args)
{
new ImageButton();
}
}

  可以把iamge.jpg裁剪为合适的大小,效果可能就会很好。
相似回答