JFileChooser似乎正在运行,但没有显示

时间:2014-11-24 01:59:13

标签: java visibility jfilechooser

我正在尝试创建一个涉及使用JFileChooser的程序,以便用户可以为程序提供文件的操作路径。当我尝试启动JFileChooser时,没有任何内容出现,程序暂停(或者说它似乎暂停)。我觉得JFileChooser 正在运行,但没有以图形方式显示。我甚至在运行时在我的应用程序托盘中获得了一个Java程序图标,这是我在运行图形程序时才得到的。我点击它并检查正在运行的应用程序下的可用窗口,但没有。我不知道为什么会这样。我的代码,即使它与我在网上找到的教程非常相似,但是:

final JFileChooser userFile = new JFileChooser();
int response = userFile.showOpenDialog(null);
if (response == JFileChooser.APPROVE_OPTION)
   fileName = userFile.getSelectedFile().toString();
else
   fileName = "The file open operation failed.";

MCVE:

import lots of stuff;

public class zipCracker {

    private static String fileName;

    public static void main(String[] args){
        String[] buttons = {"Cancel", "zDictionaryForm", "zZipCracker"};

        int rc = JOptionPane.showOptionDialog(null,
                                              "Which program would you like to use?",
                                              "Program Directory",
                                              JOptionPane.WARNING_MESSAGE,
                                              0, null, buttons, buttons[0]);
        if(rc == 2)
            zZipCracker();
        else if(rc == 1)
            System.exit(0);
        else
            System.exit(0);
    }

    public static String zZipCracker(){
        final JFileChooser userFile = new JFileChooser();
        int response = userFile.showOpenDialog(null);
        if (response == JFileChooser.APPROVE_OPTION)
            fileName = userFile.getSelectedFile().toString();
        else
            fileName = "The file open operation failed.";


        //ZipFile zipper = new ZipFile(userFile);
        return "";
    }
}

1 个答案:

答案 0 :(得分:2)

似乎使JFileChooser对象全局,私有和静态解决了我的问题。我不知道在方法中声明它和在全局声明它之间的区别在哪里,但是它有效。

import stuff;

public class zipCracker {

    private static String fileName;
    private static JFileChooser userFile = new JFileChooser(); //now declared globally

    public static void main(String[] args){
        String[] buttons = {"Cancel", "zDictionaryForm", "zZipCracker"};

        int rc = JOptionPane.showOptionDialog(null,
                                              "Which program would you like to use?",
                                              "Program Directory",
                                              JOptionPane.WARNING_MESSAGE,
                                              0, null, buttons, buttons[0]);
        System.out.println(rc);
        if(rc == 2)
            zZipCracker();
        else if(rc == 1)
            System.exit(0);
        else
            System.exit(0);
    }

    public static String zZipCracker(){
        int returnVal = userFile.showDialog(null, "Choose This"); //used without being declared here in the method
        if (returnVal == JFileChooser.APPROVE_OPTION)
            fileName = userFile.getSelectedFile().toString();
        else
            fileName = "The file open operation failed.";


        //ZipFile zipper = new ZipFile(userFile);
        return "";
    }
}