0

我是java初学者,请告诉我以下代码有什么问题..不要删除目录或文件。我正在为删除目录制作 GUI 应用程序。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;

class DeleteFile extends JFrame implements ActionListener {

    JFileChooser fc;
    JButton b, btndel;
    JLabel l;
    JTextField tf;

    DeleteFile() {
        super("Delete Directory");
        fc = new JFileChooser("G:\\Java");
        l = new JLabel("File");
        l.setBounds(25, 22, 35, 35);
        add(l);
        tf = new JTextField();
        tf.setBounds(60, 25, 170, 25);
        add(tf);
        b = new JButton("Browse");
        b.setBounds(250, 25, 80, 25);
        add(b);
        b.addActionListener(this);
        btndel = new JButton("Delete");
        btndel.setBounds(250, 100, 80, 25);
        add(btndel);
        btndel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == btndel) {
                    String text = tf.getText();
                    System.out.println(text);
                    File del = new File("text");
                    delete(del);
                }
            }
        });
        setLayout(null);
        setSize(400, 200);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b) {
            int x = fc.showOpenDialog(null);
            if (x == JFileChooser.APPROVE_OPTION) {
                File f1 = fc.getSelectedFile();
                String s1 = fc.getName(f1);
                tf.setText(s1);
                File f2 = fc.getCurrentDirectory();
                String s2 = fc.getName(f2);
            }
        }
    }

    public void delete(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                delete(files[i]);
            }
            file.delete();
            System.out.println("yes");
        } else {
            file.delete();
            System.out.println("no");
        }
    }

    public static void main(String... s) {
        new DeleteFile();
    }
}
4

4 回答 4

0

这个说法

File del = new File("text");

只是创建对 a 的引用,该引用File不是在您的JFileChooser. 而是使用所选的类成员变量引用File

currentFile = fc.getSelectedFile();

并用它来删除。在尝试删除之前,您需要检查变量是否已分配

if (currentFile != null) {
    delete(currentFile);
}
于 2013-09-26T15:10:56.187 回答
0

试试这个:改变这一行: File del = new File("text"); 到:文件删除=新文件(文本);

于 2013-09-26T15:18:31.830 回答
0

您需要进行两项更改:

  1. 删除以下文本周围的双引号:

     File del = new File("text"); -> File del = new File(text);
    
  2. 在 actionPerformed(ActionEvent) 方法中将 tf.setText(s1)更改为tf.setText(f1.getAbsolutePath()) 。

于 2013-09-26T15:32:48.593 回答
0
what you code,make it neat :)

public class DeleteFile extends JFrame implements ActionListener {

        private static final String FOLDER = "E:\\Java";
        JFileChooser myFileChooser;
        JButton myButtonBrowse, myButtonDelete;
        JLabel myLabel;
        JTextField myTextField;

        DeleteFile() {
            super("Delete Directory");
            myFileChooser = new JFileChooser(FOLDER);
            myLabel = new JLabel("File");
            myLabel.setBounds(25, 22, 35, 35);
            add(myLabel);
            myTextField = new JTextField(FOLDER);
            myTextField.setBounds(60, 25, 170, 25);
            add(myTextField);
            myButtonBrowse = new JButton("Browse");
            myButtonBrowse.setBounds(250, 25, 80, 25);
            add(myButtonBrowse);
            myButtonBrowse.addActionListener(this);
            myButtonDelete = new JButton("Delete");
            myButtonDelete.setBounds(250, 100, 80, 25);
            add(myButtonDelete);
            myButtonDelete.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    if (e.getSource() == myButtonDelete) {
                        String text = myTextField.getText();
                        File deleting = new File(text);
                        if (deleting.isDirectory()) {
                            System.out.println(text+" gonna delete");
                            delete(deleting);
                        }else{
                            System.out.println("sorry, "+text+"  doesnt exists");
                        }
                    }
                }
            });
            setLayout(null);
            setSize(400, 200);
            setVisible(true);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
        }

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == myButtonBrowse) {
                int x = myFileChooser.showOpenDialog(null);
                if (x == JFileChooser.APPROVE_OPTION) {
                    File f1 = myFileChooser.getSelectedFile();
                    // f1.delete();

                    String s1 = myFileChooser.getName(f1);
                    myTextField.setText(s1);
                    File f2 = myFileChooser.getCurrentDirectory();
                    String s2 = myFileChooser.getName(f2);
                }
            }
        }

        public void delete(File file) {
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (int i = 0; i < files.length; i++) {
                    delete(files[i]);
                }
                file.delete();
                System.out.println("folder deleted :" + file.getName());
            } else {
                file.delete();
                System.out.println("file deleted:" + file.getName());
            }
        }

        public static void main(String... s) {
            new DeleteFile();
        }
    }
于 2013-09-26T16:04:03.233 回答