简体   繁体   中英

How do I reduce the size of the JCheckBox icon?

I need to reduce the size of a JCheckBox item, which has no text. So far I tried to override the methods getPreferredSize() and getMaximumSize() . Also setting the Font size or the size of the JCheckBox itself doesn't evoke any changes. 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. You can do this with a Image by calling the yourImage.getScaledInstance(...); method. Once you get the new image, create a new ImageIcon(newImage) and use it with your 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. If you want to change these values, use setPreferredSize and setMaximumSize instead:

jCheckBox.setPreferredSize(new Dimension(100, 300));

and/or:

jCheckBox.setMaximumSize(new Dimension(200, 600));

You can override the paint function like this:

final JCheckBox cb = new JCheckBox("",autoChangeTab) {
    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        g2d.scale(.9, .9);
        super.paint(g);
    }
};

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