Kri*_*hna 2 java swing text image jradiobutton
我JRadioButton在我的应用程序中使用 Swing 。我需要在我的按钮上设置图像和文本。为此,我正在使用这个:
JRadioButton button1 = new JRadioButton("text", iconpath, false);
Run Code Online (Sandbox Code Playgroud)
但它给出的输出是隐藏单选按钮并只显示图像。
如何解决这个问题,有什么建议吗?我们也可以为 JCheckbox 创建类似问题的东西吗?
设置 a 的图标JRadioButton或JCheckBox替换这些控件使用的默认字形 - 我知道,烦人。
最简单的解决方案是简单地创建一个JLabel可与关联JRadioButton,可能使用某种Map保持之间的联系
一个更长期的解决方案可能是创建一个自定义组件,将概念结合到一个自定义和可重用的组件中,例如......
public class XRadioButton extends JPanel {
private JRadioButton radioButton;
private JLabel label;
public XRadioButton() {
setLayout(new GridBagLayout());
add(getRadioButton());
add(getLabel());
}
public XRadioButton(Icon icon, String text) {
this();
setIcon(icon);
setText(text);
}
protected JRadioButton getRadioButton() {
if (radioButton == null) {
radioButton = new JRadioButton();
}
return radioButton;
}
protected JLabel getLabel() {
if (label == null) {
label = new JLabel();
label.setLabelFor(getRadioButton());
}
return label;
}
public void addActionListener(ActionListener listener) {
getRadioButton().addActionListener(listener);
}
public void removeActionListener(ActionListener listener) {
getRadioButton().removeActionListener(listener);
}
public void setText(String text) {
getLabel().setText(text);
}
public String getText() {
return getLabel().getText();
}
public void setIcon(Icon icon) {
getLabel().setIcon(icon);
}
public Icon getIcon() {
return getLabel().getIcon();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3892 次 |
| 最近记录: |