Loading Image in Java Applet

后端 未结 4 586
伪装坚强ぢ
伪装坚强ぢ 2021-01-21 21:18

When I try to run an applet in applet viewer it is not able to find resources (Image). I try to load resource like this:

String cb= this.getCodeBase().toString()         


        
4条回答
  •  萌比男神i
    2021-01-21 22:05

    Is your applet supposed to load images after it is loaded? Or would you be better served bundling necessary image resources in the jar with your applet?

    I work daily on an applet-based application with plenty of graphics in the GUI. They are bundled in the jar-file. This si what we do:

    // 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)); 
    

    Make sure your image is actuallly in the jar where you think it is. Use:

    jar -tf

    ... to get a listing.

提交回复
热议问题