java 怎样给button加图片?

来源:百度知道 编辑:UC知道 时间:2024/06/03 23:02:02
我加过ico的jpg的png的都不行我用的是myeclipes编程
求一个运行通过了的给jbutton加图片的代码,谢谢!
图片是加上了可是为什么大小跟按钮不配套,而且一单击图片就没了松开后就又回来了,这是怎么回事啊?

import javax.swing.JButton;
import javax.swing.JFrame;
import com.swtdesigner.SwingResourceManager;

public class ButtonTest {

private JFrame frame;

public static void main(String args[]) {
ButtonTest window = new ButtonTest();
window.frame.setVisible(true);
}

public ButtonTest() {
createContents();
}

private void createContents() {
frame = new JFrame();
frame.getContentPane().setLayout(null);
frame.setBounds(100, 100, 500, 375);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

final JButton button = new JButton();
button.setIcon(SwingResourceManager.getIcon(ButtonTest.class, "2254198.jpg"));
button.setBounds(45, 47, 312, 184);
frame.getContentPane().add(button);
}

}

这样写试试

JButton temp = new JButton(getImageIcon(文件名);

public ImageIcon getImageIcon(String imagefi