Java 在 JButton 中调整 ImageIcon 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2856480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
resizing a ImageIcon in a JButton
提问by Anto
I am creating a JButton which includes a specific ImageIcon. The main issue is that the original icon size is much bigger than the button size. As result when the button is displayed, only part of the icon can be seen. What is the method that "resize" an ImageIcon i n order to make it fit inside a JButton?
我正在创建一个包含特定 ImageIcon 的 JButton。主要问题是原始图标大小比按钮大小大得多。结果当显示按钮时,只能看到图标的一部分。什么是“调整”ImageIcon 以使其适合 JButton 的方法?
采纳答案by tim_yates
Image img = icon.getImage() ;
Image newimg = img.getScaledInstance( NEW_WIDTH, NEW_HEIGHT, java.awt.Image.SCALE_SMOOTH ) ;
icon = new ImageIcon( newimg );
from http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon
来自http://www.coderanch.com/t/331731/GUI/java/Resize-ImageIcon
回答by aioobe
I would try to override the getIcon()
method of JButton
, and resize the super.getIcon()
. (Or, redefining the lnf for that button.)
我会尝试覆盖 的getIcon()
方法JButton
,并调整super.getIcon()
. (或者,重新定义该按钮的 lnf。)
回答by Scott
I used this class
我用过这个类
I included the code from "get the code" (at the bottom) as another class in my project. Be sure to change the package name if you want it to work.
我将“获取代码”(底部)中的代码作为另一个类包含在我的项目中。如果您希望它工作,请务必更改包名称。