轉載自 這裡
前言 :
javax.swing.ImageIcon is used for images, both to use on buttons and labels, and to draw in a graphics panel. The supported formats are .gif, .jpg, and .png.
載入時機 :
再載入 ImageIcon 時, 你可以有兩個選擇並說明如下 :
- Wait until loaded (recommended where timing is not critical)
- Overlap (use only for performance problems)
載入方法 :
在載入 ImageIcon 的來源可能有以下幾種, 分別說明如下 :
- To load an ImageIcon from a URL
載入 ImageIcon 從 Internet, 這時你需要一個 URL, 範例代碼如下 :
- To load an ImageIcon from a file
A file name in an ImageIcon constructor specifies the file name relative to the location of the class file. This constructor doesn't return until the ImageIcon is completely loaded. Sample code as below :
Warning: Just putting the file name or path in the ImageIcon constructor won't work in general for applets or executable jar files. See discussion of class loader in the NetBeans section below.
- Bundling images in your .jar file using NetBeans and ClassLoader
Let's say you have a directory (cardimages) of images (cardimages/ad.gif, ...), and the program is in a package called cardplayer, and you're trying to load the image within the class Card :
使用介紹 :
- To use an ImageIcon in a JButton
- Other ImageIcon methods
You can find the width and height of an image with :
int w = img.getIconWidth();
int h = img.getIconHeight();
補充說明 :
* 常用 Icon 的下載網址如下 :
* Online JDK API (javax.swing.ImageIcon)
* Class.getResource與ClassLoader.getResource()區別
前言 :
javax.swing.ImageIcon is used for images, both to use on buttons and labels, and to draw in a graphics panel. The supported formats are .gif, .jpg, and .png.
載入時機 :
再載入 ImageIcon 時, 你可以有兩個選擇並說明如下 :
- Wait until loaded (recommended where timing is not critical)
- Overlap (use only for performance problems)
載入方法 :
在載入 ImageIcon 的來源可能有以下幾種, 分別說明如下 :
- To load an ImageIcon from a URL
載入 ImageIcon 從 Internet, 這時你需要一個 URL, 範例代碼如下 :
- To load an ImageIcon from a file
A file name in an ImageIcon constructor specifies the file name relative to the location of the class file. This constructor doesn't return until the ImageIcon is completely loaded. Sample code as below :
- ImageIcon myIcon = new ImageIcon("images/myPic.gif");
- Bundling images in your .jar file using NetBeans and ClassLoader
Let's say you have a directory (cardimages) of images (cardimages/ad.gif, ...), and the program is in a package called cardplayer, and you're trying to load the image within the class Card :
使用介紹 :
- To use an ImageIcon in a JButton
- Other ImageIcon methods
You can find the width and height of an image with :
int w = img.getIconWidth();
int h = img.getIconHeight();
補充說明 :
* 常用 Icon 的下載網址如下 :
* Online JDK API (javax.swing.ImageIcon)
* Class.getResource與ClassLoader.getResource()區別
This message was edited 8 times. Last update was at 28/07/2010 10:28:51
沒有留言:
張貼留言