如何设置图像的相对路径?
public class Currency2 extends JFrame implements ActionListener{
private JButton USA_btn, INDIA_btn, DUBAI_btn, AUSTRALIA_btn, ITALY_btn, MALAYSIA_btn, SINGAPORE_btn, ENGLAND_btn, SAUDI_btn, QATAR_btn;
public Currency2 cr2;
ImagePanel panel_secondframe;
Border thickBorder;
public Currency2(){
super("Currency Converter");
this.InitializeComponents();
}
@Override
public void actionPerformed(ActionEvent e) {
String Object = e.getActionCommand();
if(Object.equals("USA")){
Currency_USA cus = new Currency_USA(this);
cus.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("INDIA")){
Currency_INDIA cin = new Currency_INDIA(this);
cin.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("DUBAI")){
Currency_DUBAI cdu = new Currency_DUBAI(this);
cdu.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("ITALY")){
Currency_ITALY cit = new Currency_ITALY(this);
cit.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("ENGLAND")){
Currency_ENGLAND cei = new Currency_ENGLAND(this);
cei.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("AUSTRALIA")){
Currency_AUSTRALIA cau = new Currency_AUSTRALIA(this);
cau.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("MALAYSIA")){
Currency_MALAYSIA cma = new Currency_MALAYSIA(this);
cma.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("SINGAPORE")){
Currency_SINGAPORE csi = new Currency_SINGAPORE(this);
csi.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("SAUDI")){
Currency_SAUDI cssi = new Currency_SAUDI(this);
cssi.setVisible(true);
this.setVisible(false);
}
else if(Object.equals("QATAR")){
Currency_QATAR cqa = new Currency_QATAR(this);
cqa.setVisible(true);
this.setVisible(false);
}
}
private void InitializeComponents() {
this.panel_secondframe = new ImagePanel(new ImageIcon("E:\\programing \\JAVA\\new netbean pro\\currency\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());
this.getContentPane().add(panel_secondframe);
this.pack();
this.thickBorder = new LineBorder(Color.black);
this.USA_btn = new JButton("USA");
this.USA_btn.setBounds(136, 35, 89, 23);
this.USA_btn.addActionListener(this);
this.USA_btn.setBorder(thickBorder);
this.panel_secondframe.add(USA_btn);
this.INDIA_btn = new JButton("INDIA");
this.INDIA_btn.setBounds(136, 65, 89, 23);
this.INDIA_btn.addActionListener(this);
this.INDIA_btn.setBorder(thickBorder);
this.panel_secondframe.add(INDIA_btn);
this.DUBAI_btn = new JButton("DUBAI");
this.DUBAI_btn.setBounds(136, 95, 89, 23);
this.DUBAI_btn.addActionListener(this);
this.DUBAI_btn.setBorder(thickBorder);
this.panel_secondframe.add(DUBAI_btn);
this.ITALY_btn = new JButton("ITALY");
this.ITALY_btn.setBounds(136, 125, 89, 23);
this.ITALY_btn.addActionListener(this);
this.ITALY_btn.setBorder(thickBorder);
this.panel_secondframe.add(ITALY_btn);
this.SAUDI_btn = new JButton("SAUDI");
this.SAUDI_btn.setBounds(136, 155, 89, 23);
this.SAUDI_btn.addActionListener(this);
this.SAUDI_btn.setBorder(thickBorder);
this.panel_secondframe.add(SAUDI_btn);
this.QATAR_btn = new JButton("QATAR");
this.QATAR_btn.setBounds(136, 185, 89, 23);
this.QATAR_btn.addActionListener(this);
this.QATAR_btn.setBorder(thickBorder);
this.panel_secondframe.add(QATAR_btn);
this.ENGLAND_btn = new JButton("ENGLAND");
this.ENGLAND_btn.setBounds(129, 215, 102, 23);
this.ENGLAND_btn.addActionListener(this);
this.ENGLAND_btn.setBorder(thickBorder);
this.panel_secondframe.add(ENGLAND_btn);
this.AUSTRALIA_btn = new JButton("AUSTRALIA");
this.AUSTRALIA_btn.setBounds(129, 245, 102, 23);
this.AUSTRALIA_btn.addActionListener(this);
this.AUSTRALIA_btn.setBorder(thickBorder);
this.panel_secondframe.add(AUSTRALIA_btn);
this.MALAYSIA_btn = new JButton("MALAYSIA");
this.MALAYSIA_btn.setBounds(129, 275, 102, 23);
this.MALAYSIA_btn.addActionListener(this);
this.MALAYSIA_btn.setBorder(thickBorder);
this.panel_secondframe.add(MALAYSIA_btn);
this.SINGAPORE_btn = new JButton("SINGAPORE");
this.SINGAPORE_btn.setBounds(129, 305, 102, 23);
this.SINGAPORE_btn.addActionListener(this);
this.SINGAPORE_btn.setBorder(thickBorder);
this.panel_secondframe.add(SINGAPORE_btn);
this.add(this.panel_secondframe);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
答案 0 :(得分:2)
设置相对路径替换为
this.panel_secondframe = new ImagePanel(new ImageIcon(".\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());
答案 1 :(得分:1)
Java类Class
有一个static
方法getResourceAsStream(path)
,它可以从应用程序本身的包结构中加载公共资源。这在您的情况下非常有用且常见。
假设您的图片位于com.yourpackage.icons
内(所以com/yourpackage/icons
为您的真实路径,那么您可以使用
ImageIcon icon = new ImageIcon(ImageIO.read(this.getClass().getResourceAsStream("/com/yourpackage/icons/imagename.png")));