e740. 向标签中加入一个图标

时间:2022-04-14 15:55:24

This example creates a JLabel component with an icon.

    // Fetch icon
Icon icon = new ImageIcon("icon.gif"); // Create a label with text and an icon; the icon appears to the left of the text
JLabel label = new JLabel("Text Label", icon, JLabel.CENTER); // Create a label with only an icon
label = new JLabel(icon);

This example adds or replaces the icon in an existing JLabel component:

    // Add an icon to an existing label
label.setIcon(icon);

The methods to control the position of the icon and text within a JLabel component are identical to those of a JButton. See also e747 在按钮上移动图标的位置, e748 在按钮上移动图标和标注的位置, e749 设置按钮上图标和标注的间隔, and e750 在按钮上加上不可用图标.