java button 里的图片怎么适应按钮大小?
答案:1 悬赏:40 手机版
解决时间 2021-11-10 09:28
- 提问者网友:寂寞撕碎了回忆
- 2021-11-09 11:11
java button 里的图片怎么适应按钮大小?
最佳答案
- 五星知识达人网友:时间的尘埃
- 2021-11-09 12:47
可以通过以下工具方法来实现图片的大小设置。
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是按钮的变量名。封装成一个函数就是可以实现了。
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是按钮的变量名。封装成一个函数就是可以实现了。
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯