摆动:调整RadioButton的大小 [英] Swing: Resizing RadioButton

查看:252
本文介绍了摆动:调整RadioButton的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的应用中实现字体大小切换.但是,当我增加字体大小时,RadioButtons保持不变,并且在高分辨率的小屏幕上,我的客户无法轻松按下它.有没有一种方法可以通过编程方式调整RadioButton的大小,而无需深入研究L& F并手动重绘图标(这很复杂,因为应用程序针对具有不同UI的多个平台,并且每个平台必须有7个图标).

I need to implement font size switching in my app. But when I increase font's size RadioButtons remain same size and on small screen with high resolution my customer just can't hit it easily. Is there a way to resize RadioButton's round thing programmatically without diging into L&F and redrawing Icons manually (it's complicated since app targets multiple platforms with different UIs and each of them must have 7 icons).

完美的解决方案如下所示:

Perfect solution could look like this:

  1. 提取本机UI图标.
  2. 调整大小
  3. 将调整大小的图标设置为组件的图标.

如何实施第1步?有可能吗?

编辑:这是我到目前为止尝试过的

EDIT: this is what i tried so far

public class IconImageSaver extends JFrame{

    public IconImageSaver() {

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBounds(0,0,100,100);
        setVisible(true);

        JRadioButton rad1 = new JRadioButton();
        rad1.setBounds(10,10,40,40);
        add(rad1);

        Icon icon = UIManager.getIcon("RadioButton.icon");//(1) trying to get icon

        int w = icon.getIconWidth(),h = icon.getIconHeight();
        Image i = rad1.createImage(w, h);
        Image i2 = rad1.createImage(w,h);
        Graphics g = i.getGraphics();
        Graphics g2 = i2.getGraphics();


        g.setColor(Color.CYAN);
        g.fillRect(0, 0, w, h);
        rad1.setIcon(new ImageIcon(i));//setting icons
        g2.setColor(Color.RED);
        g2.fillRect(0, 0, w, h);
        rad1.setPressedIcon(new ImageIcon(i2));//setting icons
    }

    public static void main(String[] args) {
        new IconImageSaver();
    }

}

在位置(1),我试图获取图标图像,但它仅返回背景色. 不明白为什么. 设置各种状态的图标可以正常工作.

At position (1) i'm trying to get icon image, but it returns only background color. Can't understand why. Setting icons for various states works as intended.

推荐答案

某些L& F(例如 Nimbus Aqua )支持 large JComponent.sizeVariant,如 调整组件大小所述em> 使用客户端属性 .

Some L&Fs (e.g. Nimbus, Aqua) support a large JComponent.sizeVariant, as discussed in Resizing a Component and Using Client Properties.

附录:我必须使用纯本地L& F .

JRadioButton的呈现取决于其关联的ButtonUI委托.由本地L& F提供的代表的内部通常是不可访问的,并且依赖于主机平台API.您必须使用用户选择的L& F的可用功能或提供您自己的功能.如果您可以解释更多有关潜在问题的信息,则可能有助于提出更好的替代方案.

The rendering of a JRadioButton is determined by its associated ButtonUI delegate. The internals of delegates supplied by the native L&F are generally inaccessible and rely on host platform APIs. You have to use the available feature(s) of the user's chosen L&F or supply your own. If you can explain more about the underlying problem, it may help to suggest better alternatives.

附录:如果没有开发完整的L& F,则可以与单选按钮的父JToggleButton一起使用.这样的按钮在ButtonGroup中效果很好,如此处所示,并且可以任意装饰,如此处.

Addendum: Absent developing a complete L&F, it may be possible to work with the radio button's parent, JToggleButton. Such buttons work well in a ButtonGroup, as shown here, and they can be decorated arbitrarily, as outlined here.

这篇关于摆动:调整RadioButton的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆