This example shows the drawing of an icon using the Icon interface for the JLable component.
package com.han; import java.awt.*; import javax.swing.*; /** * This example shows the drawing of an icon using the Icon interface * for the JLable component. * @author han * */ public class DrawIcon implements Icon{ private int width; private int height; @Override public int getIconHeight(){ return this.height; } @Override public int getIconWidth(){ return this.width; } @Override public void paintIcon(Component c, Graphics g, int x, int y){ g.setColor(Color.red); g.fillOval(x, y, width, height); } /*the construct function*/ public DrawIcon(int width, int height){ this.width=width; this.height=height; } public static void main(String[] args){ DrawIcon icon=new DrawIcon(15,15); JLabel jl=new JLabel("测试",icon,SwingConstants.CENTER); JFrame jf=new JFrame(); Container c=jf.getContentPane(); c.add(jl); jf.setVisible(true); jf.setSize(300,300); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }JAVA中Icon接口的应用(以JLabel为例)
最新推荐文章于 2021-12-21 01:12:23 发布
本文展示如何利用Icon接口在JLabel组件中绘制图标,通过自定义类DrawIcon实现图标绘制,并将其应用到JLabel实例上,演示了如何在Swing应用程序中灵活使用图标元素。
316

被折叠的 条评论
为什么被折叠?



