有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

文件Java:Bug:JFileChooser只是偶尔出现

我有一个打开JFileChooser的方法,允许用户选择一个文件,以便以后复制它的URL。然而,JFileChooser只在我运行程序时出现(?)

我不明白代码出了什么问题。提前谢谢

More clearly: The UI of the JFileChooser does not show up.

    String readThisGlos = null;

    JFrame parentFrame = new JFrame();
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setDialogTitle("Choose");
    int userSelection = fileChooser.showOpenDialog(parentFrame);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File fileToSave = fileChooser.getSelectedFile();

        readThisGlos = fileToSave.getAbsolutePath();
    } else {
        // Error
    }
    System.out.println(readThisGlos);
    return readThisGlos;

编辑:我知道该方法正在运行,因为当我放置System.out.println("//something");在这个方法的开始,它是有效的

If I put System.out.println("//something"); between fileChooser and int userSelection, the UI shows up.


共 (1) 个答案

  1. # 1 楼答案

    我曾经遇到过类似的问题,我甚至无法描述我当时的沮丧

    试试这个:

    1. 重新启动计算机
    2. 在eclipse中:File>;重启
    3. Clean:C:\eclipse\eclipse。exe-vm“C:\Program Files\Java\jdk1.6.0_24\bin”-清除
    4. showOpenDialog(parentFrame)更改为showOpenDialog(null)(正如用户所建议的,不必要的事情往往会产生问题)
    5. 再次运行程序