下面是我做的一个软件:
项目源代码见附件
这里面就有"复制图片到剪切板"功能.核心方法如下:
/***
* 复制图片到剪切板
* @param image
*/
public static void setclipboardimage(jframe frame, final image image) {
transferable trans = new transferable() {
@override
public object gettransferdata(dataflavor flavor)
throws unsupportedflavorexception, ioexception {
if (isdataflavorsupported(flavor)) {
return image;
}
throw new unsupportedflavorexception(flavor);
}
public dataflavor[] gettransferdataflavors() {
return new dataflavor[] { dataflavor.imageflavor };
public boolean isdataflavorsupported(dataflavor flavor) {
return dataflavor.imageflavor.equals(flavor);
};
frame.gettoolkit().getsystemclipboard().setcontents(trans, null);
}
在jframe中如何设置图标
方式一:
url url=this.getclass().getresource("/com/kingbase/img/kingbase_log.png");
// system.out.println(url.getpath());
imageicon icon = new imageicon(url);
frame.seticonimage(icon.getimage());
方式二:
inputstream is = this.getclass().getresourceasstream(
"/com/kingbase/img/kingbase_log.png");
bufferedinputstream isr = new bufferedinputstream(is);
byte[] b = null;
try
{
b = new byte[isr.available()];
isr.read(b);
system.out.println(b.length);
}
catch (ioexception e2)
e2.printstacktrace();
if (b != null)
imageicon imageicon = new imageicon(b);
frame.seticonimage(imageicon.getimage());
方式三:
inputstream is = this.getclass().getclassloader().getresourceasstream(
"com/kingbase/img/kingbase_log.png");前面没有斜杠
}