将图片的网址从班级传递到另一个

时间:2014-03-08 11:45:46

标签: netbeans-7

你好,伙计们。我想对所有在这个问候网站上帮助我们的人表示感谢。当用户点击按钮时,我正面临将图像路径从一个框架传递到另一个类的问题..调用是:

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

1 个答案:

答案 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);

}

} }