使用 Swing 在窗格中选择文件 [英] Choosing a file in-pane with Swing
问题描述
我正在 Swing 中编写一个 GUI,我想在其中执行主窗口中的文件选择器,如下图所示:
I'm writing a GUI in Swing where I want to do a file chooser that is in the main window, looking something like the image below:
虽然似乎有很多关于如何编写弹出文件选择器的教程,但我没有看到太多关于如何在 Swing 中完成这种类型的选择器的信息.
while there seem to be quite a few tutorials on how to write a popup file chooser, i don't see much information on how this type of chooser might be accomplished in swing.
如果之前有人问过这个问题,我也很抱歉,我四处搜索,但找不到其他任何东西..
also sorry if this has been asked before, i did a good bit of searching around and wan't able to find anything else..
推荐答案
JFileChooser 实际上扩展了 JComponent,因此您可以像使用任何其他组件一样使用.这是一个带有两个窗格内文件选择器的示例:
JFileChooser actually extends JComponent, so you can use like any other component. Here is an example with two in-pane file choosers:
public class TestInPaneChoosers {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
buildFrame();
}
});
}
private static void buildFrame() {
JFrame f = new JFrame("Test");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setLayout(new FlowLayout());
f.add(new JFileChooser());
f.add(new JFileChooser());
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
}
这篇关于使用 Swing 在窗格中选择文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!