JRadioButton: how to replace text by IconImage?

前端 未结 4 1691
不思量自难忘°
不思量自难忘° 2021-01-13 10:22

I want to replace the text in my radio button list by an icon.

I\'ve tried this:

rotateButton = new JRadioButton(rotateIcon.getImage());
4条回答
  •  梦毁少年i
    2021-01-13 11:02

    Create a JRadioButton with no text and put a JLabel with the image next to it. You can also create a class to hide complexity.

    import java.awt.event.ActionListener;
    
    import javax.swing.*;
    import javax.swing.event.ChangeListener;
    
    public class RadioButtonWithImage extends JPanel {
    
    private JRadioButton radio = new JRadioButton();
    private JLabel image;
    
    public RadioButtonWithImage(Icon icon) {
        image = new  JLabel(icon);
        add(radio);
        add(image);
    }
    
    public void addToButtonGroup(ButtonGroup group) {
        group.add(radio);
    }
    
    public void addActionListener(ActionListener listener) {
        radio.addActionListener(listener);
    }
    
    public void addChangeListener(ChangeListener listener) {
        radio.addChangeListener(listener);
    }
    
    public Icon getImage() {
        return image.getIcon();
    }
    
    public void setImage(Icon icon) {
        image.setIcon(icon);
    }
    
    } // end class RadioButtonWithImage
    

提交回复
热议问题