JRadioButton: how to replace text by IconImage?

徘徊边缘 提交于 2019-12-19 08:07:32

问题


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

I've tried this:

rotateButton = new JRadioButton(rotateIcon.getImage());

But this replaces the radio button and text by the icon. I would like to keep the radio button and display the image.

What should I do?


What I'm currently getting is:

But I want it to end up with this:


回答1:


public JRadioButton(String text, Icon icon) and simple example here




回答2:


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



回答3:


I just reproduced your described behavior using this source:

import java.awt.Image;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.net.URL;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        URL url = new URL("http://www.gravatar.com/avatar/" +
            "a1ab0af4997654345d7a949877f8037e?s=128");
        Image image = ImageIO.read(url);
        final ImageIcon imageIcon = new ImageIcon(image);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JRadioButton radioButton = new JRadioButton("A.T.", imageIcon);
                JOptionPane.showMessageDialog(null, radioButton);
            }
        });
    }
}

It seems like a bug to me, though I cannot recall seeing a radio with an icon. How are they supposed to look?


Time to reach into my 'box of hacks'.

import javax.swing.*;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        String url = "http://www.gravatar.com/avatar/" +
            "a1ab0af4997654345d7a949877f8037e?s=128";
        final String html = "<html><body><img src='" +
            url +
            "' width=128 height=128>";
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                JRadioButton radioButton = new JRadioButton(html);
                JOptionPane.showMessageDialog(null, radioButton);
            }
        });
    }
}

This technique will not work if:

  1. The use-case requires other types of icons (pressed, roll-over, selected etc.)
  2. The button is disabled (it will render incorrectly).



回答4:


There is no radio button constructor that allows an image as the content argument instead of a text. The only way to replace the text of a radio button by an image it is generate html and pass it as an argument to the default constructor.

import javax.swing.*;

class RadioWithImage {

    public static void main(String[] args) throws Exception {
        URL url = Windows_ChordsGenerator.class.getResource("/images/img1.png");

        final String html = "<html><body><img src='" + url.toString() +"'>";

        JRadioButton radioButton = new JRadioButton(html);     
       }
}


来源:https://stackoverflow.com/questions/6774987/jradiobutton-how-to-replace-text-by-iconimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!