如何调整 Swing JButon 图标的大小
How to resize Swing JButon's icon
我有一个图标,我想在 JButton 和其他地方使用其他尺寸。那么有什么方法可以调整大小(比如 button.setIconSize(124, 124);
相同的图标,但没有其中的 2 个?
图标是按实际大小绘制的,因此如果您想要不同大小的图标,则需要创建多个图标。添加图标的组件的首选大小将始终基于图标的大小。
如果您希望 Icon
能够随着其父组件大小的调整而动态缩放,那么您可以使用 Stretch Icon。
ImageIcon imageIcon = new ImageIcon("picture_Path"); //unscaled image
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH); // resize it here
imageIcon = new ImageIcon(newimg); // transform it back
所以它会像
JButton.setIcon(imageIcon(...));
我有一个图标,我想在 JButton 和其他地方使用其他尺寸。那么有什么方法可以调整大小(比如 button.setIconSize(124, 124);
相同的图标,但没有其中的 2 个?
图标是按实际大小绘制的,因此如果您想要不同大小的图标,则需要创建多个图标。添加图标的组件的首选大小将始终基于图标的大小。
如果您希望 Icon
能够随着其父组件大小的调整而动态缩放,那么您可以使用 Stretch Icon。
ImageIcon imageIcon = new ImageIcon("picture_Path"); //unscaled image
Image image = imageIcon.getImage();
Image newimg = image.getScaledInstance(newWidth, newHeight, java.awt.Image.SCALE_SMOOTH); // resize it here
imageIcon = new ImageIcon(newimg); // transform it back
所以它会像
JButton.setIcon(imageIcon(...));