在頁面想要顯示圖片,可以使用ImageIcon標籤組件獲取圖片,然後將圖片添加到JLable標籤組件顯示出,以及想要顯示不同大小的圖片,自適應容器大小顯示圖片。
1.正常顯示圖片
ImageIcon imageic = new ImageIcon("*.jpg");
JLabel lable = new JLabel(imageic);
2.自定義顯示圖片大小
ImageIcon imageic = new ImageIcon("*.jpg");
imageic.setImage(imageic.getImage().getScaledInstance(200, 300,Image.SCALE_DEFAULT));
JLabel lable = new JLabel(imageic);
3.自適應容器大小
Image image = new Image("**.jpg");
JLabel lable = new JLabel();
lable.setIcon(SwingUtil.createAutoIcon(image, true));