请求高手相助:
  我编写了2个类,Dialog类是实现图片切割界面的,ImageTest类是实现图片切割的,但是我想通过界面类Dialog输入被切割图片的地址sourcepath和切割完图片后图片的存放位置destipath1,destipath2,destipath3。然后实现切割,我先将Dialog类编译后和ImageTest类放在同一个文件夹里,可是在编译时却出现了:
javax.imageio.IIOException:Can't read input file!
            at javax.imageio.ImageIO.read(ImageIO.java:1263)
            at Test1.main(Test1.java:19)
以下是程序代码:import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;public class Dialog1 extends JFrame
{
  
  private GridBagLayout gbl=new GridBagLayout();
  private GridBagConstraints gbc=new GridBagConstraints();
  private Button button1 = new Button("源路径");
  private Button button2 = new Button("保存位置1");
  private Button button3 = new Button("保存位置2");
  private Button button4 = new Button("保存位置3");
  private Button button5 = new Button("确定");
  private Button button6 = new Button("取消");  private Label l=new Label("试卷切割器");
  private Label label1=new Label(" ");
  private Label label2=new Label(" ");
  private Label label3=new Label(" ");
  private Label label4=new Label(" ");
  private Label label5=new Label(" ");
 
  private TextField t1=new TextField(20);
  private TextField t2=new TextField(20);
  private TextField t3=new TextField(20);
  private TextField t4=new TextField(20);  private FileDialog fd1=new FileDialog(this,"打开文件",FileDialog.LOAD);
  private FileDialog fd2=new FileDialog(this,"打开文件",FileDialog.SAVE);
  private FileDialog fd3=new FileDialog(this,"打开文件",FileDialog.SAVE);
  private FileDialog fd4=new FileDialog(this,"打开文件",FileDialog.SAVE);  private JPanel p;
  
  private String fileName1=new String();
  private String fileName2=new String();
  private String fileName3=new String();
  private String fileName4=new String();
  
  public Dialog1() {                            
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    
    addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
        System.exit(0);
     }
  });
    
    p=(JPanel)this.getContentPane();
    p.setLayout(gbl);
    p.setBackground(Color.pink);
    this.setSize(new Dimension(300, 320));
    this.setTitle("试卷切割器");
    button1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button1_actionPerformed(e);
      }
    });
  
    button2.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button2_actionPerformed(e);
      }
    });    button3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button3_actionPerformed(e);
      }
    });   button4.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button4_actionPerformed(e);
      }
    });   button5.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button5_actionPerformed(e);
      }
    });
 
    button6.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(ActionEvent e) {
        button6_actionPerformed(e);
      }
    });    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(l,gbc);
    p.add(l);    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label1,gbc);
    p.add(label1);
  
    p.add(button1);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t1,gbc);
    p.add(t1);
    
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label2,gbc);
    p.add(label2);    p.add(button2);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t2,gbc);
    p.add(t2);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label3,gbc);
    p.add(label3);    p.add(button3);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t3,gbc);
    p.add(t3);    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label4,gbc);
    p.add(label4);
  
    p.add(button4);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t4,gbc);
    p.add(t4);
  
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label5,gbc);
    p.add(label5);    p.add(button5);
    p.add(button6); }
  
  void button1_actionPerformed(ActionEvent e) {
   fd1.show();
   fileName1=fd1.getDirectory()+fd1.getFile();
   fd1.dispose();
   t1.setText(fileName1);
   
}  void button2_actionPerformed(ActionEvent e) {
   fd2.show();
   fileName2=fd2.getDirectory()+fd2.getFile();
   fd2.dispose();
   t2.setText(fileName2);
  } void button3_actionPerformed(ActionEvent e) {
   fd3.show();
   fileName3=fd3.getDirectory()+fd3.getFile();
   fd3.dispose();
   t3.setText(fileName3);
  } void button4_actionPerformed(ActionEvent e) {
   fd4.show();
   fileName4=fd4.getDirectory()+fd4.getFile();
   fd4.dispose();
   t4.setText(fileName4);
  }  void button5_actionPerformed(ActionEvent e) {
   System.exit(0); 
  }  void button6_actionPerformed(ActionEvent e) {
   System.exit(0);
  }
  
  public String sourcepath(){
  return this.fileName1;
}  public String destipath1(){
  return this.fileName2;
}  public String destipath2(){
  return this.fileName3;
}  public String destipath3(){
  return this.fileName4;
}
  
}import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.imageio.ImageIO;public class Test1
{        public static void main(String[] args)
{
try {      
                           Dialog1 f=new Dialog1();
                           f.show();
                         
BufferedImage image = ImageIO.read(new File(f.sourcepath()));
saveSubImage(image, new Rectangle(10, 10, 100, 100), new File(f.destipath1()));
saveSubImage(image, new Rectangle(150, 150, 200, 300), new File(f.destipath2()));
saveSubImage(image, new Rectangle(20, 50, 70, 30), new File(f.destipath3()));
   } catch (IOException e) {
e.printStackTrace();
return;
}
} private static void saveSubImage(BufferedImage image, 
Rectangle subImageBounds, File subImageFile) throws IOException {
if (subImageBounds.x <= 0 || subImageBounds.y <= 0 ||
subImageBounds.x + subImageBounds.width >= image.getWidth() ||
subImageBounds.y + subImageBounds.height >= image.getHeight()) {
System.out.println("Bad subimage bounds");
return;
}

BufferedImage subImage = image.getSubimage(
subImageBounds.x, subImageBounds.y, 
subImageBounds.width, subImageBounds.height);
String fileName = subImageFile.getName();
String formatName = fileName.substring(fileName.lastIndexOf('.') + 1); ImageIO.write(subImag
e, formatName, subImageFile);
}
}

解决方案 »

  1.   

    这些位置你放错了:
    BufferedImage image = ImageIO.read(new File(f.sourcepath()));
    saveSubImage(image, new Rectangle(10, 10, 100, 100), new File(f.destipath1()));
    saveSubImage(image, new Rectangle(150, 150, 200, 300), new File(f.destipath2()));
    saveSubImage(image, new Rectangle(20, 50, 70, 30), new File(f.destipath3()));因为你的sourcepath,destipath1/2/3是在filechooser选中后得到的,在main里面什么都没有啊
    当然出错了
      

  2.   

    谢谢xuzhike的提醒
    可是,难道我要讲按扭事件监听和接受都放进后面那个类么?那样不是就要将2个类合并成一个了么?
    你能不能将修改的思路给我呢?我才刚学JAVA,你只给我一个问题的思路,我还是很难调的。
      

  3.   

    注意代码中你定义的对像实例,我加了两个if,就好了:public static void main(String[] args) {
        try {      Dialog1 f = new Dialog1();
          f.show();
          
          if(f!=null){//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            File file = new File(f.sourcepath());
            BufferedImage image = null;
            if (file.exists()) ){//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            {
              image = ImageIO.read(file);          saveSubImage(image, new Rectangle(10, 10, 100, 100),
                           new File(f.destipath1()));
              saveSubImage(image, new Rectangle(150, 150, 200, 300),
                           new File(f.destipath2()));
              saveSubImage(image, new Rectangle(20, 50, 70, 30),
                           new File(f.destipath3()));
            }
          }
        }
        catch (Exception e) {
          e.printStackTrace();
          return;
        }
      }
    ...................
      

  4.   

    zhenai(liu)
              谢谢你的指点,我觉得挺有道理的。但是,我调试了一下,还是不行,没有办法切割,路径还是没有传递过去。而且,你有没有注意到,我有2个按纽,确定按纽button5,取消按纽button6。我的2个按纽的事件处理都是一样的,因为我不知道该怎么响应确定这个事件
     void button5_actionPerformed(ActionEvent e) {
       System.exit(0); 
      }  void button6_actionPerformed(ActionEvent e) {
       System.exit(0);
      }
      

  5.   

    我用System.out.println在if内部输入数据测试了一下,觉得好象根本没有执行到 if (file.exists())内部 ,也就是说跳过去,直接执行后面的了。if(f!=null){//\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            File file = new File(f.sourcepath());
            BufferedImage image = null;
            if (file.exists()) //\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
            {
              image = ImageIO.read(file);          saveSubImage(image, new Rectangle(10, 10, 100, 100),
                           new File(f.destipath1()));
              saveSubImage(image, new Rectangle(150, 150, 200, 300),
                           new File(f.destipath2()));
              saveSubImage(image, new Rectangle(20, 50, 70, 30),
                           new File(f.destipath3()));
            }
          }
        }
      

  6.   

    import java.awt.Rectangle;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.imageio.ImageIO;public class Dialog1 extends JFrame
    {private GridBagLayout gbl=new GridBagLayout();
    private GridBagConstraints gbc=new GridBagConstraints();
    private Button button1 = new Button("源路径");
    private Button button2 = new Button("保存位置1");
    private Button button3 = new Button("保存位置2");
    private Button button4 = new Button("保存位置3");
    private Button button5 = new Button("确定");
    private Button button6 = new Button("取消");private Label l=new Label("试卷切割器");
    private Label label1=new Label(" ");
    private Label label2=new Label(" ");
    private Label label3=new Label(" ");
    private Label label4=new Label(" ");
    private Label label5=new Label(" ");private TextField t1=new TextField(20);
    private TextField t2=new TextField(20);
    private TextField t3=new TextField(20);
    private TextField t4=new TextField(20);private FileDialog fd1=new FileDialog(this,"打开文件",FileDialog.LOAD);
    private FileDialog fd2=new FileDialog(this,"打开文件",FileDialog.SAVE);
    private FileDialog fd3=new FileDialog(this,"打开文件",FileDialog.SAVE);
    private FileDialog fd4=new FileDialog(this,"打开文件",FileDialog.SAVE);private JPanel p;private String fileName1=new String();
    private String fileName2=new String();
    private String fileName3=new String();
    private String fileName4=new String();public Dialog1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent e){
    System.exit(0);
    }
    });p=(JPanel)this.getContentPane();
    p.setLayout(gbl);
    p.setBackground(Color.pink);
    this.setSize(new Dimension(300, 320));
    this.setTitle("试卷切割器");
    button1.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button1_actionPerformed(e);
    }
    });button2.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button2_actionPerformed(e);
    }
    });button3.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button3_actionPerformed(e);
    }
    });button4.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button4_actionPerformed(e);
    }
    });button5.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button5_actionPerformed(e);
    }
    });button6.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(ActionEvent e) {
    button6_actionPerformed(e);
    }
    });gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(l,gbc);
    p.add(l);gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label1,gbc);
    p.add(label1);p.add(button1);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t1,gbc);
    p.add(t1);gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label2,gbc);
    p.add(label2);p.add(button2);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t2,gbc);
    p.add(t2);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label3,gbc);
    p.add(label3);p.add(button3);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t3,gbc);
    p.add(t3);gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label4,gbc);
    p.add(label4);p.add(button4);
    gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(t4,gbc);
    p.add(t4);gbc.gridwidth=GridBagConstraints.REMAINDER;
    gbl.setConstraints(label5,gbc);
    p.add(label5);p.add(button5);
    p.add(button6);}void button1_actionPerformed(ActionEvent e) {
    fd1.show();
    fileName1=fd1.getDirectory()+fd1.getFile();
    fd1.dispose();
    t1.setText(fileName1);}void button2_actionPerformed(ActionEvent e) {
    fd2.show();
    fileName2=fd2.getDirectory()+fd2.getFile();
    fd2.dispose();
    t2.setText(fileName2);
    }void button3_actionPerformed(ActionEvent e) {
    fd3.show();
    fileName3=fd3.getDirectory()+fd3.getFile();
    fd3.dispose();
    t3.setText(fileName3);
    }void button4_actionPerformed(ActionEvent e) {
    fd4.show();
    fileName4=fd4.getDirectory()+fd4.getFile();
    fd4.dispose();
    t4.setText(fileName4);
    }void button5_actionPerformed(ActionEvent e) {
    try {
    BufferedImage image = ImageIO.read(new File(sourcepath()));
    saveSubImage(image, new Rectangle(10, 10, 100, 100), new File(destipath1()));
    saveSubImage(image, new Rectangle(150, 150, 200, 300), new File(destipath2()));
    saveSubImage(image, new Rectangle(20, 50, 70, 30), new File(destipath3()));
    } catch (IOException ex) {
    ex.printStackTrace();
    return;
    }
    System.exit(0);
    }void button6_actionPerformed(ActionEvent e) {
    System.exit(0);
    }private static void saveSubImage(BufferedImage image,
    Rectangle subImageBounds, File subImageFile) throws IOException {
    if (subImageBounds.x <= 0 || subImageBounds.y <= 0 ||
    subImageBounds.x + subImageBounds.width >= image.getWidth() ||
    subImageBounds.y + subImageBounds.height >= image.getHeight()) {
    System.out.println("Bad subimage bounds");
    return;
    }BufferedImage subImage = image.getSubimage(
    subImageBounds.x, subImageBounds.y,
    subImageBounds.width, subImageBounds.height);
    String fileName = subImageFile.getName();
    String formatName = fileName.substring(fileName.lastIndexOf('.') + 1);ImageIO.write(subImage, formatName, subImageFile);
    }public String sourcepath(){
    return this.fileName1;
    }public String destipath1(){
    return this.fileName2;
    }public String destipath2(){
    return this.fileName3;
    }public String destipath3(){
    return this.fileName4;
    }public static void main(String[] args)
    {
    Dialog1 f=new Dialog1();
    f.show();
    }}