[英]How to set an ImageIcon to a JButton and resize the picture according to the button's size?
我一直想知道如何使用Image
路径将ImageIcon
设置为按钮并将其设置为JButton
?
我可以根据按钮的大小来调整图像的大小:
frontViewImageFile = fc.getSelectedFile();
MainFrame.btnFrontView.setIcon(new ImageIcon(ImageIO.read(
frontViewImageFile).getScaledInstance(150, 150, Image.SCALE_SMOOTH)));
但是图像来自文件选择器,我可以使用getScaledInstance
方法调整图片的大小。
由于类型String
getScaledInstance
方法未定义,我该如何使用图像路径执行此操作?
ImageIcon icon = ...;
JButton b = ...;
Image im = icon.getImage();
Image im2 = im.getScaledInstance(b.getWidth(), b.getHeight(), ...);
b.setIcon(new ImageIcon(im2));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.