【问题标题】:How I can define a JRadioButton with large size?如何定义大尺寸的 JRadioButton?
【发布时间】:2014-11-10 05:15:56
【问题描述】:

我正在学习 Java。对于我的 GUI 程序需要大的单选按钮(大于标准)。我能做什么?

我使用 Java Netbeans IDE - 最新版本。

【问题讨论】:

  • 你能提供一些代码来展示你尝试过的东西吗?

标签: java swing jradiobutton


【解决方案1】:

您可以为单选按钮提供自己的图像,请参阅JRadioButton#setIconJRadioButton#setSelectedIconHow to Use Buttons, Check Boxes, and Radio Buttons 了解更多详情...

import java.awt.EventQueue;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class RadioButtonTest {

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

    public RadioButtonTest() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            try {
                BufferedImage checked = ImageIO.read(getClass().getResource("/Checked.png"));
                Image unchecked = ImageIO.read(getClass().getResource("/Unchecked.png")).getScaledInstance(300, 300, Image.SCALE_SMOOTH);

                JRadioButton btn = new JRadioButton("I'm not fat, I'm just big boned");
                btn.setSelectedIcon(new ImageIcon(checked));
                btn.setIcon(new ImageIcon(unchecked));
                btn.setHorizontalTextPosition(JRadioButton.CENTER);
                btn.setVerticalTextPosition(JRadioButton.BOTTOM);

                setLayout(new GridBagLayout());
                add(btn);
            } catch (IOException exp) {
                exp.printStackTrace();
            }
        }

    }

}

【讨论】:

  • 也考虑JComponent.sizeVariant,引用here
  • @AndrewThompson 我曾考虑使用JLayer/JXLayer
  • 根据您的需要,您还可以使用JLayer/JXLayer 来缩放组件,for example
  • 非常感谢大家。你给我的建议很有帮助。
  • @Saideh 很高兴这一切都有帮助
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 2019-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
相关资源
最近更新 更多