简体   繁体   中英

Use Icon in jcheckbox instead of text

I need to add an icon of an "envelope" to a 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. Then place it next to a JLabel using an icon.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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