
package GUI;
import javax.swing.*;
import java.awt.*;
public class Test16_Icon extends JFrame implements Icon{
private int width,height;
public Test16_Icon(){};
public Test16_Icon(int width,int height){
this.width=width;
this.height=height;
}
public void init(){
Test16_Icon test16Icon = new Test16_Icon(150, 150);
setSize(500,500);
setVisible(true);
Container container =getContentPane();
JLabel label = new JLabel("zhan",test16Icon,SwingConstants.CENTER);
container.add(label);
}
@Override
public void paintIcon(Component c, Graphics g, int x, int y) {
g.fillOval(x,y,width,height);
}
@Override
public int getIconWidth() {
return width;
}
@Override
public int getIconHeight() {
return height;
}
public static void main(String[] args) {
new Test16_Icon().init();
}
}

package GUI;
import javax.swing.*;
import java.awt.*;
import java.net.URL;
public class Test16_ImageIcon extends JFrame{
public Test16_ImageIcon(){
URL url = Test16_ImageIcon.class.getResource("piture.png");
ImageIcon imageIcon = new ImageIcon(url);
JLabel imageLabel = new JLabel("ImageLabel",imageIcon ,0);
Container contentPane = getContentPane();
contentPane.add(imageLabel);
setSize(500,500);
setVisible(true);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test16_ImageIcon();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?