简体   繁体   English

在jcheckbox中使用图标代替文本

[英]Use Icon in jcheckbox instead of text

I need to add an icon of an "envelope" to a jcheckbox. 我需要在jcheckbox中添加一个“信封”图标。 The documentation seems to focus on replacing the selected/unselected state icons. 该文档似乎集中于替换选定/未选定状态图标。

I am happy with those - but I want an "envelope" picture rather than text saying "email". 我对这些内容感到满意-但我想要一张“信封”图片,而不是显示“电子邮件”的文字。

Create a JCheckbox without a label. 创建一个不带标签的JCheckbox。 Then place it next to a JLabel using an icon. 然后使用图标将其放置在JLabel旁边。

public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel();

    JCheckBox box = new JCheckBox();
    JLabel label = new JLabel(myIcon);

    panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
    panel.add(box);
    panel.add(label);

    frame.getContentPane().add(panel);
    frame.pack();
    frame.setVisible(true);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM