我创建了一个包含一些图像的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();
}
}
答案 0 :(得分:3)
问题在于如何加载图像......
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);
ImageIcon(String)
假定String
参数是文件名。在您的示例中,它等同于new ImageIcon("./Label.jpg")
,即请从当前名为Label.jpg
的执行位置加载图像。
嵌入式资源不是文件,不能这样处理。
您应该使用
Icon ic=new ImageIcon(getClass().getResource("/Label.jpg"));
事实上,您应该避免使用ImageIcon
,而是直接使用ImageIO
API。
Icon ic=new ImageIcon(ImageIO.read(getClass().getResource("/Label.jpg")));
ImageIO
如果无法找到/加载命名资源,至少会抛出异常。