为什么Event Dispatch Thread无法创建新的EDT?

时间:2014-05-16 21:02:40

标签: java swing freeze event-dispatch-thread

当我创建" cnc control"程序,我想创建文件浏览器来加载或保存钻井计划。所以我制作了简单的文件浏览器并对其进行了测试[工作正常],但在主类中,当使用ActionListener构建新的EDT时,UI / JFrame冻结,主线程继续运行。

现在我知道如何通过在主线程中使用变量来解决这个障碍并用它们执行浏览器。

简而言之:EDT是正确的吗?为什么它不能像任何其他线程那样创建另一个事件调度线程?

编辑:我不知道每个java应用程序只能有一个EDT,并且冻结是由其内部的无限循环引起的。感谢 MadProgrammer 清理。

抱歉下面的那堆文字。 (我的课很难分开)

这是一个例子,如果我的英语太荒谬可怜/我让你感到困惑:

工作主要课程:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
//functions:
static void LoadSaveWhatever(){
    FileBrowser fb=new FileBrowser();                                   //create browser class  success!
    fb.createWindow(FileBrowser.SAVE,"c:\\",new String[]{".txt"});  //create browser...but it's never completed!?
    while(fb.Done()==false&fb.w!=null){                                 // wait 'till fb kills itself
        System.out.println("waitting for address.. PLUS still runing!");
        if(fb.w.isVisible()==false){
            break;
        }try {
            Thread.sleep(1000);             //because of spamming and less power consuming
        } catch (InterruptedException e) {
            e.printStackTrace();            // never happened but it's good to be there
        }
    }System.out.println("target: "+fb.GetTarget()); //print target file
}

static boolean saveOrLoad=false;

public static void main(String[] args) {
    //set things up
    JFrame w=new JFrame();                                  //main window
    JButton b=new JButton("Open (quite old/bad/laggy) browser");//the button
    boolean Alive=true;
    long t=0;               //auto close

    //EDT?
    b.addActionListener(new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            saveOrLoad=true;
        }
    });

    //construct
    w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       //making sure javaw.exe dies after w is closed
    w.setVisible(true);                                     //...
    w.getContentPane().add(b,BorderLayout.CENTER);          //put button in JFrame
    w.pack();

    //main loop
    t=System.currentTimeMillis();
    while(Alive){
        if(System.currentTimeMillis()-t>(3*60*1000)){       //close program after 3 min
            Alive=false;
        }if(saveOrLoad){
            LoadSaveWhatever();
            saveOrLoad=false;
        }try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
}`

冻结主要课程

        import java.awt.BorderLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;

    import javax.swing.JButton;
    import javax.swing.JFrame;

    public class Main2 {
        //functions:
            static void LoadSaveWhatever(){
                FileBrowser fb=new FileBrowser();                                   //create browser class  success!
                fb.createWindow(FileBrowser.SAVE,"c:\\",new String[]{".txt"});  //create browser...but it's never completed!?
                while(fb.Done()==false&fb.w!=null){                                 // wait 'till fb kills itself
                    System.out.println("waitting for address.. PLUS still runing!");
                    if(fb.w.isVisible()==false){
                        break;
                    }try {
                        Thread.sleep(1000);             //because of spamming and less power consuming
                    } catch (InterruptedException e) {
                        e.printStackTrace();            // never happened but it's good to be there
                    }
                }System.out.println("target: "+fb.GetTarget()); //print target file
            }

            public static void main(String[] args) {
                //set things up
                JFrame w=new JFrame();                                  //main window
                JButton b=new JButton("Open (quite old/bad/laggy) browser");//the button
                boolean Alive=true;
                long t=0;               //auto close

                //EDT?
                b.addActionListener(new ActionListener(){
                    @Override
                    public void actionPerformed(ActionEvent arg0) {
                        LoadSaveWhatever();
                    }
                });

                //construct
                w.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       //making sure javaw.exe dies after w is closed
                w.setVisible(true);                                     //...
                w.getContentPane().add(b,BorderLayout.CENTER);          //put button in JFrame
                w.pack();

                //main loop
                t=System.currentTimeMillis();
                while(Alive){
                    if(System.currentTimeMillis()-t>(10*1000)){     //close program after 3 min [NOT WORK!!!!! need to use kill switch]
                        Alive=false;
                    }try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
    }

FileBrowser类:

    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.io.File;
    import java.io.IOException;

    import javax.swing.JButton;
    import javax.swing.JComboBox;
    import javax.swing.JFrame;
    import javax.swing.JList;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTextArea;
    import javax.swing.JTextField;
    import javax.swing.border.TitledBorder;
    import javax.swing.event.ListSelectionEvent;
    import javax.swing.event.ListSelectionListener;

    import data.DataConverter;
    import file.Browser;

    public class FileBrowser{
        public volatile JFrame w;
        public Browser brow=new Browser();

        private JButton Back;
        private JButton Goto;
        private JButton Save;
        private JButton Cancel;

        private JTextField newadd;
        private JTextArea FolderInfo;
        private JTextArea FileInfo;
        private JTextField name;
        private JPanel gotoBar;
        private JPanel top;
        private JPanel mainp;
        private JPanel cenl;
        private JPanel cenr;
        private JPanel downrigth;
        private JPanel downcen;
        private JPanel downleft;
        private JPanel center;
        private JPanel down;
        private JPanel file;
        private JPanel folder;
        private JPanel filei;
        private JPanel folderi;

        private JComboBox<String> HDp;
        private JComboBox<String> Format;
        private JList<String> filep;
        private JList<String> folderp;

        private JScrollPane main;
        private JScrollPane filePane;
        private JScrollPane folderPane;

        private long lastClick=0;
        public static int BROWSER=0;
        public static int SAVE=1;
        public static int LOAD=2;

        private volatile String Target="";
    private volatile boolean TSelected=false;

    public synchronized boolean Done(){
        return TSelected;
    }
    public synchronized String GetTarget(){
        return Target;
    }



    public synchronized void updateHDs(){
        if(brow!=null&HDp!=null){
            String[] s=brow.getHardDrivesString();
            if(s!=null){
                HDp.removeAllItems();
                for(int a=0;a<s.length;a++){
                    HDp.addItem(s[a]);
                }
            }
        }
    }
    public synchronized void updateFiles(){
        if(brow!=null&HDp!=null){
            if(filep!=null){
                String[] s=brow.getFilesString(null);
                if(s!=null){
                    filep.setListData(s);
                }else{
                    filep.setListData(new String[1]);
                }
            }
        }
    }
    public synchronized void updateFolders(){
        if(brow!=null&HDp!=null){
            if(folderp!=null){
                String[] s=brow.getFoldersString(null);
                if(s!=null){
                    folderp.setListData(s);
                }else{
                    folderp.setListData(new String[1]);
                }
            }
        }
    }
    public synchronized void updateTop(){
        if(newadd!=null&Goto!=null){
            if(newadd.getText()!=brow.getCurrentPath()+File.separator){
                newadd.setText(brow.getCurrentPath()+File.separator);
            }
        }
    }
    public synchronized void setFormats(String[] s){
        if(s!=null&Format!=null){
            Format.removeAllItems();
            for(int a=0;a<s.length;a++){
                Format.addItem(s[a]);
            }
        }
    }
    public synchronized void Update(){
        updateFolders();
        updateFiles();
        updateTop();
        w.pack();
    }
    public synchronized void Destroy(){
        if(w!=null){
            w.dispose();
            w=null;
        }TSelected=true;
    }

    public synchronized void createWindow(int windowType,String startAdd,String[] Formats){
        try{
            w.removeAll();
            w.setVisible(false);
        }catch(NullPointerException e){
        }if(brow==null){
            brow=new Browser();
        }brow.setAddress(startAdd);
        w=null;w=new JFrame();w.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        mainp=null;
        down=null;
        HDp=null;
        filep=null;
        folderp=null;
        main=null;

        folderi=new JPanel();
        filei=new JPanel();
        mainp=new JPanel();
        top=new JPanel();
        gotoBar=new JPanel();
        down=new JPanel();
        file=new JPanel();
        folder=new JPanel();
        cenl=new JPanel();
        center=new JPanel();
        cenr=new JPanel();
        downrigth=new JPanel();
        downcen=new JPanel();
        downleft=new JPanel();

        Save=new JButton();
        Cancel=new JButton();
        Back=new JButton("Folder up");
        Goto=new JButton("Go to");


        HDp=new JComboBox<String>();
        Format=new JComboBox<String>();

        filep=new JList<String>();
        folderp=new JList<String>();



        FolderInfo=new JTextArea();
        FileInfo=new JTextArea();

        name=new JTextField();
        newadd=new JTextField();

        updateHDs();

        top.setLayout(new BorderLayout());
        HDp.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent ae) {
                if(brow==null){
                    brow=new Browser();
                }brow.setAddress(""+HDp.getSelectedItem());
                Update();
            }
        });
        folderp.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(FolderInfo!=null){
                    String[] s=brow.getInfoString(brow.getCurrentPath()+File.separator+folderp.getSelectedValue());
                    FolderInfo.setText(DataConverter.StringsToString(s));w.pack();
                }
            }
        });
        folderp.addMouseListener(new MouseListener(){
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1){
                    if(lastClick+700>=System.currentTimeMillis()){
                        if(folderp.getSelectedIndex()>=0){
                            if(brow.type==-1){
                                brow.setAddress(folderp.getSelectedValue());
                            }else{
                                brow.setAddress(brow.getCurrentPath()+File.separator+folderp.getSelectedValue()+File.separator);
                            }Update();
                        }
                    }else{
                        lastClick=System.currentTimeMillis();
                    }
                }else{
                    if(e.getButton()==3){
                        if(brow.getCurrentPath()!=null){
                            brow.FolderUp();
                            Update();
                        }
                    }
                }

            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
        });
        filep.addListSelectionListener(new ListSelectionListener(){
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if(FileInfo!=null){
                    String[] s=brow.getInfoString(brow.getCurrentPath()+File.separator+filep.getSelectedValue());
                    FileInfo.setText(DataConverter.StringsToString(s));w.pack();
                }
            }
        });
        filep.addMouseListener(new MouseListener(){
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getButton()==1){
                    if(lastClick+700>=System.currentTimeMillis()){
                        if(filep.getSelectedIndex()>=0){
                            try {
                                Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+brow.getCurrentPath()+File.separator+filep.getSelectedValue());
                            } catch (IOException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }else{
                        lastClick=System.currentTimeMillis();
                    }
                }else{
                    if(e.getButton()==3){
                        if(brow.getCurrentPath()!=null){
                            brow.FolderUp();
                            Update();
                        }
                    }
                }

            }
            @Override
            public void mouseEntered(MouseEvent e) {
            }
            @Override
            public void mouseExited(MouseEvent e) {
            }
            @Override
            public void mousePressed(MouseEvent e) {
            }
            @Override
            public void mouseReleased(MouseEvent e) {
            }
        });
        Back.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(brow.getCurrentPath()!=null){
                    brow.FolderUp();
                    Update();
                }
            }
        });
        Goto.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                if(brow!=null){
                    File tf=new File(newadd.getText());
                    if(tf.exists()){
                        if(tf.isDirectory()){
                            brow.setAddress(newadd.getText());
                            Update();
                        }else{
                            brow.setAddress(tf.getParent());
                            Update();
                        }
                    }
                }
            }
        });
        Cancel.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent arg0) {
                Destroy();
            }
        });

        gotoBar.setLayout(new BorderLayout());
        newadd.setPreferredSize(new Dimension(650,20));
        gotoBar.add(newadd,BorderLayout.WEST);
        gotoBar.add(Goto,BorderLayout.CENTER);
        gotoBar.add(Back,BorderLayout.EAST);
        top.add(gotoBar);

        filePane=new JScrollPane(filep);
        folderPane=new JScrollPane(folderp);

        folder.setLayout(new BorderLayout());
        folder.setBorder(new TitledBorder("Folders:"));
        folder.add(folderPane);
        file.setLayout(new BorderLayout());
        file.setBorder(new TitledBorder("Files:"));
        file.add(filePane);

        cenl.setLayout(new GridLayout(2,1));
        cenl.setPreferredSize(new Dimension(420,500));
        cenl.add(folder);
        cenl.add(file);

        folderi.setBorder(new TitledBorder("Folder info:"));
        filei.setBorder(new TitledBorder("File info:"));

        FolderInfo.setEditable(false);
        FileInfo.setEditable(false);

        folderi.add(new JScrollPane(FolderInfo));
        filei.add(new JScrollPane(FileInfo));

        cenr.setLayout(new GridLayout(2,1));
        cenr.add(folderi);
        cenr.add(filei);

        center.setLayout(new BorderLayout());
        center.add(cenl,BorderLayout.CENTER);
        center.add(cenr,BorderLayout.LINE_END);

        downleft.setLayout(new BorderLayout());
        downleft.setBorder(new TitledBorder("Quick"));
        downleft.add(HDp);

        downcen.setLayout(new GridLayout(2,1));
        downcen.add(name);
        downcen.add(Format);

        downrigth.setLayout(new GridLayout(2,2));
        downrigth.add(Save);
        downrigth.add(Cancel);

        down.setLayout(new BorderLayout());
        down.add(downleft,BorderLayout.LINE_START);
        down.add(downcen,BorderLayout.CENTER);
        down.add(downrigth,BorderLayout.LINE_END);

        mainp.setLayout(new BorderLayout());
        mainp.add(top,BorderLayout.NORTH);
        mainp.add(center,BorderLayout.CENTER);
        mainp.add(down,BorderLayout.SOUTH);

        Update();
        main=new JScrollPane(mainp);

        switch(windowType){
        default:
            w.setTitle("File Browser module V2.0");
            Save.setText("[unavailable]");
            TSelected=true;
            Cancel.setText("Close");
            break;
        case 1:
            w.setTitle("Save [whatever] to..");
            Save.setText("Save");

            Save.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if(brow.type!=-1){
                        if(name.getText().length()>0){
                            Target=brow.getCurrentPath()+File.separator+name.getText()+Format.getSelectedItem();
                            TSelected=true;
                            Destroy();
                        }
                    }
                }
            });
            Cancel.setText("Cancel");
            break;
        case 2:
            w.setTitle("Load [whateverLongAsIAcceptIt] from...");
            Save.setText("Open");
            Save.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if(brow.type!=-1){
                        if(filep.getSelectedIndex()>=0){
                            Target=brow.getCurrentPath()+File.separator+filep.getSelectedValue();
                            TSelected=true;
                            Destroy();
                        }
                    }
                }
            });
            Cancel.setText("Cancel");
                break;
            }w.getContentPane().add(main);
            setFormats(Formats);
            w.pack();
            w.setVisible(true);
            w.setAutoRequestFocus(true);
        }



    }

DataConverter类

        public class DataConverter {
        public static int INT=1;
        public static int CHAR=2;
        public static int STRING=3;
        public static int IntList=4;
        public static int CharList=5;
        public static int StringList=6;

        public static String CharToString(char c){
            return (""+c);
        }
        public static String CharsToString(char[] c){
            String s="";
            if(c!=null){
                if(c.length>0){
                    for(int a=0;a<c.length;a++){
                        s=s+(""+c[a]);
                    }return s;
                }
            }return null;
        }
        public static int CharToInt(char c){
            return ((int)c);
        }
        public static int[] CharsToInts(char[] c){
            int[] i;
            if(c!=null){
                if(c.length>0){
                    i=new int[c.length];
                    for(int a=0;a<c.length;a++){
                        i[a]=((int)c[a]);
                    }return i;
                }
            }return null;
        }
        public static char StringToChar(String s){
            if(s!=null){
                if(s.length()>0){
                    return s.charAt(0);
                }
            }return 0;
        }
        public static char[] StringToChars(String s){
            if(s!=null){
                if(s.length()>0){
                    char[] c=new char[s.length()];
                    for(int a=0;a<c.length;a++){
                        c[a]=s.charAt(a);
                    }return c;
                }
            }return null;
        }
        public static int StringToInt(String s){
            if(s!=null){
                if(s.length()>0){
                    return ((int)s.charAt(0));
                }
            }
            return 0;
        }
        public static int[] StringToInts(String s){
            if(s!=null){
                if(s.length()>0){
                    int[] i=new int[s.length()];
                    for(int a=0;a<i.length;a++){
                        i[a]=((int)s.charAt(a));
                    }return i;
                }
            }return null;
        }
        public static char IntToChar(int i){
            return ((char)i);
        }
        public static char[] IntsToChars(int[] i){
            if(i!=null){
                if(i.length>0){
                    char[] c=new char[i.length];
                    for(int a=0;a<i.length;a++){
                        c[a]=((char)i[a]);
                    }return c;
                }
            }return null;
        }
        public static String IntToString(int i){
            return ""+((char)i);
        }
        public static String IntsToString(int[] i){
            if(i!=null){
                if(i.length>0){
                    String s="";
                    for(int a=0;a<i.length;a++){
                        s=s+""+((char)i[a]);
                    }return s;
                }
            }return null;
        }
        public static String StringsToString(String[] s){
            if(s!=null){
                if(s.length>0){
                    String S="";
                    for(int a=0;a<s.length;a++){
                        S=S+s[a]+System.lineSeparator();
                    }return S;
                }
            }return null;
        }
        public static String[] StringToStrings(String s){
            if(s!=null){
                int a=0;
                int c=0;
                for(int b=0;b<s.length();b++){
                    if(s.substring(b,b+1).equals(System.lineSeparator())){
                        a++;
                    }
                }String[] S=new String[1+a];
                for(int b=0;b<S.length;b++){
                    if(s.substring(b,b+1).equals(System.lineSeparator())){
                        S[c]=s.substring(a,b);
                        a=b;
                    }
                }
            }return null;
        }

        public static int GetDataTypeOf(Object clip){
            try{
                if(clip.getClass().getSimpleName().equals("int[]")){
                    return DataConverter.IntList;
                }if(clip.getClass().getSimpleName().equals("char[]")){
                    return DataConverter.CharList;
                }if(clip.getClass().getSimpleName().equals("String[]")){
                    return DataConverter.StringList;
                }if(clip instanceof Integer){
                    return DataConverter.INT;
                }if(clip instanceof Character){
                    return DataConverter.CHAR;
                }if(clip instanceof String){
                    return DataConverter.STRING;
                }
            }catch(NullPointerException e){
            }return 0;
        }

        public static Object ConvertTo(Object DataFrom,int To){
            try{
                int[] out = null;
                switch(GetDataTypeOf(DataFrom)){
                case 3:
                    out=StringToInts((String)DataFrom);
                    break;
                case 4:
                    out=(int[])DataFrom;
                    break;
                case 5:
                    out=CharsToInts((char[])DataFrom);
                    break;
                case 6:
                    out=StringToInts(StringsToString((String[])DataFrom));
                    break;
                }
                switch(To){
                case 3:
                    return IntsToString(out);
                case 4:
                    return out;
                case 5:
                    return IntsToChars(out);
                case 6:
                    return StringToStrings(IntsToString(out));
                }
            }catch(NullPointerException e){
            }return null;
        }

        public static int[] toEigthBit(int value){
            int[] o=new int[4];
            int v=value;
            o[0]=v/(255*255*255);   //255*255*255*255
            v=v-(o[0]*255*255*255);
            o[1]=v/(255*255);       //255*255*255
            v=v-(o[1]*255*255);
            o[2]=v/(255);           //255*255
            v=v-(o[2]*255);
            o[3]=v;
            return o;
        }
        public static int fromEigthBit(int[] value){
            long o=value[3];
            o=o+value[2]*255;
            o=o+value[1]*255*255;
            o=o+value[0]*255*255*255;
            return (int)o;
        }

    }

和上次需要的课程,浏览器:

        import java.io.File;
    import java.io.IOException;

    public class Browser {
        File f;
        File[] files;
        public int type=-1;         //-1=not exist, 0=Folder, 1=File
        String HDL="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        File[] HDs;

        public void updateHardDrives(){
            File t;
            int b=0;
            for(int a=0;a<HDL.length();a++){
                t=new File(HDL.substring(a,a+1)+":"+File.separator);
                if(t.exists()){
                    b++;
                }
            }if(b>0){
                HDs=new File[b];
                int c=0;
                for(int a=0;a<HDL.length();a++){
                    t=new File(HDL.substring(a,a+1)+":"+File.separator);
                    if(t.exists()){
                        HDs[c]=new File(HDL.substring(a,a+1)+":"+File.separator);
                        c++;
                    }
                }
            }
        }
        public boolean updateAddress(){
            if(f!=null){
                if(f.exists()){
                    if(f.canRead()){
                        if(f.isDirectory()){
                            files=f.listFiles();
                            type=0;
                        }else{
                            files=null;
                            type=1;
                        }return true;
                    }
                }type=-1;
            }return false;
        }

        public boolean setAddress(String add){
            if(add!=null){
                f=new File(add);
                if(f.exists()){
                    if(f.isFile()){
                        type=1;
                    }else{
                        type=0;
                    }return this.updateAddress();
                }return false;
            }else{
                type=-1;
                updateHardDrives();
                files=new File[HDs.length];
                for(int a=0;a<files.length;a++){
                    files[a]=new File(HDs[a].getAbsolutePath());
                }
            }return false;
        }
        public String getCurrentPath(){
            if(f!=null&type!=-1){
                return f.getAbsolutePath();
            }return null;
        }

        public String[] getInfoString(String add){
            String[] s=null;
            if(add!=null){
                File tf=new File(add);
                if(tf.exists()){
                    if(tf.isDirectory()){
                        s=new String[13];
                        s[0]=""+tf.getName();
                        s[1]="Type:\tFolder";
                        s[2]="Is readable:\t"+tf.canRead();
                        s[3]="Is writable:\t"+tf.canWrite();
                        s[4]="Is executable:\t"+tf.canExecute();
                        s[5]="Is hidden:\t"+tf.isHidden();
                        s[6]="Absolute path:\t"+tf.getAbsolutePath();
                        try {
                            s[7]="Canonical path:\t"+tf.getCanonicalPath();
                        } catch (IOException e) {
                            s[7]="Canonical path:\t"+"Error";
                        }s[8]="Folders size:\t"+tf.length()+" Byte(s)";
                        s[9]="Usable space:\t"+tf.getUsableSpace()+" Byte(s)";
                        s[10]="Free space:\t"+tf.getFreeSpace()+" Byte(s)";
                        s[11]="Total Space:\t"+tf.getTotalSpace()+" Byte(s)";
                        s[12]="Parent address:\t"+tf.getParent();
                    }if(tf.isFile()){
                        s=new String[13];
                        s[0]=""+tf.getName();
                        s[1]="Type:\tFile";
                        s[2]="Is readable:\t"+tf.canRead();
                        s[3]="Is writable:\t"+tf.canWrite();
                        s[4]="Is executable:\t"+tf.canExecute();
                        s[5]="Is hidden:\t"+tf.isHidden();
                        s[6]="Absolute path:\t"+tf.getAbsolutePath();
                        try {
                            s[7]="Canonical path:\t"+tf.getCanonicalPath();
                        } catch (IOException e) {
                            s[7]="Canonical path:\t"+"Error";
                        }s[8]="Files size:\t"+tf.length()+" Byte(s)";
                        s[9]="Usable space:\t"+tf.getUsableSpace()+" Byte(s)";
                        s[10]="Free space:\t"+tf.getFreeSpace()+" Byte(s)";
                        s[11]="Total Space:\t"+tf.getTotalSpace()+" Byte(s)";
                        s[12]="Parent address:\t"+tf.getParent();
                    }return s;
                }
            }else{System.out.println("failed: ");
                if(f!=null){
                    if(f.exists()){

                    }
                }
            }return s;
        }
        public String[] getNamesString(String add){
            String[] s=null;
            if(add!=null){
                File tf=new File(add);
                if(tf.isDirectory()){
                    if(2<tf.list().length){
                        s=tf.list();
                    }
                }
            }else{
                if(f!=null){
                    if(f.isDirectory()){
                        s=f.list();
                    }
                }
            }return s;
        }
        public String[] getFilesString(String add){
            String[] s=null;
            File[] fi=null;
            if(add!=null){
                File tf=new File(add);
                if(tf.isDirectory()){
                    fi=tf.listFiles();
                    if(fi!=null){
                        int b=0;
                        for(int a=0;a<fi.length;a++){
                            if(fi[a].isFile()){
                                b++;
                            }
                        }s=new String[b];b=0;
                        for(int a=0;a<fi.length;a++){
                            if(fi[a].isFile()&b<s.length){
                                s[b]=fi[a].getName();b++;
                            }
                        }
                    }
                }
            }else{
                if(f!=null&type!=-1){
                    if(f.isDirectory()){
                        fi=f.listFiles();
                        if(fi!=null){
                            int b=0;
                            for(int a=0;a<fi.length;a++){
                                if(fi[a].isFile()){
                                    b++;
                                }
                            }s=new String[b];b=0;
                            for(int a=0;a<fi.length;a++){
                                if(fi[a].isFile()&b<s.length){
                                    s[b]=fi[a].getName();b++;
                                }
                            }
                        }
                    }
                }
            }return s;
        }
        public String[] getFoldersString(String add){
            String[] s=null;
            File[] fi=null;
            if(add!=null){
                File tf=new File(add);
                if(tf.isDirectory()){
                    fi=tf.listFiles();
                    if(fi!=null){
                        int b=0;
                        for(int a=0;a<fi.length;a++){
                            if(fi[a].isDirectory()){
                                b++;
                            }
                        }s=new String[b];b=0;
                        for(int a=0;a<fi.length;a++){
                            if(fi[a].isDirectory()&b<s.length){
                                s[b]=fi[a].getName();b++;
                            }
                        }
                    }
                }
            }else{/*
                if(f!=null){
                    if(f.isDirectory()){
                        fi=f.listFiles();
                        if(fi!=null){
                            int b=0;
                            for(int a=0;a<fi.length;a++){
                                if(fi[a].isDirectory()){
                                    b++;
                                }
                            }s=new String[b];b=0;
                            for(int a=0;a<fi.length;a++){
                                if(fi[a].isDirectory()&b<s.length){
                                    s[b]=fi[a].getName();b++;
                                }
                            }
                        }
                    }
                }*/
                if(files!=null){
                    int b=0;
                    for(int a=0;a<files.length;a++){
                        if(files[a].isDirectory()){
                            b++;
                        }
                    }s=new String[b];b=0;
                    for(int a=0;a<files.length;a++){
                        if(files[a].isDirectory()){
                            if(type!=-1){
                                s[b]=files[a].getName();
                                b++;
                            }else{
                                s[b]=""+files[a];
                                b++;
                            }
                        }
                    }
                }
            }return s;
        }
        public String[] getHardDrivesString(){
            String[] s=null;
            this.updateHardDrives();
            //try{
                s=new String[HDs.length];
                for(int a=0;a<HDs.length;a++){
                    s[a]=HDs[a].getAbsolutePath();
                }
            return s;
        }
        public void FolderUp(){
            String Nadd=f.getParent();
            setAddress(Nadd);
        }
    }

1 个答案:

答案 0 :(得分:1)

您似乎遇到的主要问题是对并发性的误解以及事件调度线程的实际工作方式。

Swing(以及几乎所有GUI框架)都是单线程环境。也就是说,所有与UI的交互都必须在EDT的上下文中完成。因此,只要您想要创建或更改UI,就必须在EDT内完成。

美国东部时间提出的所有事件(例如actionPerformed)都是在EDT背景下完成的。

EDT基本上从事件队列中弹出一个事件并对其进行处理,通知所有感兴趣的各方发生了某些事情,直到所有感兴趣的各方都从此调用返回,EDT无法再处理任何事件。

与任何线程一样,如果执行任何长时间运行或调用任何阻塞进程,则在该代码块完成之前不能执行任何其他操作。

因此,在您的代码中,所有精彩的循环实际上阻止了EDT,阻止它处理任何新事件并导致应用程序出现阻塞。

你&#34;工作&#34;示例实际上是一个侥幸,这是你在主线程中构建UI并在此处开始循环这一事实的副作用,但actionPerformed方法实际上是从EDT调用的

首先阅读......

此外,您应该使用模式JFrame,而不是使用JDialog,它有自己的机制来阻止EDT中的代码执行,而不会阻止EDT本身。

看看How to use dialogs,您甚至可以考虑使用JFileChooser代替

相关问题