简体   繁体   English

更改JRadioButton中ImageIcon的大小

[英]Change size of ImageIcon in a JRadioButton

I simply want to change the size ( diameter ) of the actual ( default ) ImageIcon of my JRadioButton . 我只是想改变我的JRadioButton的实际( 默认ImageIcon的大小( 直径 )。 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: 给了我这个巨大的按钮:

GIANT按钮!!

Do I really have to create my own ImageIcon ? 我真的需要创建自己的ImageIcon吗? Or can I somehow scale the default one, without too much of a hassle? 或者我可以以某种方式缩放默认值,没有太多的麻烦?

There are no icons added to a JRadioButton. JRadioButton没有添加图标。 The UI just paints Icons as needed so you can't use the getIcon...() methods to get the icons and scale then. 用户界面只是根据需要绘制图标,因此您无法使用getIcon ...()方法获取图标和比例。 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. Screen Image类使得创建组件的图像变得容易。 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. 这可以通过为单选按钮的正常状态和选定状态设置ButtonModel.setRollover(true)方法来完成。

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" 如果您只为Mac平台开发(基于您的屏幕截图)您可以考虑使用Apple的客户端属性“mini”

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

as shown at this link: 如此链接所示:

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

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? 不知道我是否得到了错误的实际问题,但是......为什么不为单选按钮创建一个gif图像,或者放大当前的一个? Load it in with ImageIcon g = new ImageIcon(NAME OF FILE) . 使用ImageIcon g = new ImageIcon(NAME OF FILE)加载它。 Then, assign it to the radio button with nameOfRadioButton.setIcon(g) ; 然后,使用nameOfRadioButton.setIcon(g)将其分配给单选按钮;

Given an ImageIcon, you can derive a new, scaled instance as follows: 给定ImageIcon,您可以导出一个新的缩放实例,如下所示:

    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. 但请注意,重复缩放图像会降低质量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM