代码之家  ›  专栏  ›  技术社区  ›  loadP

如何使用JFileChooser直接打开目录(位置)?

  •  0
  • loadP  · 技术社区  · 8 年前

    我尝试使用以下代码设置一些目录:

       File fn = new File("C://Users//me//Documents//Test");
       openFile = new JFileChooser();
       openFile.showOpenDialog(f);
       openFile.setCurrentDirectory(fn);
       fto = openFile.getSelectedFile();
       loadFile(openFile.getSelectedFile());
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   DevilsHnd - 退した    8 年前

    它可以是这样的:

    String startPath = "C://Users//me//Documents//Test";
    JFileChooser openFile = new JFileChooser(new File(startPath));
    openFile.showOpenDialog(null);
    
    File fileChoosen = openFile.getSelectedFile();
    String fileName = openFile.getSelectedFile().getName();
    String filePathAndName = openFile.getSelectedFile().getPath();
    
    //Do what you want with the variables...
    System.out.println(fileName);
    System.out.println(filePathAndName);