简体   繁体   中英

Set size/dimension of image in JButton

I am using several JButtons in my frame and I want to set image in JButton. I am able to set the size of image using the following code.

Image img = icon.getImage() ;  
Image newimg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT,  java.awt.Image.SCALE_SMOOTH ) ;  
icon = new ImageIcon( newimg );

And I also viewed the following link.

resizing a ImageIcon in a JButton

I am using WindowBuilder in my eclipse juno. And I set the image using setting the properties called icon and in the source code it generates the following code.

btnSave.setIcon(new ImageIcon(Patient_Detail.class.getResource("/Icons/download_icon.png")));

So, as my image size is comparatively bigger than my button size.

Is there any direct approach or method to set image size as the size of JButton using WindowBuider or adding any method to my current code?

See this answer for an example of how to do it (presuming 'it' is 'have a button the same size as the button icon').

What you see below is actually 5 labels & 4 buttons each holding parts of an image. The red border can be seen around one of the buttons.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM