How to run the Jar well?

来源:互联网 发布:linux查找大文件命令 编辑:程序博客网 时间:2024/05/16 01:27

This problem has puzzled me for nearly three days...

As I don't know even this kind of thing would happen at the first place...

About how to display the picture, if i write the related codes like this:

aquariumImage = Toolkit.getDefaultToolkit().getImage("bubbles.gif");

it can be run well in the eclipse but in the jar.

so I change it into this by adding URL :

URL address = getClass().getResource("bubbles.gif");
  aquariumImage = Toolkit.getDefaultToolkit().getImage(address);

it then works perfectly.

 

 

import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.URL;


public class Test extends Frame
{

    
private static final long serialVersionUID = 1L;

    
static Image aquariumImage;

    
public Test()
    
{
        addWindowListener(
new WindowAdapter()
        
{
            
public void windowClosing(WindowEvent e)
            
{
                dispose();
                System.exit(
0);
            }

        }
);
    }


    
public static void main(String[] args)
    
{
        Test test 
= new Test();
        test.go();
        test.setSize(
600435);
        test.setVisible(
true);

        Graphics g 
= test.getGraphics();
        
while (!g.drawImage(aquariumImage, 00, test));
    }


    
public void go()
    
{
        URL address 
= getClass().getResource("bubbles.gif"); 
        aquariumImage 
= Toolkit.getDefaultToolkit().getImage(address);
    }

}

 

 

 

原创粉丝点击