开发者

Change size of ImageIcon in a JRadioButton

I simply want to change the size (diameter) of the actua开发者_运维问答l (default) ImageIcon of my JRadioButton. I've changed the size of the font displayed in the widget, so it really looks silly with such a large radiobutton.

JRadioButton button = new JRadioButton("Button");
button.setFont(new Font("Lucida Grande",Font.PLAIN, 11));

gives me this giant button:

Change size of ImageIcon in a JRadioButton

Do I really have to create my own ImageIcon? Or can I somehow scale the default one, without too much of a hassle?


There are no icons added to a JRadioButton. The UI just paints Icons as needed so you can't use the getIcon...() methods to get the icons and scale then. So as a work around you need to create your own image of the icons first before you scale them.

Here is something to get you started. The Screen Image class makes creating images of a component easy. Note you will also need to create images for the "rollover" icons. This can be done by setting the ButtonModel.setRollover(true) method for both the normal and selected states of the radio button.

import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.border.*;

public class RadioButtonScale extends JFrame
{
    private static void createAndShowGUI()
    {
        JPanel north = new JPanel(new FlowLayout(0, 0, FlowLayout.LEFT));

        JRadioButton button = new JRadioButton("Radio Button");
        north.add(button);

        JPanel south = new JPanel();
        JLabel west = new JLabel();
        west.setBorder(new LineBorder(Color.GREEN));
        south.add(west, BorderLayout.WEST);
        JLabel east = new JLabel();
        east.setBorder(new LineBorder(Color.GREEN));
        south.add(east, BorderLayout.EAST);

        //  Create images of radio button icon

        Icon icon = UIManager.getIcon("RadioButton.icon");
        Dimension preferred = button.getPreferredSize();
        Insets insets = button.getInsets();
        int height = preferred.height - insets.top - insets.bottom;
//      Rectangle r = new Rectangle(insets.left, insets.top, height, height);
        Rectangle r = new Rectangle(insets.left, insets.top, icon.getIconWidth(), height);

        west.setIcon( new ImageIcon( ScreenImage.createImage(button, r) ) );
        button.setSelected(true);
        east.setIcon( new ImageIcon( ScreenImage.createImage(button, r) ) );

        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(north, BorderLayout.NORTH);
        frame.add(south, BorderLayout.SOUTH);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}


If you are developing only for Mac Platform (based on your screen-shot) You may consider using apple's client properties "mini"

component.putClientProperty("JComponent.sizeVariant", "mini");

as shown at this link:

http://developer.apple.com/mac/library/technotes/tn2007/tn2196.html#SIZE_EXAMPLES


Given an ImageIcon, you can derive a new, scaled instance as follows:

    ImageIcon original = ...;

    Image img = original.getImage();
    Image scaledImage = img.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT));
    ImageIcon newInstance = new ImageIcon(scaledImage);

Note however, that repeatedly scaling an image will degrade the quality.


Don't know if I got the actual question wrong but ... Why not create a gif image for the radio button, or enlarge current one? Load it in with ImageIcon g = new ImageIcon(NAME OF FILE). Then, assign it to the radio button with nameOfRadioButton.setIcon(g);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜