1
我想从目录加载图像并将其作为图标放入JLabel
。但是,当图像尺寸很大时,它不能完全符合标签。我尝试调整图像的大小以适应标签,但不起作用。我可否知道我要去哪里?调整图像大小以适应JLabel
JFileChooser choose=new JFileChooser();
choose.showOpenDialog(null);
File f=choose.getSelectedFile();
String filename=f.getAbsolutePath();
path.setText(filename);
ImageIcon icon=new ImageIcon(filename);
Image img=icon.getImage();
BufferedImage bi = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_ARGB);
Graphics g = bi.createGraphics();
g.drawImage(img,500, 500, null);
ImageIcon newIcon = new ImageIcon(bi);
image_label.setIcon(newIcon);
啊,没有。认真。请尝试定义“最大”允许大小,并缩小任何太大的图像。对于[示例](http://stackoverflow.com/questions/14548808/scale-the-imageicon-automatically-to-label-size/14553003#14553003)和[示例](http://stackoverflow.com/questions/ 11959758/java-maintain-aspect-ratio-of-jpanel-background-image/11959928#11959928) – MadProgrammer
*“哪里出错了?”* - 首先创建一个BufferedImage,其大小与你刚刚加载的图像,然后在画布内绘制图像500x500 ...除非图像大于500x500,否则图像将呈现出视野... – MadProgrammer
@MadProgrammer我试着通过你给出的2个例子,并且我想承认我是处理图像的新手,因此我无法理解发生了什么。我的jLabel的大小是500x500,因此我将图像缩放到500x500,并且我加载的图像肯定比这个大, –