当我尝试在applet浏览器中运行applet时,无法找到资源(图像)。我尝试像这样加载资源:
String cb= this.getCodeBase().toString(); String imgPath = cb+"com/blah/Images/a.png"; System.out.println("imgPath:"+imgPath); java.net.URL imgURL = Applet.class.getResource(path);
但是当我在appet查看器中运行它时,路径是这样的:imgPath:file:D:/Work/app/build/classes/com/blah/Images/a.png
虽然此路径中有图像,但前缀文件是:导致问题,我该如何测试此代码?
当部署在服务器中并且代码库返回服务器URL时,此代码是否有效?
您的applet是否应该在加载后加载图像?还是将您的applet捆绑在罐子中的必要图像资源更好?
我每天都在基于applet的应用程序上工作,该应用程序在GUI中具有大量图形。它们捆绑在jar文件中。这是我们的工作:
// get the class of an object instance - any object. // We just defined an empty one, and did everything as static. class EmptyClass{} Class loadClass = new EmptyClass().getClass(); // load the image and put it directly into an ImageIcon if it suits you ImageIcon ii = new ImageIcon(loadClass.getResource("/com/blah/Images/a.png")); // and add the ImageIcon to your JComponent or JPanel in a JLabel aComponent.add(new JLabel(ii));
确保您的图像确实在您认为它所在的罐子中。用:
jar -tf <archive_file_name>
…以获得清单。