Java 如何设置图像的相对路径?

Java 如何设置图像的相对路径?,java,swing,embedded-resource,Java,Swing,Embedded Resource,如何设置图像的相对路径 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; Im

如何设置图像的相对路径

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

要将相对路径替换为

this.panel_secondframe = new ImagePanel(new ImageIcon(".\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());

要将相对路径替换为

this.panel_secondframe = new ImagePanel(new ImageIcon(".\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());

要将相对路径替换为

this.panel_secondframe = new ImagePanel(new ImageIcon(".\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());

要将相对路径替换为

this.panel_secondframe = new ImagePanel(new ImageIcon(".\\src\\newpackage\\image of currency converter\\secondframe.png").getImage());

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

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

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

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


请解释你尝试了什么,并在问题中详细说明你到底想做什么,而不是标题。为了让你朝着正确的方向迈出一步,你需要将图像打包到你的JAR中(如果你正在构建一个JAR)或者将其放在类路径上,并使用资源路径而不是文件系统路径。不过,我不完全确定这将如何与Swing
ImageIcon
配合使用。为变量指定与类相同的名称,例如
Object
,是一种不好的做法,这将在维护代码时造成很大的困难(对于试图分析堆栈溢出的读者来说也是困难的)。始终以小写字母开头变量名,除非它们是静态的最终字段。下面的标记中有示例。请解释您尝试了什么,并在问题中详细说明您到底想做什么,而不是标题。为了让您朝着正确的方向迈出一步,您需要将图像打包到您的JAR中(如果您正在建造一个)或者将其放在类路径上,并使用资源路径而不是文件系统路径。不过,我不完全确定这将如何与Swing
ImageIcon
配合使用。为变量指定与类相同的名称,例如
Object
,是一种不好的做法,这将在维护代码时造成很大的困难(对于试图分析堆栈溢出的读者来说也是困难的)。始终以小写字母开头变量名,除非它们是静态的最终字段。下面的标记中有示例。请解释您尝试了什么,并在问题中详细说明您到底想做什么,而不是标题。为了让您朝着正确的方向迈出一步,您需要将图像打包到您的JAR中(如果您正在建造一个)或者将其放在类路径上,并使用资源路径而不是文件系统路径。不过,我不完全确定这将如何与Swing
ImageIcon
配合使用。为变量指定与类相同的名称,例如
Object
,是一种不好的做法,这将在维护代码时造成很大的困难(对于试图分析堆栈溢出的读者来说也是困难的)。始终以小写字母开头变量名,除非它们是静态的最终字段。下面的标记中有示例。请解释您尝试了什么,并在问题中详细说明您到底想做什么,而不是标题。为了让您朝着正确的方向迈出一步,您需要将图像打包到您的JAR中(如果您正在建造一个)或者将其放在类路径上,并使用资源路径而不是文件系统路径。不过,我不完全确定这将如何与Swing
ImageIcon
配合使用。为变量指定与类相同的名称,例如
Object
,是一种不好的做法,这将在维护代码时造成很大的困难(对于试图分析堆栈溢出的读者来说也是困难的)。始终以小写字母开头变量名,除非它们是静态的最终字段。getResourceAsStream下的标记中有示例。此外,实际上不需要ImageIO,因为有一个ImageIcon构造函数接受URL。ImageIO在无法读取图像文件时会抛出错误。ImageIcon构造函数r不会抛出错误。getResourceAsStream不是静态方法。此外,实际上不需要ImageIO,因为存在一个接受URL的ImageIcon构造函数。如果ImageIcon构造函数无法读取图像文件,则会抛出错误。ImageIcon构造函数不会抛出错误。getResourceAsStream不是静态方法。此外,ImageIO也不是真正需要的d、 因为存在一个接受URL的ImageIcon构造函数。ImageIO在无法读取图像文件时会抛出错误。ImageIcon构造函数不会抛出错误。getResourceAsStream不是静态方法。此外,ImageIO也不是真正需要的,因为存在一个接受URL的ImageIcon构造函数。ImageIO在无法读取时会抛出错误d图像文件。ImageIcon构造函数没有