JOptionpane在JDialog中实现,调用一次,但反复运行。为什么呢?

时间:2016-02-24 12:49:09

标签: java joptionpane jdialog

源文件夹中有2个文件是JOptionPane的4倍。

以前,源文件夹中有5个文件,消息出现了7次。 它基于出现次数而不依赖于复制的文件数。

来电者代码:

JButton btnCopyAllFiles = new JButton("Copy All Files");
btnCopyAllFiles.addActionListener(new ActionListener() 
{
    public void actionPerformed(ActionEvent arg0) 
    {
        final JDialog dialog = new JDialog(WebAdmin.this, "Copy files", true);
        dialog.setSize(500, 80);
        JPanel panel = new JPanel();

        GridBagLayout gbl_panel = new GridBagLayout();
        GridBagConstraints gBC = new GridBagConstraints();
        //gBC.fill = GridBagConstraints.HORIZONTAL;
        panel.setLayout(gbl_panel);

        final JTextField txtTargetDir = new JTextField(30);
        txtTargetDir.setFont(new Font("Dialog", Font.BOLD, 12));
        gBC.insets = gridBagInsets;
        gBC.fill = GridBagConstraints.BOTH;
        gBC.gridx = 1;
        gBC.gridy = 0;
        panel.add(txtTargetDir, gBC);

        JButton btnBrowseTarget = new JButton("Browse");
        btnBrowseTarget.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) 
            {
                JFileChooser chooser = new JFileChooser();
                chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.setAcceptAllFileFilterUsed(false);
                chooser.showOpenDialog(WebAdmin.this);
                File ff = chooser.getCurrentDirectory();
                File fl = chooser.getSelectedFile();
                String filename_last = fl.getName();
                String filename_first = ff.getAbsolutePath();

                txtTargetDir.setText(filename_first + "/" + filename_last + "/");
            }
        });         
        gBC.insets = gridBagInsets;
        gBC.fill = GridBagConstraints.BOTH;
        gBC.gridx = 2;
        gBC.gridy = 0;
        gBC.gridwidth = 2;
        panel.add(btnBrowseTarget, gBC);

        JButton btnOK = new JButton("OK");
        btnOK.setToolTipText("All files copied to the specified folder");
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    File source = new File(Functions.readStoreFile());
                    File target = new File(txtTargetDir.getText());
                    Functions.copyDirectory(source, target);
                    dialog.dispose();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        gBC.insets = gridBagInsets;
        gBC.fill = GridBagConstraints.BOTH;
        gBC.gridx = 3;
        gBC.gridy = 1;
        gBC.gridwidth = 1;
        panel.add(btnOK, gBC);

        dialog.add(panel);
        dialog.pack();
        dialog.setVisible(true);
    }
});

方法调用地点:

JButton btnOK = new JButton("OK");
        btnOK.setToolTipText("All files copied to the specified folder");
        btnOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                try {
                    File source = new File(Functions.readStoreFile());
                    File target = new File(txtTargetDir.getText());
                    Functions.copyDirectory(source, target);
                    dialog.dispose();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

叫方法:

public static void copyDirectory(File sourceLocation , File targetLocation) {
    boolean isOK = true;
    if (sourceLocation.isDirectory()) {
        if (!targetLocation.exists()) {
            targetLocation.mkdir();
        }

        String[] children = sourceLocation.list();
        for (int i=0; i<children.length; i++) {
            copyDirectory(new File(sourceLocation, children[i]),
                new File(targetLocation, children[i]));
        }
    } 
    else {
        try {
            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (Exception e) {
            isOK = false;
            e.printStackTrace();
        }
    }
    if (isOK){
        JOptionPane.showMessageDialog(null, "Files copied success!", "Copy finished!", JOptionPane.INFORMATION_MESSAGE);
    }
}

2 个答案:

答案 0 :(得分:0)

您的代码将为每个复制失败的文件生成一个JOptionPane。

您可能正在尝试复制特殊目录&#34; ..&#34;。 打印文件列表以证明这一点。

检查堆栈跟踪以获取更多详细信息。

答案 1 :(得分:0)

我设法解决了!每个副本都是成功的,但因为该方法本身递归调用了JOptionPane所以文件数量出现了+1次。

btnOK.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
        try {
            ...
            if ( Functions.copyDirectory(source, target) ){
                JOptionPane.showMessageDialog(null, "Files copied success!", "Copy finished!", JOptionPane.INFORMATION_MESSAGE);
            }
            dialog.dispose();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
});

public static boolean copyDirectory(File sourceLocation , File targetLocation) {
    boolean isOK = true;
    try{
            ...
            ...
            in.close();
            out.close();
            isOK = true;
        } catch (Exception e) {
            isOK = false;
            e.printStackTrace();
        }
    }
    return isOK;
}