固定大小图片显示
javax.swing.ImageIcon imageIconSource = new ImageIcon("cool.jpg");
java.awt.Image image = imageIconSource.getImage();
Image tempImage = image.getScaledInstance(100,100,Image.SCALE_DEFAULT);
ImageIcon imageIcon = new ImageIcon(tempImage);
javax.swing.JLabel photoView = new JLabel();
photoView.setIcon(imageIcon);
========================================================
通常让这些控件加载图片的代码如下:
JButton addTebBtn = new JButton(new ImageIcon(TabbedPanel.class
.getResource("/addTab.gif")));
如果要显示动态Gif图片这样做法就不灵了.如果要显示动态Gif图片的话,我们需要从JLabel,JButton等控件继承一个类,并重载其public void paint(Graphics g)方法,然后用一个线程不断去刷新它(用Timer也可以,请参考文章" 封装完毕,能显示当前时间并改变风格的菜单类 ( http://www.blogjava.net/sitinspring/archive/2007/06/08/122753.html )"中Timer 的做法,它有少实现一个Runnable接口的优势),这样gif的动态效果就显示出来了.
标签的完整代码如下,其它控件大家可自行参照实现:
package
com.junglesong.common.component.label;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
java.awt.Graphics;
import
java.awt.Graphics2D;
import
java.awt.Image;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
import
javax.swing.JLabel;
![](https://i-blog.csdnimg.cn/blog_migrate/4f1150b881333f12a311ae9ef34da474.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/1fa987a29c6482f53d401256f96355eb.gif)
public
class
DynGifLabel
extends
JLabel
implements
Runnable
{
private static final long serialVersionUID = 45345345355L;
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
// 用以存储Gif动态图片
public Image image;
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
// 用以刷新paint函数
Thread refreshThread;
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** *//**
*
* @param image:
* Sample:new ImageIcon(DynGifLabel.class
* .getResource("/picture.gif")).getImage()
*/
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public DynGifLabel(Image image)
{
this.image = image;
refreshThread = new Thread(this);
refreshThread.start();
}
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** *//**
* 重载paint函数
*/
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public void paint(Graphics g)
{
super.paint(g);
Graphics2D graph = (Graphics2D) g;
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
if (image != null)
{
// 全屏描绘图片
graph.drawImage(image, 0, 0, getWidth(), getHeight(), 0, 0, image
.getWidth(null), image.getHeight(null), null);
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/d18c02628675d0a2c816449d98bda930.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
/** *//**
* 隔100毫秒刷新一次
*/
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
public void run()
{
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
while (true)
{
this.repaint();// 这里调用了Paint
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
try
{
Thread.sleep(100);// 休眠100毫秒
![](https://i-blog.csdnimg.cn/blog_migrate/97e794c86028c5f5b5461ae5ef440a4c.gif)
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
使用过程如下:
DynGifLabel stateLbl
=
new
DynGifLabel(
new
ImageIcon(ThreadPanel.
class
.getResource(
"
/startThread.gif
"
)).getImage());