gpt4 book ai didi

java - 如何设置图片的相对路径?

转载 作者:行者123 更新时间:2023-12-01 22:16:53 25 4
gpt4 key购买 nike

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

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

关于java - 如何设置图片的相对路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30808828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com