Resize a picture to fit a JLabel

后端 未结 8 562
不思量自难忘°
不思量自难忘° 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:27

    The best and easy way for image resize using Java Swing is:

    jLabel.setIcon(new ImageIcon(new javax.swing.ImageIcon(getClass().getResource("/res/image.png")).getImage().getScaledInstance(200, 50, Image.SCALE_SMOOTH)));
    

    For better display, identify the actual height & width of image and resize based on width/height percentage

提交回复
热议问题