您的位置:首页 > 编程语言 > Java开发

JAVA中Icon接口的应用(以JLabel为例)

2012-01-02 18:07 525 查看
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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: