窗口没有弹出

时间:2012-03-31 00:07:55

标签: java swing file jframe inputstream

我的代码中缺少什么才能显示该程序的窗口?我需要一个窗口来显示并显示转换为字符串计算的inputstream src当前正在处理哪些文件,但是当我执行我的程序时JFrame不会弹出?这是我的代码

import java.io.*;
import java.awt.*;
import javax.swing.*;
public class Mover  {
public static void main(String[] args) throws IOException, InterruptedException {   

    String usb = new File(".").getAbsolutePath();
    String user = System.getProperty("user.home") + "/Desktop";
    File TS3S = new File(usb + "/Teamspeak 3");
    File TS3D = new File(user + "/TS3");
    File MinecraftLauncherS = new File(usb + "/Minecraft");
    File MinecraftLauncherD = new File(user);
    File ShortcutS = new File(usb + "/Shortcuts");
    File ShortcutD = new File(user);
    File MinecraftFilesS = new File(usb + "/minecraft files");
    File MinecraftFilesD = new File(user + "/Application Data");

    //make sure source exists
    if(!TS3S.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);

    }else{

       try{
        copyFolder(TS3S,TS3D);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
            System.exit(0);
       }
    }

    //make sure source exists
    if(!MinecraftLauncherS.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);

    }else{

       try{
        copyFolder(MinecraftLauncherS,MinecraftLauncherD);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
            System.exit(0);
       }
    }

    //make sure source exists
    if(!ShortcutS.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);

    }else{

       try{
        copyFolder(ShortcutS,ShortcutD);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
            System.exit(0);
       }
    }

    //make sure source exists
    if(!MinecraftFilesS.exists()){

       System.out.println("Directory does not exist.");
       //just exit
       System.exit(0);

    }else{

       try{
        copyFolder(MinecraftFilesS,MinecraftFilesD);
       }catch(IOException e){
        e.printStackTrace();
        //error, just exit
            System.exit(0);
       }
    }


    System.out.println("Done");
    Runtime.getRuntime ().exec (user + "/TS3/ts3client_win32.exe");
    System.exit(0);
    }

public static void copyFolder(File src, File dest)
    throws IOException{

    if(src.isDirectory()){

        //if directory not exists, create it
        if(!dest.exists()){
           dest.mkdir();
           System.out.println("Directory copied from " 
                          + src + "  to " + dest);
        }

        //list all the directory contents
        String files[] = src.list();

        for (String file : files) {
           //construct the src and dest file structure
           File srcFile = new File(src, file);
           File destFile = new File(dest, file);
           //recursive copy
           copyFolder(srcFile,destFile);
        }

    }else{
        //if file, then copy it
        //Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest); 

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes 
            while ((length = in.read(buffer)) > 0){
               out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);

            //read it with BufferedReader
            BufferedReader br
                = new BufferedReader(
                    new InputStreamReader(in));

            StringBuilder sb = new StringBuilder();

            String compute;
            while ((compute = br.readLine()) != null) {
                sb.append(compute);

                JFrame a = new JFrame("Current Files");
                a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                JLabel process = new JLabel(compute);
                process.setPreferredSize(new Dimension (300, 100));
                a.getContentPane().add(process, BorderLayout.CENTER);
                a.setLocationRelativeTo(null);
                a.pack();
                a.setVisible(true);
        } 
    }
}
}

2 个答案:

答案 0 :(得分:4)

我认为Swing代码的最大问题是:

  • 您没有遵循Swing的线程规则,其中在事件线程上调用Swing代码,并且在后台线程上调用所有其他长时间运行的进程。不这样做会有效地冻结你的Swing GUI。
  • 更多的挑剔:您似乎正在弹出许多JFrame而不是简单地显示一个JFrame,并在其中显示文件名,可能在JTextArea中。如果我是该计划的用户,我会很感激你做的是后者,而不是前者。

考虑

  • 为您的GUI代码创建一个单独的类,一个没有静态成员。
  • 在此GUI中有来自用户的输入字段,包括源目录和目标目录,可能预先填充了默认值。
  • 在EDT上创建并显示你的Swing GUI,让用户按下JButton来开始工作,
  • 在后台线程中进行文件处理,例如SwingWorker
  • 提供的后台线程
  • 从后台线程中,向事件线程发布正在执行的文件名,
  • 然后在显示的JFrame中显示该信息,也可能在JScrollPane持有的JTextArea中显示。

答案 1 :(得分:0)

Hovercraft Full Of Eels是对的。如果您只使用打开JFrame的代码,那么显示。