GUI Swing 3.3 标签

3.3 标签

label-icon

//标签 --- 图标操作 ---自己画
//图标 是一个接口,需要实现类,同时继承JFrame
public class IconDemo extends JFrame implements Icon {
    private int width;
    private  int height;
    public IconDemo() { }
    public IconDemo(int width,int height){
        this.width = width;
        this.height = height;
    }

    //初始化
    public void init(){
        IconDemo iconDemo = new IconDemo(15,15);
        setBounds(200,200,500,500);
        Container contentPane = getContentPane();
        //图标放在标签上,也可以是按钮上
        JLabel jLabel = new JLabel("lable1",iconDemo,SwingConstants.CENTER);
        contentPane.add(jLabel);
        setVisible(true);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    public static void main(String[] args) {
        new IconDemo().init();
    }

    @Override
    public void paintIcon(Component c, Graphics g, int x, int y) {
        g.fillOval(x,y,this.height,this.width);
    }

    @Override
    public int getIconWidth() {
        return this.width;
    }

    @Override
    public int getIconHeight() {
        return this.height;
    }
}

image icon

//图片图标
public class IconImageDemo extends JFrame implements Icon {
    public IconImageDemo(){}
    public void init(){
        //获取同类下,图片地址
        URL url = IconImageDemo.class.getResource("3.jpg");
        //创建图片图标,存放图片
        ImageIcon imageIcon = new ImageIcon(url);
        setVisible(true);
        setBounds(300,300,500,500);
        JLabel jLabel = new JLabel(imageIcon,SwingConstants.CENTER);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        Container contentPane = getContentPane();
        contentPane.add(jLabel);
    }

posted @   NeverGGp  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示