来自命令行程序的JFileChooser并弹出所有窗口下方

kyl*_*ill 8 java swing command-line jfilechooser awt

我已经在我的命令行程序中实现了jFileChooser,它的工作正常,只有一个烦人的问题.它似乎在每个窗口下方打开,没有任何警报.事实上,我甚至错过了几次,首先让我相信我已经错误地实施了它.

我已经实现了如下:

System.out.println("Please select the file");
JFileChooser fc = new JFileChooser();
int retValue = fc.showOpenDialog(new JPanel());
if(retValue == JFileChooser.APPROVE_OPTION){
    g.inputFile = fc.getSelectedFile();
}else {
    System.out.println("Next time select a file.");
    System.exit(1);
}
Run Code Online (Sandbox Code Playgroud)

基本上我只想要jFileChooser,以便让用户选择一个文件作为输入文件.这是唯一需要GUI实现的组件,因此如果我可以避免编写GUI,那将会很有帮助.

kyl*_*ill 11

因此,在尝试了来自不同堆栈溢出主题的各种事物后,我最终得到的结果是在Windows 7上的每个窗口上始终可靠地打开.

public class ChooseFile {
    private JFrame frame;
    public ChooseFile() {
        frame = new JFrame();

        frame.setVisible(true);
        BringToFront();
    }
    public File getFile() {
        JFileChooser fc = new JFileChooser();
        if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)){
            frame.setVisible(false);
            return fc.getSelectedFile();
        }else {
            System.out.println("Next time select a file.");
            System.exit(1);
        }
        return null;
    }

    private void BringToFront() {                  
                    frame.setExtendedState(JFrame.ICONIFIED);
            frame.setExtendedState(JFrame.NORMAL);

    }

}
Run Code Online (Sandbox Code Playgroud)

因为它在我的程序中是一个内部类,并通过调用调用:

System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();
Run Code Online (Sandbox Code Playgroud)