我如何定义大尺寸的JRadioButton?

时间:2014-11-10 05:15:56

标签: java swing jradiobutton

我正在学习java。对于我的GUI程序需要大型单选按钮(大于标准)。我该怎么办?

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

1 个答案:

答案 0 :(得分:3)

您可以为单选按钮提供自己的图片,有关详细信息,请参阅JRadioButton#setIconJRadioButton#setSelectedIconHow to Use Buttons, Check Boxes, and Radio Buttons ...

enter image description here enter image description here

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();
            }
        }

    }

}