java 嵌入在 JPanel 中的 JFileChooser
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/259575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JFileChooser embedded in a JPanel
提问by Shane
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.
我正在编写一个需要文件打开对话框的 Java 程序。文件打开对话框并不难,我希望使用JFileChooser. 我的问题是我想要一个双窗格JFrame(由 2 组成JPanels)。左侧面板将有一个JList,右侧面板将有一个文件打开对话框。
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 JPaneland not pop-up above it?
当我使用JFileChooser.showOpenDialog()它打开所有其他窗口上方的对话框时,这不是我想要的。有没有办法让JFileChooser(或者另一个文件选择对话框)显示在 a 内JPanel而不是在它上面弹出?
Here is the code that I've tried, at this point it's very simplified. I'm only trying to get the JFileChooserto be embedded in the JPanelat this point.
这是我尝试过的代码,在这一点上它非常简化。在这一点上,我只是想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();
}
}
I have also tried calling chooser.showOpenDialogwith thisand panel, but to no avail. Also, I have tried adding the JFileChooserdirectly to the frame. Both of the attempts listed above still have the JFileChooserpop up in front of the frame or panel (depending on which I add the JFileChooserto).
我也试过chooser.showOpenDialog用thisand调用panel,但无济于事。另外,我尝试将JFileChooser直接添加到框架中。上面列出的两种尝试仍然JFileChooser在框架或面板前面弹出(取决于我添加JFileChooser到哪个)。
回答by Steve Kuo
JFileChooser extends JComponent and Component so you should be able to add it directly to your frame.
JFileChooser 扩展了 JComponent 和 Component,因此您应该能够将其直接添加到您的框架中。
JFileChooser fc = ...
JPanel panel ...
panel.add(fc);
回答by Shane
To access the "buttons" in the file chooser, you will have to add an ActionListener to it:
要访问文件选择器中的“按钮”,您必须向其添加一个 ActionListener:
fileChooser.addActionListener(this);
[...]
public void actionPerformed(ActionEvent action)
{
if (action.getActionCommand().equals("CancelSelection"))
{
System.out.printf("CancelSelection\n");
this.setVisible(false);
this.dispose();
}
if (action.getActionCommand().equals("ApproveSelection"))
{
System.out.printf("ApproveSelection\n");
this.setVisible(false);
this.dispose();
}
}
回答by James Schek
If you are adding the JFileChooser on the fly, you will need to call revalidate().
如果您要动态添加 JFileChooser,则需要调用 revalidate()。
Steve's answer is correct. You can add a JFileChooser to other containers.
史蒂夫的回答是正确的。您可以将 JFileChooser 添加到其他容器。
回答by Carles Barrobés
To Johannes: thanks for your useful snippet.
致约翰内斯:感谢您提供有用的片段。
Instead of "ApproveSelection" and "CancelSelection" I used the defined constants JFileChooser.APPROVE_SELECTIONand JFileChooser.CANCEL_SELECTION
我使用了定义的常量JFileChooser.APPROVE_SELECTION而不是“ApproveSelection”和“CancelSelection”JFileChooser.CANCEL_SELECTION

