【问题标题】:JFileChooser .txt extensionJFileChooser .txt 扩展名
【发布时间】:2013-06-15 09:40:19
【问题描述】:

我在 java 中开发了简单的记事本应用程序。当我想保存文件时,我想为该文件添加 .txt 扩展名。这在文件对话框中工作正常

String filename;
FileDialog fd = new FileDialog(Editor.this,"Save File",FileDialog.SAVE);
fd.show();
if (fd.getFile()!=null)
{
    filename = fd.getDirectory() + fd.getFile()+".txt";
    setTitle(filename);
}

在 FileDialog 中使用finename.txt 运行良好

但是当我服用

String filename;
JFileChooser save = new JFilechooser();

尝试添加扩展名

filename =save.getDirectory() + save.getFile()+".txt";

无法正常工作,请问如何解决?

【问题讨论】:

  • 如果您不提供任何示例输入、您期望的输出以及您得到的结果,那么没有人可以提供帮助
  • 是我还是 JFileChooser 甚至没有 getDirectory() 和 getFile() 方法?
  • 在这里我试图打开一个保存对话框并尝试保存扩展名为 .txt 的文件我怎样才能在 filechooser 中正常工作但在 Jfilechooser 中无法正常工作?
  • @daveychu JFileChoosergetCurrentDirectory()getSelectedFile()
  • 如需尽快获得更好的帮助,请发帖SSCCE

标签: java swing awt jfilechooser filedialog


【解决方案1】:

JFileChooser获取你使用的文件:

File currentFile= save.getCurrentFile();

只有这样你才能得到你需要的东西(例如路径):

String fileName = file.getPath();

在旁注中,JFileChooser 有一个很好的文档,请务必阅读:http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-28
    • 2013-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多