Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JFileChooser似乎正在运行,但未显示_Java_Visibility_Jfilechooser - Fatal编程技术网

Java JFileChooser似乎正在运行,但未显示

Java JFileChooser似乎正在运行,但未显示,java,visibility,jfilechooser,Java,Visibility,Jfilechooser,我正在尝试创建一个使用JFileChooser的程序,这样用户就可以为程序提供一个文件路径来进行操作。当我尝试启动JFileChooser时,没有显示任何内容,程序暂停(或者更确切地说,它似乎正在暂停)。我觉得JFileChooser正在运行,但没有以图形方式显示。当应用程序运行时,我甚至会在应用程序托盘中看到一个Java程序图标,这是我运行图形程序时才会看到的。我已经点击了它,并在运行的应用程序下检查了可用的窗口,但是没有。我不知道为什么会这样。尽管我的代码与我在网上找到的教程非常相似,但我的

我正在尝试创建一个使用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:


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

如果你不尽快得到帮助,考虑创建和发布一个你把代码浓缩到最小的位,它仍然是编译和运行的,没有外部依赖关系(比如需要链接到一个数据库或图像),没有额外的与你的问题不相关的代码,但是仍然显示你的问题。你的程序也是一个Swing GUI吗?还是一个控制台程序?如果是一个Swing GUI,请考虑使用一个非空参数用于<代码> StutOpenCudio(…)< /Cult>方法调用。如果是非Swing控制台程序,则需要确保在Swing事件线程上创建并显示对话框。它应该是利用Swing的。从我从Oracle的文档页面收集到的信息来看,提供
null
参数应该会让它在屏幕中央弹出。我尝试创建一个占据整个屏幕的JFrame,并将
showOpenDialog(parent)
的父对象设置为JFrame,但仍然没有得到任何结果。好的,请给我们看看您的。您可能在未显示的代码中有一个bug,因此您必须进行一些调试以找到该bug,或者向我们显示足够多的代码,但数量合理的代码,编译和运行的代码,这为我们演示了您的问题。我添加了整个类,因为没有什么可看的。我不确定我是否应该明确声明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 "";
    }
}