使用 Java 的文件资源管理器 - 如何进行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2914627/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 23:26:09  来源:igfitidea点击:

File Explorer using Java - how to go about it?

javaswingswtjface

提问by Chaitanya

I am set to create a file explorer using Java. The aim is to emulate the behavior of the default explorer as closely as possible, whatever may be the underlying OS.

我准备使用 Java 创建一个文件浏览器。目的是尽可能模拟默认浏览器的行为,无论底层操作系统是什么。

I have done NO GUI programming in Java.

我没有用 Java 做过 GUI 编程。

I have looked-up Swing, SWT and JFace, and I am beginning my project with this tutorial: http://www.ibm.com/developerworks/opensource/library/os-ecgui1/

我已经查找了 Swing、SWT 和 JFace,并且我正在通过本教程开始我的项目:http: //www.ibm.com/developerworks/opensource/library/os-ecgui1/

I would like to know your opinions about the best approach to tackle this problem. If you could comment on complexity of coding, portability and OS-independence, and efficiency, it would be great.

我想知道您对解决此问题的最佳方法的看法。如果您能评论编码的复杂性、可移植性和操作系统独立性以及效率,那就太好了。

Is there anything else I should know? Do some other ways exist?

还有什么我应该知道的吗?是否存在其他方式?

Thanks a lot!

非常感谢!



Thanks for the answers and replies.

感谢您的回答和回复。

Looks like I will choose Swing to implement the file explorer. What gives me the creeps is the thought that there would be nothing to mimic the default explorer view... Could you please provide some pointers about it? Do I get list of files, get icons and then arrange them in a grid fashion on the screen to show the default explorer view?

看来我会选择 Swing 来实现文件浏览器。让我毛骨悚然的是认为没有什么可以模仿默认的资源管理器视图......你能提供一些关于它的指针吗?我是否获取文件列表、获取图标,然后在屏幕上以网格方式排列它们以显示默认资源管理器视图?

采纳答案by Gilbert Le Blanc

You would be better off using Swing. You need different versions of SWT and JFace for different operating systems.

最好使用 Swing。对于不同的操作系统,您需要不同版本的 SWT 和 JFace。

The best approach is to start off simple, and add to what you have as you learn more.

最好的方法是从简单的开始,并随着您了解的更多而添加到您所拥有的。

To get you started, you need a JFrame with two JPanel children.

为了让您开始,您需要一个带有两个 JPanel 子项的 JFrame。

You'll need to add a JMenuBar to the JFrame. JMenu items are added to the JMenuBar. JMenuItem items are added to the JMenu.

您需要将 JMenuBar 添加到 JFrame。JMenu 项被添加到 JMenuBar。JMenuItem 项被添加到 JMenu。

Oracle's Swing Overviewwill help you add more Swing components to your project.

Oracle 的Swing 概述将帮助您向项目添加更多 Swing 组件。

回答by trashgod

I'd start with How to Use File Choosers, but the example in org.netbeans.swing.outline.Outline, discussed here, is appealing.

我将从如何使用文件选择器开始,但此处org.netbeans.swing.outline.Outline讨论的 中的示例很吸引人。

Addendum: @Gilbert Le Blanc raises an excellent point about the ease & portability of Swing. In contrast, SWT requires slightly more effort to deploy, but some users prefer the greater fidelity of org.eclipse.swt.widgets.FileDialog, as shown here.

附录:@Gilbert Le Blanc 就 Swing 的易用性和便携性提出了一个很好的观点。与此相反,SWT需要稍微更多的精力来部署,但有些用户喜欢的更高的保真度org.eclipse.swt.widgets.FileDialog,如图所示这里

Addendum: I notice that FileDialogdisplays a more native-looking window, as seen here. You might try it on your target platform(s).

附录:我注意到,FileDialog显示器更本地外观的窗口,看到这里。您可以在目标平台上尝试。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** @see https://stackoverflow.com/questions/2914733 */
public class FileDialogTest {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLayout(new GridLayout(0, 1));
        frame.add(new JButton(new AbstractAction("Load") {

            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd = new FileDialog(frame, "Test", FileDialog.LOAD);
                fd.setVisible(true);
                System.out.println(fd.getFile());
            }
        }));
        frame.add(new JButton(new AbstractAction("Save") {

            @Override
            public void actionPerformed(ActionEvent e) {
                FileDialog fd = new FileDialog(frame, "Test", FileDialog.SAVE);
                fd.setVisible(true);
                System.out.println(fd.getFile());
            }
        }));
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}