JavaVM Windows 7 64 位 - JFileChooser() 不显示对话框 [英] JavaVM Windows 7 64bit - JFileChooser() not showing dialog box

查看:32
本文介绍了JavaVM Windows 7 64 位 - JFileChooser() 不显示对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个简单的基于控制台的 Java 应用程序,它要求用户从他们的本地文件系统中选择文件.

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;
}

无论我做什么,JFileChooser 弹出框都打不开.控制台上未显示任何错误,但调试步骤显示以下错误:

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:

块引用线程 [主要](暂停)
ClassNotFoundException(Throwable).(String, 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() 行:90Client.() 行:60
Client.main(String[]) 行:36

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

同样的代码在 Linux 32 位机器上运行完美,所以我怀疑问题与 Windows 相关.

The same code runs perfectly on a Linux 32 bit machine, so I suspect the problem is Windows related.

下面的代码在 Windows 和 Linux 上都按预期运行,所以我怀疑可能是由于在 Windows 和 Linux (CR LF) 中处理控制台输入的方式不同.

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);
    }
}

谢谢

推荐答案

看来你和我一样新来的.;) 让我们看看能不能帮上忙.

It looks like you're just as new here as I am. ;) Let's see if I can help.

我对您的代码进行了更改以使其能够编译,并在 Windows Server 2003 x64 机器上运行它,并且没有发现任何问题 - 文件选择器对话框打开.

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) 确保系统的原生外观&感觉定了.设置您的外观通过在程序启动时使用它来感受系统默认值:UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

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) 确保您仅在事件调度线程 (EDT) 内构建和打开您的 JFileChooserDialog 和所有其他 Swing 组件.如果您知道当前线程是主线程或其他一些工作线程(我认为这是因为您正在接受控制台输入),您需要调用 SwingUtilities.invokeLater(Runnable) 以正确执行.

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 call SwingUtilities.invokeLater(Runnable) for correct execution.

祝你好运.

这篇关于JavaVM Windows 7 64 位 - JFileChooser() 不显示对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆