Java,使用FileDialog后,编译不会结束
我有简单的java代码来选择文件并返回数组Java,使用FileDialog后,编译不会结束,java,filedialog,Java,Filedialog,我有简单的java代码来选择文件并返回数组 当我使用专用方法(下面的整个代码)时,我的编译(在IntelliJ中)不会结束->它到达公共静态void main(String[]arg)的最后一个通道,它执行最后一个通道System.out.println(“程序结束”)但我没有从编译器获取信息: 进程已完成,退出代码为-1 相反,我需要手动停止它,如果我想再次运行它,我会得到无法并行运行代码的信息,我想现在停止它并重新运行。 整个代码: import java.util.HashMap;
当我使用专用方法(下面的整个代码)时,我的编译(在IntelliJ中)不会结束->它到达公共静态void main(String[]arg)的最后一个通道,它执行最后一个通道
System.out.println(“程序结束”)代码>但我没有从编译器获取信息:
进程已完成,退出代码为-1
相反,我需要手动停止它,如果我想再次运行它,我会得到无法并行运行代码的信息,我想现在停止它并重新运行。
整个代码:
import java.util.HashMap; //import the HashMap class
import java.io.File; // Import the File class
import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files
import java.awt.FileDialog;
import java.awt.Frame;
public class Project1
{
public static void main(String[] arg)
{
HashMap<Integer, int[][]> dicUsedFuncs = new HashMap<Integer, int[][]>();
//import textfile with haskell code
String[] arrString=SelectFile();
System.out.println(arrString[0]);
System.out.println(arrString[1]);
//scan load
System.out.println("End");
}
private static String[] SelectFile()
{
String[] arrReturn = new String[2];
String strDefaultPath=System.getProperty("user.dir"); //default location to open
Frame frame = null;
FileDialog fd = new FileDialog(frame, "Please choose a text file with code.", FileDialog.LOAD);
fd.setDirectory(strDefaultPath);
fd.setFile("*.txt");
fd.setVisible(true);
String filename = fd.getFile(); //get just the selected file name
if (filename == null) {
arrReturn[0] = "NotSelected";
}else {
filename= new File(fd.getFile()).getAbsolutePath(); //get full file path of selected file
arrReturn[0] = "Selected";
arrReturn[1] = filename;
}
return arrReturn;
}
}
import java.util.HashMap//导入HashMap类
导入java.io.File;//导入文件类
导入java.io.FileNotFoundException;//导入此类以处理错误
导入java.util.Scanner;//导入Scanner类以读取文本文件
导入java.awt.FileDialog;
导入java.awt.Frame;
公共类项目1
{
公共静态void main(字符串[]arg)
{
HashMap dicUsedFuncs=新的HashMap();
//使用haskell代码导入文本文件
String[]arrString=SelectFile();
System.out.println(arrString[0]);
System.out.println(arrString[1]);
//扫描负载
系统输出打印项次(“结束”);
}
私有静态字符串[]SelectFile()
{
字符串[]arrrurn=新字符串[2];
String strDefaultPath=System.getProperty(“user.dir”);//要打开的默认位置
Frame=null;
FileDialog fd=新建FileDialog(框架,“请选择带有代码的文本文件”,FileDialog.LOAD);
fd.setDirectory(strDefaultPath);
fd.setFile(“*.txt”);
fd.setVisible(真);
String filename=fd.getFile();//只获取选定的文件名
如果(文件名==null){
arrrurn[0]=“未选定”;
}否则{
filename=新文件(fd.getFile()).getAbsolutePath();//获取所选文件的完整文件路径
arrrurn[0]=“已选择”;
arrrurn[1]=文件名;
}
回归与回归;
}
}
我添加了以下内容:
- 图书馆
导入java.awt.Window代码>
- 循环关闭方法中的所有窗口
私有静态字符串[]SelectFile()
for(Window-Window:Window.getWindows()){Window.dispose();}
您在编译源代码时遇到问题,还是在运行应用程序时遇到问题?请将您的问题包括完整的源代码(如果可能的话)。然后在问题中添加完整的错误消息。我更新了代码-现在一切都完成了。您必须处理GUI组件,请参见,您也可以通过fd.dispose()处理您的FileDialog
实例代码>。