简体   繁体   中英

How I can define a JRadioButton with large size?

I'm learning java. for my GUI program need to large radio buttons (larger than the standard). What can I do?

I use Java Netbeans IDE - the latest version.

You can supply you're own images for radio button, see JRadioButton#setIcon , JRadioButton#setSelectedIcon and How to Use Buttons, Check Boxes, and Radio Buttons for more details...

在此输入图像描述在此输入图像描述

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

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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