JLabel label = new JLabel("Hello"); Icon icon = new ImageIcon("myicon.png"); label.setIcon(icon); SwingUtilities.layoutCompoundLabel(label.getFontMetrics(label.getFont()), label.getText(), icon, SwingConstants.LEFT, SwingConstants.RIGHT, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getInsets(), label.getIconTextGap(), label.getPreferredSize());
JLabel label = new JLabel("Hello"); Icon icon = new ImageIcon("myicon.png"); label.setIcon(icon); SwingUtilities.layoutCompoundLabel(label.getFontMetrics(label.getFont()), label.getText(), icon, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, SwingConstants.CENTER, label.getInsets(), label.getIconTextGap(), label.getPreferredSize());
JLabel label = new JLabel("Hello"); Icon icon = new ImageIcon("myicon.png"); label.setIcon(icon); SwingUtilities.layoutCompoundLabel(label.getFontMetrics(label.getFont()), label.getText(), icon, SwingConstants.RIGHT, SwingConstants.LEFT, label.getVerticalAlignment(), label.getHorizontalAlignment(), label.getInsets(), label.getIconTextGap(), label.getPreferredSize());In this example, the text is aligned to the left and the icon is aligned to the right within the JLabel. These code examples demonstrate the various ways in which layoutCompoundLabel can be used to position and align the icon and text within a JLabel.