要创建JLabel带有图片图标的图片,我们可以将anImageIcon作为第二个参数传递给JLabel构造函数,也可以使用JLabel.setIcon()方法设置图标。
package org.nhooo.example.swing; import javax.swing.*; import java.awt.*; public class JLabelWithIcon extends JFrame { public JLabelWithIcon() throws HeadlessException { initialize(); } private void initialize() { setSize(300, 300); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); setLayout(new FlowLayout(FlowLayout.LEFT)); Icon icon = new ImageIcon("ledgreen.png"); JLabel label1 = new JLabel("Full Name :", icon, JLabel.LEFT); JLabel label2 = new JLabel("Address :", JLabel.LEFT); label2.setIcon(new ImageIcon("ledyellow.png")); getContentPane().add(label1); getContentPane().add(label2); } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { public void run() { new JLabelWithIcon().setVisible(true); } }); } }