【问题标题】:Window not poping up窗口不弹出
【发布时间】:2012-04-14 15:50:59
【问题描述】:

为了让程序的窗口显示出来,我在代码中遗漏了什么?我需要一个窗口来显示并显示转换为字符串计算的输入流 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);
        } 
    }
}
}

【问题讨论】:

  • 请学习java命名约定并遵守它们

标签: java swing file jframe inputstream


【解决方案1】:

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

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

考虑

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

【讨论】:

    【解决方案2】:

    充满鳗鱼的气垫船是对的。如果您只使用打开 JFrame 的代码,它确实会显示出来。

    【讨论】:

      猜你喜欢
      • 2022-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多