[英]How do I reduce the size of the JCheckBox icon?
I need to reduce the size of a JCheckBox item, which has no text.我需要减小没有文本的 JCheckBox 项目的大小。 So far I tried to override the methods
getPreferredSize()
and getMaximumSize()
.到目前为止,我尝试覆盖方法
getPreferredSize()
和getMaximumSize()
。 Also setting the Font size or the size of the JCheckBox itself doesn't evoke any changes.此外,设置字体大小或 JCheckBox 本身的大小不会引起任何更改。 Is there a way to achieve this?
有没有办法实现这一目标?
If you're talking about an Icon that is added to the JCheckBox, then best would be to create a new Icon from a new Image that is a resize of the original image.如果您正在谈论添加到 JCheckBox 的图标,那么最好从调整原始图像大小的新图像创建一个新图标。 You can do this with a Image by calling the
yourImage.getScaledInstance(...);
您可以通过调用
yourImage.getScaledInstance(...);
对 Image 执行此操作yourImage.getScaledInstance(...);
method.方法。 Once you get the new image, create a
new ImageIcon(newImage)
and use it with your JCheckBox.获得新图像后,创建一个
new ImageIcon(newImage)
并将其与 JCheckBox 一起使用。
eg例如
Image oldImage = oldIcon.getImage();
Image newImage = oldImage.getScaledInstance(newWidth, newHeight,
Image.SCALE_DEFAULT);
Icon newIcon = new ImageIcon(newImage);
checkBox.setIcon(newIcon);
getPreferredSize
and getMaximumSize
only return the values assigned to preferredSize
and maximumSize
, respectively, for the JCheckBox
object.对于
JCheckBox
对象, getPreferredSize
和getMaximumSize
只返回分别分配给preferredSize
和maximumSize
的值。 If you want to change these values, use setPreferredSize
and setMaximumSize
instead:如果要更改这些值,请改用
setPreferredSize
和setMaximumSize
:
jCheckBox.setPreferredSize(new Dimension(100, 300));
and/or:和/或:
jCheckBox.setMaximumSize(new Dimension(200, 600));
You can override the paint function like this:你可以像这样覆盖paint函数:
final JCheckBox cb = new JCheckBox("",autoChangeTab) {
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.scale(.9, .9);
super.paint(g);
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.