【问题标题】:Change size of ImageIcon in a JRadioButton更改 JRadioButton 中 ImageIcon 的大小
【发布时间】:2011-02-04 14:33:12
【问题描述】:

我只是想更改我的JRadioButton 的实际(默认ImageIcon 的大小(直径)。我已经更改了小部件中显示的字体大小,所以用这么大的单选按钮看起来真的很傻。

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

给我这个巨大的按钮:

我真的必须创建自己的ImageIcon 吗?或者我可以以某种方式缩放默认值,而不用太麻烦吗?

【问题讨论】:

  • 令人惊讶的是,SWING 最终没有给出这个简单问题的答案。你有我的声援。

标签: java user-interface swing radio-button


【解决方案1】:

没有添加到 JRadioButton 的图标。 UI 只是根据需要绘制图标,因此您不能使用 getIcon...() 方法来获取图标并进行缩放。因此,作为一种解决方法,您需要先创建自己的图标图像,然后再缩放它们。

这里有一些东西可以帮助您入门。 Screen Image 类使创建组件的图像变得容易。请注意,您还需要为“翻转”图标创建图像。这可以通过为单选按钮的正常状态和选中状态设置 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();
            }
        });
    }
}

【讨论】:

    【解决方案2】:

    如果您仅针对 Mac 平台进行开发(基于您的屏幕截图) 你可以考虑使用苹果的客户端属性“mini”

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

    如此链接所示:

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

    【讨论】:

      【解决方案3】:

      给定一个 ImageIcon,您可以按如下方式派生一个新的缩放实例:

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

      但是请注意,重复缩放图像会降低质量。

      【讨论】:

      • 调用 ImageIcon 的 getImage() 时出现空指针异常。我的 JRadioButton 通过调用 getIcon() 返回一个图标,而不是一个 ImageIcon,所以我将它转换为它,这可能与它有关吗?编辑:出于某种原因,我的 JRadioButtons 的图标似乎为空......
      【解决方案4】:

      不知道我是否把实际问题弄错了,但是... 为什么不为单选按钮创建一个 gif 图像,或者放大当前的图像?使用ImageIcon g = new ImageIcon(NAME OF FILE) 加载它。然后,将其分配给带有nameOfRadioButton.setIcon(g)的单选按钮;

      【讨论】:

      • 看起来问题是如何以编程方式更改它,而不是如何用图片进行更改。
      • 因为你想使用 LAF 使用的图标,只是更小 ;-) @jonsca 没有看到投票的理由......它根本没有回答这个问题......跨度>
      • (它通过了第一个答案队列,所以它可能被另一个用户投票)
      • 谢谢,还没有看到:-) 然后我的评论转到@JNF ...和-1
      • hach ... 好像我之前没看到你的评论是瞎了眼 ;-) 谢谢
      猜你喜欢
      • 2015-09-22
      • 2011-02-20
      • 1970-01-01
      • 2011-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 2023-04-01
      相关资源
      最近更新 更多