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

发布于 2024-12-05 19:01:51 字数 539 浏览 1 评论 0原文

我已经在我的命令行程序中实现了 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);
}

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

Ive implemented the jFileChooser in my command line program and it works, just as it should with only one annoying issue. It seems that it opens underneath every window with no alert of any kind. In fact I even missed it a couple of times at first leading me to believe that i had implemented it wrong.

I have implemented this as follows:

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

Essentially I only want the jFileChooser in order to have the user select a file as an input file. This is the only component that has a need for a GUI implementation, so if i can avoid writing up an GUI, that would be helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

何以畏孤独 2024-12-12 19:01:51

因此,在尝试了不同堆栈溢出主题中的各种内容之后,我最终得到了一个在 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);

    }

}

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

System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();

So after trying a variety of things from different stack overflow topics I ended up with a result that consistently and reliably opens above every window on 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);

    }

}

As it stands in my program it is an inner class and is invoked by calling:

System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();
森林很绿却致人迷途 2024-12-12 19:01:51

我认为出现这种情况有两个可能的原因:

  • 您试图在同一个程序中混合 AWT 和 Swing GUI,或者
  • 您试图混合控制台程序(即使用 System.out.println(...)并通过 Scanner 对象获取输入)并使用 Swing GUI。

如果您正在做这两件事中的任何一个,那么您应该简化并使其只是一个 Swing GUI 类型的程序。如果此信息没有帮助,那么您可能希望向我们提供有关您的问题的更多信息。

编辑 1
我刚刚注意到你的代码的细节。下面的新 JPanel() 部分是一个问题:

int retValue = fc.showOpenDialog(new JPanel());

要使 JFileChooser 充当顶级窗口的对话框(它当前没有执行此操作,这是您的主要问题),您应该传递位于父顶级窗口中的组件,例如 JFrame 或 JApplet 内部的 JPanel 或 JButton。

编辑2
好吧,您正在尝试将 Java 控制台程序与 Swing GUI 程序混合在一起,这就像吃冰淇淋和泡菜一样——它们只是不能很好地结合在一起。没有顶级窗口可提供给 JFileChooser 的 showOpenDialog 方法,因此它将充当真正的对话框。

最好的解决方案是不要这样做,而是将应用程序重写为完整的 Swing GUI。

I think of two possible causes for something like this:

  • You're trying to mix AWT and Swing GUI's in the same program, or
  • You're trying to mix a console program (i.e., using System.out.println(...) and getting input via a Scanner object) with a Swing GUI.

If you are doing either of these two things, then you should simplify and make it only a Swing GUI type program. If this information doesn't help, then you may wish to give us more information about your problem.

Edit 1
I just noticed the details of your code. The new JPanel() part below is a problem:

int retValue = fc.showOpenDialog(new JPanel());

To make the JFileChooser act as a dialog to your top level window (which it is currently not doing and which is your chief problem), you should instead pass a component that is in the parent top level window, such as a JPanel or JButton that is held inside of your JFrame or JApplet.

Edit 2
OK, you're trying to mix a Java console program with a Swing GUI program which is like eating ice cream with pickles -- they just don't go well together. There is no top-level window to offer to the JFileChooser's showOpenDialog method so that it will act as a true dialog.

The best solution is to not do this, to instead re-write your application to be a complete Swing GUI.

黯然#的苍凉 2024-12-12 19:01:51

在我的代码中,我可以只使用 null 并且它可以工作。我在 Windows 7 上使用 Java 7。

JFileChooser chooser = new JFileChooser(System.getProperty("java.class.path"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
 try {
  Scanner inputFile= new Scanner(new File(chooser.getSelectedFile().getAbsolutePath()));
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
}

In my code, I can just use null and it works. I'm using Java 7 on Windows 7.

JFileChooser chooser = new JFileChooser(System.getProperty("java.class.path"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
 try {
  Scanner inputFile= new Scanner(new File(chooser.getSelectedFile().getAbsolutePath()));
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文