简体   繁体   中英

My JRadioButton change look with setOpaque(false)

I want to remove the background of my JRadioButton but still keeping the same look & feel. Images will speak by themselves:

When I do this code:

JRadioButton myJRadioButton = new JRadioButton("My JRadioButton");
add(myJRadioButton);

I get this:

在此输入图像描述

And when I run it with this code:

JRadioButton myJRadioButton = new JRadioButton("My JRadioButton");
myJRadioButton.setForeground(Color.white); //To see it on the black background.
myJRadioButton.setOpaque(false); 
add(myJRadioButton);

I get this:

在此输入图像描述

I've something like a "star" instead of a great and beautiful circle. And what I want is to keep the great and beautiful circle of the first image but without the default background according to it.

Document says

public void setOpaque(boolean isOpaque)  

If true the component paints every pixel within its bounds. Otherwise, the component may not paint some or all of its pixels, allowing the underlying pixels to show through. The default value of this property is false for JComponent. However, the default value for this property on most standard JComponent subclasses (such as JButton and JTree) is look-and-feel dependent.

It says all.

You can create a class which extends the JRadioButton and add all the properties inside the class:

        setOpaque(false);
        setContentAreaFilled(false);
        setBorderPainted(false);
        setForeground(Color.white);
        setBackground(Color.BLACK);

Sample output:

picture

Code:

public class Sample extends JPanel {

    public Sample() {
        super(new BorderLayout());
        setBackground(Color.BLACK);

        TransparentButton testButton = new TransparentButton("hello");
        testButton.setSelected(true);

        add(testButton, BorderLayout.LINE_START);
        setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
    }

    private static void createAndShowGUI() {
        JFrame frame = new JFrame("Hello Word demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = new Sample();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                createAndShowGUI();
            }
        });
    }

    class TransparentButton extends JRadioButton {
        public TransparentButton(String string) {
            super(string);
            setOpaque(false);
            setContentAreaFilled(false);
            setBorderPainted(false);
            setForeground(Color.white);
            setBackground(Color.BLACK);
        }
    }

}

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