swing 图片显示

dzy_pb 2008-01-25 10:02:10
在一个按钮选择了图片,怎么让图片显示在界面上,例如一个人的照片,好象swing没有图片控件???
怎么显示!
...全文
2348 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ant 2008-04-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 helin561 的回复:]
哥们挺强的
如果我想引用别的文件夹下的图片 URL该怎么写?
ImageIcon icon = new ImageIcon(this.getClass().getResource("D:\xxx\fefe\aaa.gif"));
好像这样就显示不了了
[/Quote]

你应该先搞清除你的工作目录是在哪里才能脱离绝对路径的引用方式,可以用System.getProperty("user.dir")来查看

另外如果要用ImageIcon,构造函数有一个只需要传入filename就可以了,直接ImageIcon icon = new ImageIcon("D:\xxx\fafe\aaa.gif");
qusic 2008-04-30
  • 打赏
  • 举报
回复
mark,学习
chaorenwopashei 2008-04-30
  • 打赏
  • 举报
回复
呵呵,好久没有用这些东西了都不会用了啊,就是的, lable的 setIcon 方法
zinoR1 2008-04-30
  • 打赏
  • 举报
回复
用一个lable
对他setIcon()那就可以了
Inhibitory 2008-04-25
  • 打赏
  • 举报
回复
哥们挺强的
如果我想引用别的文件夹下的图片 URL该怎么写?
ImageIcon icon = new ImageIcon(this.getClass().getResource("D:\xxx\fefe\aaa.gif"));
好像这样就显示不了了

------------------------------------------------------------------
用JFileChooser来选择.
helin561 2008-04-25
  • 打赏
  • 举报
回复
哥们挺强的
如果我想引用别的文件夹下的图片 URL该怎么写?
ImageIcon icon = new ImageIcon(this.getClass().getResource("D:\xxx\fefe\aaa.gif"));
好像这样就显示不了了
craky 2008-01-25
  • 打赏
  • 举报
回复
包你满意~~~~~~~~~~~~


package craky;

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JPanel;

/**
* 可设置背景图片的JPanel,提供了三种显示背景图片的方式:居中、平铺和拉伸。
* 未设置背景图片的情况下,同JPanel。
*
* @author 003
*/
public class JImagePane extends JPanel
{
private static final long serialVersionUID = -8251916094895167058L;

/**
* 居中
*/
public static final String CENTRE = "Centre";

/**
* 平铺
*/
public static final String TILED = "Tiled";

/**
* 拉伸
*/
public static final String SCALED = "Scaled";

/**
* 背景图片
*/
private Image backgroundImage;

/**
* 背景图片显示模式
*/
private String imageDisplayMode;

/**
* 背景图片显示模式索引(引入此属性有助于必要时扩展)
*/
private int modeIndex;

/**
* 构造一个没有背景图片的JImagePane
*/
public JImagePane()
{
this(null, CENTRE);
}

/**
* 构造一个具有指定背景图片和指定显示模式的JImagePane
* @param image 背景图片
* @param modeName 背景图片显示模式
*/
public JImagePane(Image image, String modeName)
{
super();
setBackgroundImage(image);
setImageDisplayMode(modeName);
}

/**
* 设置背景图片
* @param image 背景图片
*/
public void setBackgroundImage(Image image)
{
this.backgroundImage = image;
this.repaint();
}

/**
* 获取背景图片
* @return 背景图片
*/
public Image getBackgroundImage()
{
return backgroundImage;
}

/**
* 设置背景图片显示模式
* @param modeName 模式名称,取值仅限于ImagePane.TILED ImagePane.SCALED ImagePane.CENTRE
*/
public void setImageDisplayMode(String modeName)
{
if(modeName != null)
{
modeName = modeName.trim();

//居中
if(modeName.equalsIgnoreCase(CENTRE))
{
this.imageDisplayMode = CENTRE;
modeIndex = 0;
}
//平铺
else if(modeName.equalsIgnoreCase(TILED))
{
this.imageDisplayMode = TILED;
modeIndex = 1;
}
//拉伸
else if(modeName.equalsIgnoreCase(SCALED))
{
this.imageDisplayMode = SCALED;
modeIndex = 2;
}

this.repaint();
}
}

/**
* 获取背景图片显示模式
* @return 显示模式
*/
public String getImageDisplayMode()
{
return imageDisplayMode;
}

/**
* 绘制组件
* @see javax.swing.JComponent#paintComponent(Graphics)
*/
@Override
protected void paintComponent(Graphics g)
{
super.paintComponent(g);

//如果设置了背景图片则显示
if(backgroundImage != null)
{
int width = this.getWidth();
int height = this.getHeight();
int imageWidth = backgroundImage.getWidth(this);
int imageHeight = backgroundImage.getHeight(this);

switch(modeIndex)
{
//居中
case 0:
{
int x = (width - imageWidth) / 2;
int y = (height - imageHeight) / 2;
g.drawImage(backgroundImage, x, y, this);
break;
}
//平铺
case 1:
{
for(int ix = 0; ix < width; ix += imageWidth)
{
for(int iy = 0; iy < height; iy += imageHeight)
{
g.drawImage(backgroundImage, ix, iy, this);
}
}

break;
}
//拉伸
case 2:
{
g.drawImage(backgroundImage, 0, 0, width, height, this);
break;
}
}
}
}
}
dzy_pb 2008-01-25
  • 打赏
  • 举报
回复
用label 的 setIcon,但是有的图片好大,不知道怎么控制一下!
fdsfafdasf 2008-01-25
  • 打赏
  • 举报
回复
用lable

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧