我想把filename里面的地址转化成URL的类型
这是我的部分代码,不知道错在那里了
或者还有什么别的方法可以实现吗?谢了先!
FileDialog op = new FileDialog(this,"插入",FileDialog.LOAD);
op.setVisible(true);
String filename = op.getDirectory()+op.getFile();//要转化的字符串
System.out.println("filename="+filename); //输出结果为E:\My Pictures\11.jpg
URL url=null;
try{
url = new URL("file",null,filename);// String转化成URL
}
catch(Exception e){
System.out.println("e="+e);
}
System.out.println("url="+url); //输出结果是file:E:\My Pictures\11.jpg
ImageIcon bicon = new ImageIcon(url); //到这句就出错了
DrawObject drawObject = new DrawIcon(bicon, 100, 100,1);
这是我的部分代码,不知道错在那里了
或者还有什么别的方法可以实现吗?谢了先!
FileDialog op = new FileDialog(this,"插入",FileDialog.LOAD);
op.setVisible(true);
String filename = op.getDirectory()+op.getFile();//要转化的字符串
System.out.println("filename="+filename); //输出结果为E:\My Pictures\11.jpg
URL url=null;
try{
url = new URL("file",null,filename);// String转化成URL
}
catch(Exception e){
System.out.println("e="+e);
}
System.out.println("url="+url); //输出结果是file:E:\My Pictures\11.jpg
ImageIcon bicon = new ImageIcon(url); //到这句就出错了
DrawObject drawObject = new DrawIcon(bicon, 100, 100,1);
我试过了还是不行呀
File f=new File("E:\\My Pictures\\11.jpg")这句可以执行,
输出的地址为f=E:\My Pictures\11.jpg
但是URL url = f.toURL();就出现下面的错误了
怎么回事呢Exception in thread "AWT-EventQueue-5" java.security.AccessControlException: access denied (java.io.FilePermission E:\My Pictures\11.jpg read)
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkRead(Unknown Source)
at java.io.File.isDirectory(Unknown Source)
at java.io.File.toURL(Unknown Source)
at draw.PrintDoc.backDocument(PrintDoc.java:548)
at draw.Draw$MainFrame.commandBack(Draw.java:645)
at draw.Draw$MainFrame$5.actionPerformed(Draw.java:196)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
ImageIcon icon = new ImageIcon(YourApplet.class.getResource("iconfilename.jpg"));
打包的时候把图标文件和YourApplet类放在一起。