首页javajfilechooserJava Swing - 如何在JFileChooser中打开带有默认应用程序的所选文件

Java Swing - 如何在JFileChooser中打开带有默认应用程序的所选文件

我们想知道如何在JFileChooser中打开带有默认应用程序的所选文件。
import java.awt.Desktop;
import java.io.File;

import javax.swing.JFileChooser;

public class Main {
  public static void main(String[] args) throws Exception{
    JFileChooser fileChooser = new JFileChooser();
    int a = fileChooser.showOpenDialog(null);

    if (a == JFileChooser.APPROVE_OPTION) {
      File fileToOpen = fileChooser.getSelectedFile();
      Desktop.getDesktop().open(fileToOpen);
    }
  }
}