在Java中使用它将打开Windows资源管理器到C盘:
Desktop.getDesktop().open(new File("c:\\"));
但是,我还需要在此处突出显示“打开文件”功能:http://i.imgur.com/XfgnozF.jpg
有没有办法在Java中实现它(使用Windows资源管理器,而不是Swing的FileChooser)?
答案 0 :(得分:4)
在使用原生系统的JFileChooser时,请查看使用look & feel:
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 :(得分:1)
我们可以使用JFileChooser,
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();
......
}