简体   繁体   English

在JButton中设置图像的大小/尺寸

[英]Set size/dimension of image in JButton

I am using several JButtons in my frame and I want to set image in JButton. 我在框架中使用了几个JButton,我想在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 在JButton中调整ImageIcon的大小

I am using WindowBuilder in my eclipse juno. 我在Eclipse Juno中使用WindowBuilder。 And I set the image using setting the properties called icon and in the source code it generates the following code. 我通过设置名为icon的属性来设置图像,并在源代码中生成以下代码。

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? 是否有直接方法或方法使用WindowBuider或将任何方法添加到我的当前代码中来将图像大小设置为JButton的大小?

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. 您在下面看到的实际上是5个标签和4个按钮,每个标签都包含图像的一部分。 The red border can be seen around one of the buttons. 可以在其中一个按钮周围看到红色边框。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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