Java GUI设置图标
生活随笔
收集整理的這篇文章主要介紹了
Java GUI设置图标
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
ImageIcon是Icon接口的一個(gè)實(shí)現(xiàn)類(lèi)。
ImageIcon類(lèi)的構(gòu)造函數(shù):
ImageIcon()
ImageIcon(String filename) //本地圖片文件
ImageIcon(URL location) //網(wǎng)絡(luò)圖片
ImageIcon(byte[] imageData)
ImageIcon(Image image) //Image是一個(gè)抽象類(lèi)
ImageIcon類(lèi)的常用方法:
setImage(Image image)
getImage() //返回值是Image類(lèi)型
ImageIcon實(shí)現(xiàn)的是
1、設(shè)置JLabe、JButton的圖標(biāo)
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);
./表示項(xiàng)目的根目錄。
2、設(shè)置程序左上角的圖標(biāo)
1 ImageIcon imageIcon=new ImageIcon("./image/1.png");
2 frame.setIconImage(imageIcon.getImage());
3
4 /*
5 參數(shù)是Image抽象類(lèi)的對(duì)象。ImageIcon實(shí)現(xiàn)的是Icon接口 ,并沒(méi)有實(shí)現(xiàn)Image抽象類(lèi)。
6 需要使用getImage()獲取Image對(duì)象
7 */
JFrame、JDialog均可使用此種方式設(shè)置窗口左上角的圖標(biāo)。
此圖標(biāo)就是程序在任務(wù)欄顯示的圖標(biāo)。
總結(jié)
以上是生活随笔為你收集整理的Java GUI设置图标的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql 唯一索引 二叉法_mysql
- 下一篇: comet for java_java