Java 如何使用JFileChooser查找文件位置

Java 如何使用JFileChooser查找文件位置,java,swing,jfilechooser,Java,Swing,Jfilechooser,是否有一种方法可以简单地查找文件位置?我尝试允许用户选择一个文件并打开它,但我必须让JFileChooser选择该文件并将位置发送给另一个方法。最好的方法是什么?show中的示例: JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG & GIF Images", "jpg", "gif"); chooser.set

是否有一种方法可以简单地查找文件位置?我尝试允许用户选择一个文件并打开它,但我必须让JFileChooser选择该文件并将位置发送给另一个方法。最好的方法是什么?

show中的示例:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}
这就是
chooser.getSelectedFile()
正在做的事情。将结果传递给另一个方法

show中的示例演示了如何执行此操作:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
    "JPG & GIF Images", "jpg", "gif");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
   System.out.println("You chose to open this file: " +
        chooser.getSelectedFile().getName());
}
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// You can use 
// chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);  too

File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();
这就是
chooser.getSelectedFile()
正在做的事情。将结果传递给另一个方法

chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

// You can use 
// chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);  too

File file = chooser.getSelectedFile();
String fullPath = file.getAbsolutePath();
然后将字符串传递给另一个方法


然后将字符串传递给另一个方法。

我们还可以使用TextArea获取任何文件的路径,例如图像文件和文件名 对象TextArea是txtPath,我们使用以下方法对名为bChoose的JButton执行ActionPerformed

JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif");
fc.setFileFilter(filter);
fc.showDialog(bChoose, "Choose File");
String strPath = txtPath.getText() + "\n" + fc.getSelectedFile().toString();
txtPath.setText(strPath);

我们还可以使用TextArea获取任何文件的路径,例如图像文件和文件名 对象TextArea是txtPath,我们使用以下方法对名为bChoose的JButton执行ActionPerformed

JFileChooser fc = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Image Files", "jpg", "png", "gif");
fc.setFileFilter(filter);
fc.showDialog(bChoose, "Choose File");
String strPath = txtPath.getText() + "\n" + fc.getSelectedFile().toString();
txtPath.setText(strPath);

为什么不传递文件引用?它有许多与文件系统相关联的有用方法,只是saying@MadProgrammer同意OP要求发送有关位置的信息。所以给了OP一个方法。为什么不传递文件引用呢?它有许多与文件系统相关联的有用方法,只是saying@MadProgrammer同意OP要求发送有关位置的信息。所以我给了OP一个方法,我不明白你的问题。您想“…简单地查找文件位置…”?是否希望“…允许用户选择文件…”?但是你必须有一个JFileChooser?JFileChooser是让用户选择文件的好方法。让用户更容易的一件事是为JFileChooser指定一个目录。如果您想通过自己的对话框让用户选择一个文件,您可以在JDialog中有一个JList,您可以在自己的代码中将文件名添加到JList中。我不确定你到底在问什么。不管你得到什么,都要使用文件对象。我不明白你的问题。您想“…简单地查找文件位置…”?是否希望“…允许用户选择文件…”?但是你必须有一个JFileChooser?JFileChooser是让用户选择文件的好方法。让用户更容易的一件事是为JFileChooser指定一个目录。如果您想通过自己的对话框让用户选择一个文件,您可以在JDialog中有一个JList,您可以在自己的代码中将文件名添加到JList中。我不确定你到底在问什么。无论您如何获得它,请使用文件对象。。