嵌入 JPanel 中的 JFileChooser
我正在编写一个需要文件打开对话框的java程序。 文件打开对话框并不困难,我希望使用JFileChooser
。 我的问题是我想要一个双窗格 JFrame
(由 2 个 JPanels
组成)。 左侧面板将有一个 JList
,右侧面板将有一个文件打开对话框。
当我使用 JFileChooser.showOpenDialog() 时,这会在所有其他窗口之上打开对话框,这不是我想要的。 有没有办法让 JFileChooser (或者可能是另一个文件选择对话框)显示在 JPanel 内部而不是在其上方弹出?
这是我尝试过的代码,此时它非常简化。 此时我只想将 JFileChooser
嵌入到 JPanel
中。
public class JFC extends JFrame{
public JFC()
{
setSize(800,600);
JPanel panel= new JPanel();
JFileChooser chooser = new JFileChooser();
panel.add(chooser);
setVisible(true);
chooser.showOpenDialog(null);
}
public static void main(String[] args)
{
JFC blah = new JFC();
}
}
我还尝试使用 this
和 panel
调用 chooser.showOpenDialog
,但无济于事。 另外,我尝试将 JFileChooser
直接添加到框架中。 上面列出的两种尝试仍然会在框架或面板前面弹出 JFileChooser
(具体取决于我将 JFileChooser
添加到的位置)。
I am writing a java program that needs a file open dialog. The file open dialog isn't difficult, I'm hoping to use a JFileChooser
. My problem is that I would like to have a dual pane JFrame
(consisting of 2 JPanels
). The left panel would have a JList
, and the right panel would have a file open dialog.
When I use JFileChooser.showOpenDialog()
this opens the dialog box above all other windows, which isn't what I want. Is there any way to have the JFileChooser
(or maybe another file selection dialog) display inside a JPanel
and not pop-up above it?
Here is the code that I've tried, at this point it's very simplified. I'm only trying to get the JFileChooser
to be embedded in the JPanel
at this point.
public class JFC extends JFrame{
public JFC()
{
setSize(800,600);
JPanel panel= new JPanel();
JFileChooser chooser = new JFileChooser();
panel.add(chooser);
setVisible(true);
chooser.showOpenDialog(null);
}
public static void main(String[] args)
{
JFC blah = new JFC();
}
}
I have also tried calling chooser.showOpenDialog
with this
and panel
, but to no avail. Also, I have tried adding the JFileChooser
directly to the frame. Both of the attempts listed above still have the JFileChooser
pop up in front of the frame or panel (depending on which I add the JFileChooser
to).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
JFileChooser 扩展了 JComponent 和 Component,因此您应该能够将其直接添加到框架中。
JFileChooser extends JComponent and Component so you should be able to add it directly to your frame.
要访问文件选择器中的“按钮”,您必须向其中添加一个 ActionListener:
To access the "buttons" in the file chooser, you will have to add an ActionListener to it:
如果您要动态添加 JFileChooser,则需要调用 revalidate()。
史蒂夫的回答是正确的。 您可以将 JFileChooser 添加到其他容器。
If you are adding the JFileChooser on the fly, you will need to call revalidate().
Steve's answer is correct. You can add a JFileChooser to other containers.
致约翰内斯:感谢您提供有用的片段。
我使用定义的常量
JFileChooser.APPROVE_SELECTION
和JFileChooser.CANCEL_SELECTION
而不是“ApproveSelection”和“CancelSelection”To Johannes: thanks for your useful snippet.
Instead of "ApproveSelection" and "CancelSelection" I used the defined constants
JFileChooser.APPROVE_SELECTION
andJFileChooser.CANCEL_SELECTION