Adding image in JApplet

限于喜欢 提交于 2019-12-13 21:17:35

问题


ImageIcon icon= new ImageIcon("a.gif");
JLabel jLabel1=new JLabel(icon);
jLabel1.setVisible(true);
card1.add(jLabel1);

I am a newbie to Java and I am facing a problem to add image in a panel in applet. My image is in the same folder. My applet is visible without any problem but only image is not displayed.


回答1:


public void init() 
    URL imageURL = new URL(getDocumentBase(), "a.gif");
    Image image = getImage(imageURL);
    ImageIcon icon = new ImageIcon(image);
    // ...

The ImageIcon constructor that accepts a String presumes the string represents the path & file name of a File.

Only trusted applets can access a File, and then only on the client file-system (not the server). If this is an application resource, it should be on the server, and can be accessed by URL.

Note that the ImageIcon constructor will also accept an URL, rather than the Image used above. I just wanted to highlight that applets have an inbuilt method to obtain images.



来源:https://stackoverflow.com/questions/14309301/get-applet-to-load-pictures-when-user-clicks-on-level-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!