Java JRadioButton:如何用IconImage替换文本?

Java JRadioButton:如何用IconImage替换文本?,java,swing,jradiobutton,Java,Swing,Jradiobutton,我想用图标替换单选按钮列表中的文本 我试过这个: rotateButton = new JRadioButton(rotateIcon.getImage()); 但这将用图标替换单选按钮和文本。我想保留单选按钮并显示图像 我该怎么办 我目前得到的是: 但我希望它以这样的方式结束: 还有一个简单的例子我刚刚使用以下来源再现了您描述的行为: import java.awt.Image; import javax.swing.*; import javax.imageio.ImageIO; im

我想用图标替换单选按钮列表中的文本

我试过这个:

rotateButton = new JRadioButton(rotateIcon.getImage());
但这将用图标替换单选按钮和文本。我想保留单选按钮并显示图像

我该怎么办


我目前得到的是:

但我希望它以这样的方式结束:


还有一个简单的例子

我刚刚使用以下来源再现了您描述的行为:

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

对我来说,这似乎是一个错误,尽管我不记得看到过一台带有图标的收音机。他们看起来怎么样


是时候进入我的“黑客盒子”了

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);
            }
        });
    }
}
import javax.swing.*;
带图像的类{
公共静态void main(字符串[]args)引发异常{
字符串url=”http://www.gravatar.com/avatar/" +
“a1ab0af4997654345d7a949877f8037e?s=128”;
最终字符串html=“”;
SwingUtilities.invokeLater(新的Runnable(){
公开募捐{
JRadioButton radioButton=新的JRadioButton(html);
showMessageDialog(空,单选按钮);
}
});
}
}

如果出现以下情况,此技术将不起作用:

  • 用例需要其他类型的图标(按下、翻滚、选择等)
  • 该按钮已禁用(将不正确渲染)

  • 创建一个没有文本的JRadioButton,并在其旁边放置一个带有图像的JLabel。您还可以创建一个类来隐藏复杂性

    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
    

    没有单选按钮构造函数允许将图像作为内容参数而不是文本。用图像替换单选按钮文本的唯一方法是生成html并将其作为参数传递给默认构造函数

    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);     
           }
    }
    
    import javax.swing.*;
    带图像的类{
    公共静态void main(字符串[]args)引发异常{
    URL=Windows_ChordsGenerator.class.getResource(“/images/img1.png”);
    最终字符串html=“”;
    JRadioButton radioButton=新的JRadioButton(html);
    }
    }
    
    @Eng.Fouad:我在大多数的秋千问题中也看到了他,似乎mKorble是一个秋千爱好者。@Eng.Fouad因为有很多有趣、善良的人,他们面容平静,学习英语:-)这对我来说就像我所知道的任何PL一样困难,“对我来说就像我所知道的任何PL一样困难……”编程语言比英语逻辑性强得多抱歉,这不是我要找的,我想保留现有的收音机,并用图像替换文本。因为这里是用图片替换默认单选按钮并保留文本这个答案是错误的,不应该被接受。@andrew-thompson给出了一个正确的答案。看起来Java6忘记了这一点,但我认为您是Htlm大师之一+1@mKorbel哦,不!对于Java程序员来说,我似乎是一个HTML专家,但是对于web设计师来说,我是一个新手。但是谢谢你这么说顺便说一句,你的回答很好,+1。我喜欢包含最新JDOC链接的答案。:)我真的很困惑如何快速编码今天的新手伟大的答案!这比@Andrew Thompson的答案更有用,因为它使用的是实际上与jar捆绑在一起的图像,而不是外部资源。