为了显示程序窗口,我的代码中缺少什么?我需要一个窗口来显示并显示当前正在由转换为字符串计算的输入流 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);
}
}
}
}
最佳答案
我认为您的 Swing 代码最大的问题是:
- 您没有遵循 Swing 的线程规则,即在事件线程上调用 Swing 代码,而在后台线程上调用所有其他长时间运行的进程。不这样做将会有效地卡住您的 Swing GUI。
- 更多的挑剔:您似乎弹出了许多 JFrame,而不是简单地显示一个 JFrame 并在其中显示文件名,也许在 JTextArea 中。如果我是该程序的用户,我会很感激您执行后者而不是前者。
考虑
- 为您的 GUI 代码创建一个没有静态成员的单独类。
- 在此 GUI 中有用于获取用户输入的字段,包括源目录和目标目录,可能预先填充了默认值。
- 在 EDT 上创建并显示 Swing GUI,并让用户按 JButton 开始操作,
- 在后台线程中进行文件处理,例如 SwingWorker 提供的线程,
- 从后台线程,将正在处理的文件名发布到事件线程,
- 然后在显示的 JFrame 中显示该信息,也可能在由 JScrollPane 保存的 JTextArea 中显示。
关于java - 窗口不弹出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9951814/