使用Java的文件资源管理器 - 如何去做?

时间:2021-07-12 21:48:37

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来实现文件浏览器。是什么给了我毛骨悚然的想法,没有什么可以模仿默认的资源管理器视图...你能提供一些关于它的指示吗?我是否获取文件列表,获取图标,然后在屏幕上以网格方式排列它们以显示默认的资源管理器视图?

2 个解决方案

#1


6  

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 Overview will help you add more Swing components to your project.

Oracle的Swing Overview将帮助您为项目添加更多Swing组件。

#2


4  

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 FileDialog displays 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://*.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);
    }
}

#1


6  

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 Overview will help you add more Swing components to your project.

Oracle的Swing Overview将帮助您为项目添加更多Swing组件。

#2


4  

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 FileDialog displays 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://*.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);
    }
}