gpt4 book ai didi

java - JRadioButton:如何用 IconImage 替换文本?

转载 作者:搜寻专家 更新时间:2023-10-31 08:08:00 25 4
gpt4 key购买 nike

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

我已经试过了:

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

但这用图标替换了单选按钮和文本。我想保留单选按钮并显示图像。

我该怎么办?


我目前得到的是:

Enter image description here

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

Enter image description here

最佳答案

创建一个没有文本的 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

关于java - JRadioButton:如何用 IconImage 替换文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6774987/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com