【发布时间】:2015-12-12 14:24:58
【问题描述】:
如果图像太大,我想调整图像大小,但我希望它保持纵横比,我怎样才能定义它的高度并让它自动获得它的宽度?
ImageIcon image2 = new ImageIcon(image);
//Check if image size is more than 200
if(!checking){
Image img = image2.getImage() ;
Image newimg = img.getScaledInstance( "What to put here?", 200, java.awt.Image.SCALE_SMOOTH ) ;
image2 = new ImageIcon( newimg );
}
JButton newimage = new JButton(image2);
【问题讨论】:
-
您总是可以通过将旧高度除以新高度(或宽度)来获得比率。