【问题标题】:Using Windows Explorer's "Open File" function in Java [closed]在 Java 中使用 Windows 资源管理器的“打开文件”功能 [关闭]
【发布时间】:2013-05-26 23:42:45
【问题描述】:

在 Java 中使用它会将 Windows 资源管理器打开到 C 驱动器:

Desktop.getDesktop().open(new File("c:\\"));

但是,我还需要此处突出显示的“打开文件”功能:http://i.imgur.com/XfgnozF.jpg

有没有办法在 Java 中实现这一点(使用 Windows 资源管理器,而不是 Swing 的 FileChooser)?

【问题讨论】:

  • 该图像是来自 MS Word 而不是 Windows 资源管理器的打开对话框。后者没有 open 对话框
  • 是的,只是一个例子
  • 您需要更具体。 Windows 资源管理器没有打开的对话框,而所有 MS Office 应用程序都有一个打开的对话框
  • @JasmineMercier 问题是,为什么? JFileChooser 有什么问题?你试过FileDialog
  • 然后将L&F设置为当前平台的

标签: java windows swing file-io windows-explorer


【解决方案1】:

看看在使用原生系统的look & feel的同时使用JFileChooser

public class NativeOpenDialogDemo {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                }

                final JFrame frame = new JFrame("Open File Example");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                JButton openButton = new JButton("Open");
                openButton.addActionListener(new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        JFileChooser chooser = new JFileChooser();
                        if (chooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
                            // do something
                        }
                    }
                });
                frame.add(openButton);
                frame.pack();
                frame.setLocationByPlatform(true);
                frame.setVisible(true);
            }
        });
    }
}

【讨论】:

  • +1 用于查找问题和解决方案
【解决方案2】:

我们可以使用 JFileChoose,

JFileChooser chooser = new JFileChooser();
            int status = chooser.showOpenDialog(null);
            if (status == JFileChooser.APPROVE_OPTION) {
                File file = chooser.getSelectedFile();
                if (file == null) {
                    return;
                }

                String fileName = chooser.getSelectedFile().getAbsolutePath();
   ......

            }

【讨论】:

  • (使用 Windows 资源管理器,而不是 Swing 的 FileChooser)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-25
  • 1970-01-01
  • 2020-08-29
  • 1970-01-01
  • 1970-01-01
  • 2011-08-11
  • 1970-01-01
相关资源
最近更新 更多