Resize a picture to fit a JLabel

后端 未结 8 557
不思量自难忘°
不思量自难忘° 2020-11-29 01:56

I\'m trying to make a picture fit a JLabel. I wish to reduce the picture dimensions to something more appropriate for my Swing JPanel.

I tried with setPreferredSiz

8条回答
  •  旧巷少年郎
    2020-11-29 02:26

    i have done the following and it worked perfectly

    try {
            JFileChooser jfc = new JFileChooser();
            jfc.showOpenDialog(null);
            File f = jfc.getSelectedFile();
            Image bi = ImageIO.read(f);
            image1.setText("");
            image1.setIcon(new ImageIcon(bi.getScaledInstance(int width, int width, int width)));
    
        } catch (Exception e) {
        } 
    

提交回复
热议问题