无法获取 JFileChooser 在 NetBeans IDE 8.2 中工作的示例 JAVA 代码
Can't get example JAVA code for JFileChooser to work in NetBeans IDE 8.2
我正在尝试编写一个程序,提示用户 select 一个文件夹,然后允许用户显示该文件夹中的所有内容。我一直在网上搜索并找到了几个使用 JFileChooser 的示例,包括这个 here。当我尝试在 JGrasp 中使用之前 link 中的示例时,它 运行 没有问题。但是,当我尝试在 NetBeans 中 运行 它时,出现错误(DemoJFileChooser class 无法编译,因为需要 return 类型,并且 Main 中的行说不能找到符号 DemoJFileChooser)。我不确定它有什么问题,但我已经尽我所能在下面对其进行了调整
下面的程序编译 运行 没问题,但是当我 select 选项 1 时,程序只是冻结了一秒钟,然后重新显示菜单。我无法让它显示文件 selection 对话框。我做错了什么?
import java.util.Scanner;
import javax.swing.JFileChooser;
public class FileDirectoryProcessing {
JFileChooser chooser;
String choosertitle;
public static void main(String[] args) {
SelectionMenu:
while (true) {
Scanner scannerIn = new Scanner(System.in);
System.out.println("0 - Exit");
System.out.println("1 - Select Directory");
System.out.println("2 – List directory contents");
System.out.println("Select Option");
try{
int userInput = Integer.parseInt(scannerIn.nextLine());
switch (userInput) {
case 0:
System.out.println("Thank you");
break SelectionMenu;
case 1:
System.out.println("Select a directory");
new FileDirectoryProcessing().ChooseDirectory();
break;
case 2:
System.out.println("Directory Contents");
break;
default:
System.out.println("Please make a valid selection");
break;
}
} catch (NumberFormatException e){
System.out.println("Please make a valid selection");
}
}
}
private void ChooseDirectory(){
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getCurrentDirectory()
+ "getCurrentDirectory(): ");
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
}
}
方法 showOpenDialog
接受组件,而 this
(FileDirectoryProcessing
的实例)不是组件,因此出现问题。
在方法中提供父 component
实例。或者现在您也可以传递 chooser
(JFileChooser
的实例)或 null
。
因此,以下两个选项都可以正常工作:
chooser.showOpenDialog(chooser)
或
chooser.showOpenDialog(null)
我正在尝试编写一个程序,提示用户 select 一个文件夹,然后允许用户显示该文件夹中的所有内容。我一直在网上搜索并找到了几个使用 JFileChooser 的示例,包括这个 here。当我尝试在 JGrasp 中使用之前 link 中的示例时,它 运行 没有问题。但是,当我尝试在 NetBeans 中 运行 它时,出现错误(DemoJFileChooser class 无法编译,因为需要 return 类型,并且 Main 中的行说不能找到符号 DemoJFileChooser)。我不确定它有什么问题,但我已经尽我所能在下面对其进行了调整
下面的程序编译 运行 没问题,但是当我 select 选项 1 时,程序只是冻结了一秒钟,然后重新显示菜单。我无法让它显示文件 selection 对话框。我做错了什么?
import java.util.Scanner;
import javax.swing.JFileChooser;
public class FileDirectoryProcessing {
JFileChooser chooser;
String choosertitle;
public static void main(String[] args) {
SelectionMenu:
while (true) {
Scanner scannerIn = new Scanner(System.in);
System.out.println("0 - Exit");
System.out.println("1 - Select Directory");
System.out.println("2 – List directory contents");
System.out.println("Select Option");
try{
int userInput = Integer.parseInt(scannerIn.nextLine());
switch (userInput) {
case 0:
System.out.println("Thank you");
break SelectionMenu;
case 1:
System.out.println("Select a directory");
new FileDirectoryProcessing().ChooseDirectory();
break;
case 2:
System.out.println("Directory Contents");
break;
default:
System.out.println("Please make a valid selection");
break;
}
} catch (NumberFormatException e){
System.out.println("Please make a valid selection");
}
}
}
private void ChooseDirectory(){
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle(choosertitle);
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
System.out.println(chooser.getCurrentDirectory()
+ "getCurrentDirectory(): ");
System.out.println("getSelectedFile() : "
+ chooser.getSelectedFile());
}
else {
System.out.println("No Selection ");
}
}
}
方法 showOpenDialog
接受组件,而 this
(FileDirectoryProcessing
的实例)不是组件,因此出现问题。
在方法中提供父 component
实例。或者现在您也可以传递 chooser
(JFileChooser
的实例)或 null
。
因此,以下两个选项都可以正常工作:
chooser.showOpenDialog(chooser)
或
chooser.showOpenDialog(null)