zoukankan      html  css  js  c++  java
  • Java GUI设置图标

    ImageIcon是Icon接口的一个实现类。

    ImageIcon类的构造函数:

    • ImageIcon()
    • ImageIcon(String filename)   //本地图片文件
    • ImageIcon(URL  location)   //网络图片
    • ImageIcon(byte[]  imageData)
    • ImageIcon(Image image)   //Image是一个抽象类

    ImageIcon类的常用方法:

    • setImage(Image image)    
    • getImage()    //返回值是Image类型

    ImageIcon实现的是

    1、设置JLabe、JButton的图标

    1    ImageIcon imageIcon=new ImageIcon("./image/1.png");
    2 
    3         JLabel label1=new JLabel(imageIcon);
    4         JLabel label2=new JLabel("test",imageIcon,SwingConstants.CENTER);
    5         
    6         JButton button1=new JButton(imageIcon);
    7         JButton button2=new JButton("提交",imageIcon);

    ./表示项目的根目录。

    2、设置程序左上角的图标

    1 ImageIcon imageIcon=new ImageIcon("./image/1.png");
    2         frame.setIconImage(imageIcon.getImage());  
    3                 
    4         /*
    5        参数是Image抽象类的对象。ImageIcon实现的是Icon接口 ,并没有实现Image抽象类。
    6        需要使用getImage()获取Image对象    
    7         */

    JFrame、JDialog均可使用此种方式设置窗口左上角的图标。

    此图标就是程序在任务栏显示的图标。

  • 相关阅读:
    常见试题
    Makefile学习笔记
    安装java
    IntelliJ IDEA for mac 快捷键
    JPA--Caused by: javax.persistence.PersistenceException: [PersistenceUnit: mysqlJPA] Unable to configure EntityManagerFactory
    log4j 配置
    Numbers
    mac 命令
    Intellij IDEA for mac 快捷键
    vim命令
  • 原文地址:https://www.cnblogs.com/chy18883701161/p/11069993.html
Copyright © 2011-2022 走看看