来自命令行程序的 JFileChooser 并在所有窗口下方弹出
我已经在我的命令行程序中实现了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因此,在尝试了不同堆栈溢出主题中的各种内容之后,我最终得到了一个在 Windows 7 上的每个窗口上方一致且可靠地打开的结果
。在我的程序中,它是一个内部类,并通过调用来调用:
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.
As it stands in my program it is an inner class and is invoked by calling:
我认为出现这种情况有两个可能的原因:
如果您正在做这两件事中的任何一个,那么您应该简化并使其只是一个 Swing GUI 类型的程序。如果此信息没有帮助,那么您可能希望向我们提供有关您的问题的更多信息。
编辑 1
我刚刚注意到你的代码的细节。下面的新 JPanel() 部分是一个问题:
要使 JFileChooser 充当顶级窗口的对话框(它当前没有执行此操作,这是您的主要问题),您应该传递位于父顶级窗口中的组件,例如 JFrame 或 JApplet 内部的 JPanel 或 JButton。
编辑2
好吧,您正在尝试将 Java 控制台程序与 Swing GUI 程序混合在一起,这就像吃冰淇淋和泡菜一样——它们只是不能很好地结合在一起。没有顶级窗口可提供给 JFileChooser 的 showOpenDialog 方法,因此它将充当真正的对话框。
最好的解决方案是不要这样做,而是将应用程序重写为完整的 Swing GUI。
I think of two possible causes for something like this:
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:
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.
在我的代码中,我可以只使用 null 并且它可以工作。我在 Windows 7 上使用 Java 7。
In my code, I can just use null and it works. I'm using Java 7 on Windows 7.