ImageIcon

它根據 Image 繪製 Icon。可使用 MediaTracker 預載根據 URL、檔案名稱或位元組數組創建的圖像,以監視該圖像的載入狀態。

簡介

ImageIcon類

實現的接口: Serializable, Accessible, Icon

父類:Object

示例

import java.awt.FileDialog;

import java.awt.Frame;

import java.awt.Menu;

import java.awt.MenuBar;

import java.awt.MenuItem;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.WindowAdapter;

import java.awt.event.WindowEvent;

import javax.swing.Icon;

import javax.swing.ImageIcon;

import javax.swing.JLabel;

/**

*

* @author Administrator

*/

public class fabu {

/**

* @param args

* the command line arguments

*/

@SuppressWarnings("deprecation")

public static void main(String[] args) {

// TODO code application logic here

final Frame f = new Frame("圖片上傳");// 標題

final JLabel lbl = new JLabel();

f.setSize(600, 400);// 對話框大小

f.setLocation(100, 100);// 對話框初始位置

f.addWindowListener(new WindowAdapter() {

public void windowClosing(WindowEvent e) {

System.exit(0);

}

});

MenuBar mb = new MenuBar();

Menu m1 = new Menu("photolist");

Menu m2 = new Menu("video欄");

MenuItem mi1 = new MenuItem("photo1");

MenuItem mi2 = new MenuItem("open");

mi2.addActionListener(new ActionListener() { // 打開資料夾

public void actionPerformed(ActionEvent e) {

FileDialog fd = new FileDialog(f, "photo open",

FileDialog.LOAD);

fd.show();

String fileName = fd.getDirectory() + fd.getFile();

Icon icon = new ImageIcon(fileName,"");

lbl.setIcon(icon);

}

});

MenuItem mi3 = new MenuItem("photo3");

MenuItem mi4 = new MenuItem("photo4");

MenuItem mi5 = new MenuItem("video1");

MenuItem mi6 = new MenuItem("video2");

m1.add(mi1);

m1.add(mi2);

m1.add(mi3);

m1.add(mi4);

m2.add(mi5);

m2.add(mi6);

mb.add(m1);

mb.add(m2);

f.setMenuBar(mb);

f.add(lbl);

f.show();

}

}

相關詞條

相關搜尋

熱門詞條

聯絡我們