我的代码中缺少什么才能显示该程序的窗口?我需要一个窗口来显示并显示转换为字符串计算的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);
}
}
}
}
答案 0 :(得分:4)
我认为Swing代码的最大问题是:
考虑
答案 1 :(得分:0)
Hovercraft Full Of Eels是对的。如果您只使用打开JFrame的代码,那么会显示。