e754. adding an icon to the label of a jcheckbox component.txt

来自「这里面包含了一百多个JAVA源文件」· 文本 代码 · 共 22 行

TXT
22
字号
Unlike a button, setIcon() does not add an icon to the text label. Rather, in a checkbox, the method is used to customize the icons used to depict its state. However, by using the HTML capabilities in a label, it is possible to add an icon to the label without affecting the state-depicting icons. This example demonstrates the technique. 
    // Define an HTML fragment with an icon on the left and text on the right.
    // The elements are embedded in a 3-column table.
    String label = "<html><table cellpadding=0><tr><td><img src=file:"
        // The location of the icon
        + icon.gif"
        + "/></td><td width="
    
        // The gap, in pixels, between icon and text
        + 3
        + "><td>"
    
        // Retrieve the current label text
        + checkbox.getText()
        + "</td></tr></table></html>";
    
    // Add the icon
    checkbox.setText(label);

 Related Examples 

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?