我已经创建了一个带有一些图像的 jar 文件,但是如果我将 jar 的位置(即 c 更改为 D 目录),则不会显示图像。如果我将图像保留在 jar 文件存在的位置,则 jar 文件将显示图像,但如果我更改 jar 文件的位置而不是图像的位置,则 jar 文件不会显示图像。请帮我。
为了加载图像,代码执行以下操作:
Icon ic=new ImageIcon("Label.jpg");
I have created a jar file with some Images, but if I change the location of the jar viz., c to D directory, images are not showing. The jar file is showing Images if I keep Images where jar file exist but if I change the location of the jar file other than location of the Images, jar file not showing the Images. Please help me.
为了加载图像,代码执行以下操作:
Icon ic=new ImageIcon("Label.jpg");
代码是:
import javax.swing.*;
import javax.imageio.*;
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.JLabel;
public class sk extends JFrame
{
BufferedImage img;
JFrame f;
JPanel p,p1;
JLabel l1,l2,l3,l4,l5,l6;
File fl;
JLabel l=new JLabel("Process Status:");
JButton b=new JButton("Browse");
JButton b1=new JButton("Submit");
public sk()
{
BufferedImage img;
f=new JFrame("Akritiv.1.0");
Container fc=f.getContentPane();
BufferedImage img= null;
try {
Icon ic=new ImageIcon(ImageIO.read(getClass().getResource("/Label.jpg")));
Icon ic1=new ImageIcon(ImageIO.read(getClass().getResource("/fig.jpg")));
Icon ic2=new ImageIcon(ImageIO.read(getClass().getResource("/na.jpg")));
Icon ic3=new ImageIcon(ImageIO.read(getClass().getResource("/logo1.jpg")));
Icon ic4=new ImageIcon(ImageIO.read(getClass().getResource("/status1.jpg")));
Icon ic5=new ImageIcon(ImageIO.read(getClass().getResource("/ver.jpg")));
}catch (IOException e) {}
//Icon ic=new ImageIcon("Label.jpg");
//Icon ic1=new ImageIcon("fig.jpg");
//Icon ic2=new ImageIcon("na.jpg");
//Icon ic3=new ImageIcon("logo1.jpg");
//Icon ic4=new ImageIcon("status1.jpg");
//Icon ic5=new ImageIcon("ver.jpg");
p=new JPanel();
p1=new JPanel();
l1=new JLabel(ic);
l2=new JLabel(ic1);
l3=new JLabel(ic2);
l4=new JLabel(ic3);
l5=new JLabel(ic4);
l6=new JLabel(ic5);
//l5.setBounds(5,200,30,10);
b.setBounds(50,100,150,100);
p.add(l1);
p1.add(b);
p1.add(b1);
b.addActionListener(new AL());
b1.addActionListener(new BL());
fc.setLayout(new FlowLayout(FlowLayout.LEFT));
l3.setBounds(10,300,200,300);
l4.setBounds(180,250,70,50);
ll.setBounds(250,400,500,150);
fc.add(p);
fc.add(l6);
fc.add(p1);
//fc.add(b1);
fc.add(l);
fc.add(l2);
fc.add(l3);
fc.add(l4);
fc.add(l5);
fc.add(ll);
f.pack();
fc.setBackground(Color.blue);
f.setSize(813,455);
f.setVisible(true);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public class AL implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
JFileChooser fc=new JFileChooser();
fc.setFont(new Font("Comic Sans MS",Font.PLAIN,9));
int choice=fc.showOpenDialog(f);
if(choice==JFileChooser.APPROVE_OPTION)
{
try{
String filename=fc.getSelectedFile().getAbsolutePath();
fl=new File(filename);
}
catch(Exception e){}
}
}
}
public class BL implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==b1)
{
l.setText("Please wait...");
try
{
new shyam(fl);
l.setText("completed");
}
catch(Exception e){}
}
}
}
public static void main(String arg[])
{
sk ob=new sk();
}
}