本文介绍了JFileChooser嵌入在JPanel中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个需要文件打开对话框的java程序。文件打开对话框并不困难,我希望使用 JFileChooser 。我的问题是我希望有一个双窗格 JFrame (由2 JPanels 组成)。左侧面板将具有 JList ,右侧面板将具有文件打开对话框。

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.

当我使用 JFileChooser.showOpenDialog()时,这会打开所有其他窗口上方的对话框,这不是我想要的是。有没有办法让 JFileChooser (或者可能是另一个文件选择对话框)显示在 JPanel 中,而不是pop-高于它?

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?

这是我尝试过的代码,此时它非常简单。我此时只想将 JFileChooser 嵌入 JPanel

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

我也尝试过调用 chooser.showOpenDialog 使用面板,但无济于事。另外,我尝试将 JFileChooser 直接添加到框架中。上面列出的两个尝试仍然在框架或面板前弹出 JFileChooser (取决于我添加 JFileChooser to)。

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).

推荐答案

JFileChooser扩展了JComponent和Component,因此您应该能够将它直接添加到您的框架中。

JFileChooser extends JComponent and Component so you should be able to add it directly to your frame.

JFileChooser fc = ...
JPanel panel ...
panel.add(fc);

这篇关于JFileChooser嵌入在JPanel中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 01:38