Java如何使用图像图标创建JLabel?
要创建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);
}
});
}
}
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短