dadi723 2017-01-18 14:09 采纳率: 0%
浏览 1326
已采纳

一个Java小程序,复制文件的,有界面,为何运行没反应?没报错

程序分为界面和功能两个类,代码较长,我做了必要注释,请耐心看完,请大神指出运行没反应的原因(尽量不要写新代码给我,在原代码上修正,便于我理解)
实在没积分了,无法悬赏
界面部分

 /*
 * 界面
 * @version  1.0
 * 主要功能: 将查找目录内(含子目录)的指定后缀名文件复制到输出目录
 */
package check;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class appearance {
    static File OldPath=null;                              //定义文件对象
    static File NewPath=null;
    static String LastName=null;                          //定义后缀名对象
    static int x=0;                                      //用于对话框判断
    public static void main(String[] args){
    Frame f=new Frame("文件查找");
    Panel p1=new Panel();
    Panel p2=new Panel();
    Panel p3=new Panel();
    Label la1=new Label("查找目录:");
    Label la2=new Label("输出目录:");
    Label la3=new Label("后缀名:");
    TextField t1=new TextField(50);
    TextField t2=new TextField(50);
    TextField t3=new TextField(20);
    Button b1=new Button("浏览");
    Button b2=new Button("浏览");
    Button b3=new Button("确定");
    JFileChooser choose1=new JFileChooser();
    JFileChooser choose2=new JFileChooser();
    choose1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    choose2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    t1.setEditable(false);
    t2.setEditable(false);
    f.setBounds(200,150,520,200);
    p1.setLayout(new FlowLayout());
    p2.setLayout(new FlowLayout());
    p3.setLayout(new FlowLayout());
    f.setLayout(new BorderLayout());
    f.addWindowListener(new WindowAdapter(){
        public void windowClosing(WindowEvent e){
            f.setVisible(false);
            System.exit(0);
        }
    });
    b1.addActionListener(new ActionListener(){                                         //按钮监听(文件选择器)
        public void actionPerformed(ActionEvent arg0) {
            choose1.showDialog(new JLabel(),"选择文件夹");
            OldPath=choose1.getSelectedFile();
            t1.setText(OldPath.getAbsolutePath());
        }
    });
    b2.addActionListener(new ActionListener(){                                           //按钮监听(文件选择器)
        public void actionPerformed(ActionEvent arg){
            choose2.showDialog(new JLabel(),"选择文件夹");
            NewPath=choose2.getSelectedFile();
            t2.setText(NewPath.getAbsolutePath());
        }
    });
    b3.addActionListener(new ActionListener(){                                            //按钮监听(调用功能模块)
        public void actionPerformed(ActionEvent arg1){
            LastName=t3.getText();
            if(t1.getText().trim().length()<1||t2.getText().trim().length()<1||t3.getText().trim().length()<1){//对话框处理
                x=JOptionPane.showConfirmDialog(f,"请输入完整","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.ERROR_MESSAGE);
                if(x==JOptionPane.OK_OPTION)
                System.exit(0);
            }
            Function.cheek(OldPath, NewPath);                             //调用功能方法,传递文件对象参数
        }
    });
    f.add(p1,"North");                                                    //添加组件
    f.add(p2,"Center");
    f.add(p3,"South");
    p1.add(la1);
    p1.add(t1);
    p1.add(b1);
    p2.add(la2);
    p2.add(t2);
    p2.add(b2);
    p2.add(la3);
    p2.add(t3);
    p3.add(b3);
    f.setVisible(true);
}
}

功能部分

 /*
 * 功能
 * @version 1.0
 */
package check;
import java.io.*;
class Function {
    static BufferedInputStream bis=null;             //文件输入输出流,用于复制文件
    static BufferedOutputStream bos=null;
    public static void cheek(File OldPath,File NewPath){        //功能方法 接受界面传值
        if(!NewPath.exists()){
            NewPath.mkdir();
        }
        ergodic(OldPath,NewPath);     //递归,扫描子目录内所有文件
    }

    private static void ergodic(File oldPath, File newPath) {
        File[] files=oldPath.listFiles();
        for(File f:files){
            if(f.isDirectory()){
                ergodic(f,newPath);
            } if(f.getName().endsWith(appearance.LastName)){
                copy(f,newPath);       //文件复制,
            }
        }
    }

    private static void copy(File f, File newPath) {      //文件复制
        String Name=newPath.getName();
                File newfile=new File(f,Name);
                try{
                    bis=new BufferedInputStream(new FileInputStream(f));
                     bos=new BufferedOutputStream(new FileOutputStream(newfile));
                     byte[] by=new byte[1024];
                        int len=0;
                        while((len=bis.read(by))!=-1){
                            bos.write(by,0,len);
                        }
                        bos.close();
                        bis.close();
                    }catch(IOException e){

                    }
    }
}


  • 写回答

4条回答 默认 最新

  • miaoch 2017-01-19 01:27
    关注

    为何你匿名类(就是监听事件那些)中的变量都不是final的 你也不报错。。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • 海之沐 2017-01-18 15:10
    关注
     /*
     * 界面
     * @version  1.0
     * 主要功能: 将查找目录内(含子目录)的指定后缀名文件复制到输出目录
     */
    package check;
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class appearance {
        static File OldPath=null;                              //定义文件对象
        static File NewPath=null;
        static String LastName=null;                          //定义后缀名对象
        static int x=0;                                      //用于对话框判断
        public static void main(String[] args){
            final Frame f=new Frame("文件查找");
            Panel p1=new Panel();
            Panel p2=new Panel();
            Panel p3=new Panel();
            Label la1=new Label("查找目录:");
            Label la2=new Label("输出目录:");
            Label la3=new Label("后缀名:");
            final TextField t1=new TextField(50);
            final TextField t2=new TextField(50);
            final TextField t3=new TextField(20);
            Button b1=new Button("浏览");
            Button b2=new Button("浏览");
            Button b3=new Button("确定");
            final JFileChooser choose1=new JFileChooser();
            final JFileChooser choose2=new JFileChooser();
            choose1.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            choose2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            t1.setEditable(false);
            t2.setEditable(false);
            f.setBounds(200,150,520,200);
            p1.setLayout(new FlowLayout());
            p2.setLayout(new FlowLayout());
            p3.setLayout(new FlowLayout());
            f.setLayout(new BorderLayout());
            f.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e){
                    f.setVisible(false);
                    System.exit(0);
                }
            });
            b1.addActionListener(new ActionListener(){                                         //按钮监听(文件选择器)
                public void actionPerformed(ActionEvent arg0) {
                    choose1.showDialog(new JLabel(),"选择文件夹");
                    OldPath=choose1.getSelectedFile();
                    t1.setText(OldPath.getAbsolutePath());
                }
            });
            b2.addActionListener(new ActionListener(){                                           //按钮监听(文件选择器)
                public void actionPerformed(ActionEvent arg){
                    choose2.showDialog(new JLabel(),"选择文件夹");
                    NewPath=choose2.getSelectedFile();
                    t2.setText(NewPath.getAbsolutePath());
                }
            });
            b3.addActionListener(new ActionListener(){                                            //按钮监听(调用功能模块)
                public void actionPerformed(ActionEvent arg1){
                    LastName=t3.getText();
                    if(t1.getText().trim().length()<1||t2.getText().trim().length()<1||t3.getText().trim().length()<1){//对话框处理
                        x=JOptionPane.showConfirmDialog(f,"请输入完整","提示",JOptionPane.OK_CANCEL_OPTION,JOptionPane.ERROR_MESSAGE);
                        if(x==JOptionPane.OK_OPTION)
                            System.exit(0);
                    }
                    Function.cheek(OldPath, NewPath);                             //调用功能方法,传递文件对象参数
                }
            });
            f.add(p1,"North");                                                    //添加组件
            f.add(p2,"Center");
            f.add(p3,"South");
            p1.add(la1);
            p1.add(t1);
            p1.add(b1);
            p2.add(la2);
            p2.add(t2);
            p2.add(b2);
            p2.add(la3);
            p2.add(t3);
            p3.add(b3);
            f.setVisible(true);
        }
    }
    
     /*
     * 功能
     * @version 1.0
     */
    package check;
    import java.io.*;
    public class Function {
        static BufferedInputStream bis=null;             //文件输入输出流,用于复制文件
        static BufferedOutputStream bos=null;
        public static void cheek(File OldPath,File NewPath){        //功能方法 接受界面传值
            if(!NewPath.exists()){
                NewPath.mkdir();
            }
            ergodic(OldPath,NewPath);     //递归,扫描子目录内所有文件
        }
    
        private static void ergodic(File oldPath, File newPath) {
            File[] files=oldPath.listFiles();
            for(File f:files){
                if(f.isDirectory()){
                    ergodic(f,newPath);
                } if(f.getName().endsWith(appearance.LastName)){
                    copy(f,newPath);       //文件复制,
                }
            }
        }
    
        private static void copy(File f, File newPath) {      //文件复制
            String Name=f.getName();
            File newfile=new File(newPath,Name);
            try{
                bis=new BufferedInputStream(new FileInputStream(f));
                bos=new BufferedOutputStream(new FileOutputStream(newfile));
                byte[] by=new byte[1024];
                int len=0;
                while((len=bis.read(by))!=-1){
                    bos.write(by,0,len);
                }
                bos.close();
                bis.close();
            }catch(IOException e){
                System.out.println(e.getMessage());
            }
        }
    }
    
    评论
  • 安果移不动 2017-01-18 17:21
    关注

    这个gui程序 并不好用 有时候有bug 所以不建议使用~~~~用sout就可以啦

    评论
  • gbguanbo 2017-01-19 01:23
    关注

    没仔细看你代码,简单看了一下,你界面程序和拷贝程序在同一进程中,当拷贝文件是界面进程被挂起,所以界面不会有任何响应,等文件拷贝完,并等待拷贝经常被挂起(至于何时挂起鬼才知道,有时候就一直不会被挂起)界面才有响应,所以建议采用2个进程,或者3个,一般的GUI编程都是这样的,业务处理放到另一个进程中,保证页面在业务处理过程中同样可以响应用户操作。

    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 基于python进行多背包问题的多值编码
  • ¥15 相同型号电脑与配置,发现主板有一台貌似缺少了好多元器件似的,会影响稳定性和使用寿命吗?
  • ¥15 要求编写稀疏矩阵A的转置矩阵的算法
  • ¥15 编写满足以下要求的停车场管理程序,设停车场只有一个可停放n辆车的狭窄通道且只有一个大门可供车辆进出。
  • ¥15 C语言:数据子序列基础版
  • ¥20 powerbulider 导入excel文件,显示不完整
  • ¥15 用keil调试程序保证结果进行led相关闪烁
  • ¥15 paddle训练自己的数据loss降不下去
  • ¥20 用matlab的pdetool解决以下三个问题
  • ¥15 单个福来轮的平衡与侧向滑动是如何做到的?