2012-03-31 137 views
2

我在代码中缺少什么让程序的窗口显示出来?我需要一个窗口来显示并显示当前正在被输入流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); 
     } 
    } 
} 
} 
+1

请学习java命名约定并坚持 – kleopatra 2012-03-31 09:37:38

回答

4

我想,以你的Swing代码的最大问题是:

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

考虑

  • 您的GUI代码,一个没有静态成员创建一个单独的类。
  • 在这个GUI中有一些字段可以从用户那里获得输入,包括源目录和目标目录,可能预先填入了默认值。
  • 创建并显示在EDT你的Swing GUI的,并让用户按一个JButton得到的东西开始,
  • 做你的文件处理在后台线程,如由SwingWorker提供,
  • 从后台线程,向事件线程发布正在执行的文件名,
  • 然后在显示的JFrame中显示该信息,也可能在JScrollPane所持有的JTextArea中显示该信息。
0

气垫船Full Of Eels是对的。如果您只打开打开JFrame的代码,则显示并显示