JavaVM Windows 7 64 位 - JFileChooser() 不显示对话框
我正在尝试创建一个基于控制台的简单java应用程序,它要求用户从本地文件系统中选择文件。
控制台提示用户选择可用选项之一,然后打开给定的输入。
public Client() throws UnknownHostException, IOException {
printuseroptions();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char userdecision = br.readLine().charAt(0);
System.out.println(userdecision);
switch(userdecision){
case '1':
System.out.println("Which file would you like to open?");
openfile(br.readLine());
break;
case '2':
System.out.println("Which file would you like to close?");
closefile(br.readLine());
break;
}
private boolean openfile(String path){
System.out.println("openfile("+path+")");
return false;
}
private boolean closefile(String path){
System.out.println("closefile("+path+")");
new JFileChooser().showOpenDialog(null);
return false;
}
无论我做什么,JFileChooser 弹出框都不会打开。控制台上没有显示错误,但调试单步显示以下错误:
块引用 线程 [主](已暂停)
ClassNotFoundException(Throwable)。(字符串,Throwable)行:217
ClassNotFoundException(Exception).(String, Throwable) 行:不可用 ClassNotFoundException.(String) 行:不可用
URLClassLoader$1.run() 行:不可用
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) 行:不可用 [本机方法]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) 行:不可用
Launcher$ExtClassLoader.findClass(String) 行:不可用
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) 行:不可用 Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) 行:不可用 Launcher$AppClassLoader.loadClass(String, boolean) 行:不可用
Launcher$AppClassLoader(ClassLoader).loadClass(String) 行:不可用
ResourceBundle$RBClassLoader.loadClass(String) 行:不可用
CoreResourceBundleControl(ResourceBundle$Control).newBundle(String, Locale, String, ClassLoader, boolean) 行:不可用
ResourceBundle.loadBundle(CacheKey, List, Control, boolean) 行:不可用 ResourceBundle.findBundle(CacheKey, List, List, int, Control, ResourceBundle) 行:不可用
ResourceBundle.getBundleImpl(String, Locale, ClassLoader, ResourceBundle$Control) 行:不可用
ResourceBundle.getBundle(String, ResourceBundle$Control) 行:不可用
Toolkit$3.run() 行:不可用 AccessController.doPrivileged(PrivilegedAction) 行:不可用 [本机方法]
Toolkit.() 行:不可用
Component.() 行:不可用
Client.closefile() 行:90 客户端.() 行:60
Client.main(String[]) 行:36
相同的代码在 Linux 32 位机器上完美运行,所以我怀疑问题与 Windows 有关。
下面的代码在 Windows 和 Linux 上都按预期运行,因此我怀疑可能是由于 Windows 与 Linux 中处理控制台输入的方式不同(CR LF)所致。
import javax.swing.JFileChooser;
public class Example {
public static void main(String[] args) {
new JFileChooser().showOpenDialog(null);
}
}
谢谢
I am trying to create a simple console based java application, which requires users to select files from their local filesystem.
The console prompts the user to select one of the available options and then switches on the input given.
public Client() throws UnknownHostException, IOException {
printuseroptions();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char userdecision = br.readLine().charAt(0);
System.out.println(userdecision);
switch(userdecision){
case '1':
System.out.println("Which file would you like to open?");
openfile(br.readLine());
break;
case '2':
System.out.println("Which file would you like to close?");
closefile(br.readLine());
break;
}
private boolean openfile(String path){
System.out.println("openfile("+path+")");
return false;
}
private boolean closefile(String path){
System.out.println("closefile("+path+")");
new JFileChooser().showOpenDialog(null);
return false;
}
No matter what I do, the JFileChooser pop up box will not open. No error is shown on the console, but a debug step-through shows the following error:
Blockquote
Thread [main] (Suspended)
ClassNotFoundException(Throwable).(String, Throwable) line: 217
ClassNotFoundException(Exception).(String, Throwable) line: not available
ClassNotFoundException.(String) line: not available
URLClassLoader$1.run() line: not available
AccessController.doPrivileged(PrivilegedExceptionAction, AccessControlContext) line: not available [native method]
Launcher$ExtClassLoader(URLClassLoader).findClass(String) line: not available
Launcher$ExtClassLoader.findClass(String) line: not available
Launcher$ExtClassLoader(ClassLoader).loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String, boolean) line: not available
Launcher$AppClassLoader.loadClass(String, boolean) line: not available
Launcher$AppClassLoader(ClassLoader).loadClass(String) line: not available
ResourceBundle$RBClassLoader.loadClass(String) line: not available
CoreResourceBundleControl(ResourceBundle$Control).newBundle(String, Locale, String, ClassLoader, boolean) line: not available
ResourceBundle.loadBundle(CacheKey, List, Control, boolean) line: not available
ResourceBundle.findBundle(CacheKey, List, List, int, Control, ResourceBundle) line: not available
ResourceBundle.getBundleImpl(String, Locale, ClassLoader, ResourceBundle$Control) line: not available
ResourceBundle.getBundle(String, ResourceBundle$Control) line: not available
Toolkit$3.run() line: not available
AccessController.doPrivileged(PrivilegedAction) line: not available [native method]
Toolkit.() line: not available
Component.() line: not available
Client.closefile() line: 90
Client.() line: 60
Client.main(String[]) line: 36
The same code runs perfectly on a Linux 32 bit machine, so I suspect the problem is Windows related.
The code below runs as expected on both Windows and Linux so I suspect might be due to the different ways in while console input is handled in Windows vs Linux (CR LF).
import javax.swing.JFileChooser;
public class Example {
public static void main(String[] args) {
new JFileChooser().showOpenDialog(null);
}
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来你和我一样是新来的。 ;) 让我们看看我是否可以提供帮助。
我对您的代码进行了更改以使其能够编译,并在 Windows Server 2003 x64 计算机上运行它,并且没有看到任何问题 - 文件选择器对话框打开。
我建议您可以做两件事来消除其他可能性:
1)确保系统的本机外观和外观。感觉已设定。设置你的外观和程序启动时使用此功能来感受系统默认设置:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
2) 确保您只构建并打开
JFileChooserDialog
,并且事件调度线程 (EDT) 内的所有其他 Swing 组件。如果您知道当前线程是主线程或其他工作线程(我假设这是因为您正在接受控制台输入),则需要调用SwingUtilities.invokeLater(Runnable)
才能正确执行。祝你好运。
It looks like you're just as new here as I am. ;) Let's see if I can help.
I made changes to your code to get it to compile, and ran it on a Windows Server 2003 x64 machine, and didn't see any problems - the file chooser dialog opens.
I suggest two things you can do to eliminate other possibilities:
1) Ensure the system's native look & feel is set. Set your look & feel to the system default by using this when your program starts:
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
2) Ensure you only construct and open your
JFileChooserDialog
, and all other Swing components, inside the Event Dispatch Thread (EDT). If you know the current thread is the main thread or some other worker thread (and I assume it is because you're taking console input), you need to callSwingUtilities.invokeLater(Runnable)
for correct execution.Good luck with that.