Icon标签

Icon标签

package com.zishi.lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {

public ImageIconDemo(){
//获取图片地址
JLabel label = new JLabel("ImageIcon");
URL url = ImageIconDemo.class.getResource("Amucs.jpg"); //获取当前类的同级目录下的资源

ImageIcon imageIcon = new ImageIcon(url);//命名不要冲突了
label.setIcon(imageIcon);
label.setHorizontalAlignment(SwingConstants.CENTER);

Container container = getContentPane();
container.add(label);

setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}


public static void main(String[] args) {
new ImageIconDemo();
}
}

posted @ 2021-08-22 20:56  子时未临  阅读(52)  评论(0编辑  收藏  举报