Filename=txt_attach.getText();
LoadSickLeaveImage sickLeave=new LoadSickLeaveImage();
sickLeave.ImageIcon(Filename);
我希望将图片的路径从txt_attach
传递到另一个类到ImageIcon
方法,但没有任何显示。没有错误或没有错误。
public class LoadSickLeaveImage {
public ImageIcon ImageIcon(String Filename){
ImageIcon icon = new ImageIcon(Filename);
return icon;
}
public static void main(String[] args) throws IOException {
LoadSickLeaveImage icon = new LoadSickLeaveImage();
ImageIcon image = icon.ImageIcon("");
//Check if file exists or not.
File file = new File(image.toString());
boolean exists = file.exists();
if (!exists) {
JOptionPane.showMessageDialog(null, "the file or directory you are searching does not exist");
}else{
JLabel label = new JLabel(); //Make new label where image will be on printed.
label.setIcon(image);
JFrame gallery = new JFrame();
gallery.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gallery.getContentPane().add(label);
gallery.pack();
gallery.setLocation(500,450);
gallery.setVisible(true);
}
}
}
答案 0 :(得分:0)
不需要main方法,只需在构造函数中提供所有内容 二等。代码==>
import java.io. ; import javax.swing。;公共课 LoadSickLeaveImage扩展了治疗{
public LoadSickLeaveImage(String fileName){ ImageIcon image = new ImageIcon(fileName); File file = new File(image.toString()); boolean exists = file.exists(); if (!exists) { JOptionPane.showMessageDialog(null, "the file or directory you are searching does not exist"); }else{ JLabel label = new JLabel(); label.setIcon(image); JFrame gallery = new JFrame(); gallery.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); gallery.getContentPane().add(label); gallery.pack(); gallery.setLocation(500,450); gallery.setVisible(true); } } }