[英]How to change icon size of UIManager.getIcon("OptionPane.errorIcon)?
I'm working on swing project. 我正在做秋千项目。
I've jErrorMsgLabel
to show error message. 我有jErrorMsgLabel
显示错误消息。
I want to use L&F icons for jErrorMsgLabel
same as below snap showing information message "Please specify a value for Database:" 我想为jErrorMsgLabel
使用L&F图标,如下图所示,显示信息消息“请为数据库指定一个值:”
So I did 所以我做了
jErrorMsgLabel.setIcon(UIManager.getIcon("OptionPane.errorIcon"));`
But icon size is same as JOptionPane
但是图标大小与JOptionPane
相同
How can I change icon size? 如何更改图标大小?
Or is there any other way around to show error messages? 还是有其他方法可以显示错误消息?
You need to scale the image yourself: 您需要自己缩放图像:
ImageIcon icon = (ImageIcon)UIManager.getIcon("OptionPane.errorIcon");
Image image = icon.getImage();
Image scaledImage = image.getScaledInstance(80, 80, Image.SCALE_DEFAULT);
Icon scaledIcon = new ImageIcon( scaledImage );
yourLabel.setIcon( scaledIcon );
Of course whenever you scale an image larger you will get pixilation. 当然,每当您放大图像时,都将获得像素化。
Edit: 编辑:
Following is the code that paints the Icon to a BufferedImage which can then be scaled: 以下是将图标绘制为BufferedImage的代码,然后可以对其进行缩放:
Icon icon = UIManager.getIcon("OptionPane.errorIcon");
BufferedImage bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bufferedImage.createGraphics();
icon.paintIcon(null, g, 0, 0);
g.dispose();
ImageIcon errorIcon = new ImageIcon(bufferedImage.getScaledInstance(15, 15, Image.SCALE_SMOOTH));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.