简体   繁体   English

JFileChooser-设置固定文件名

[英]JFileChooser - setting a fixed file name

I am making an application that works similar to a text editor with code highlighting. 我正在制作一个应用程序,其工作方式类似于带有代码突出显示的文本编辑器。

When I try to save some text, I want a fixed file name to appear on the JFileChooser which will not change while navigating through directories. 当我尝试保存一些文本时,我希望在JFileChooser上显示一个固定的文件名,该文件名在浏览目录时不会更改。

The reason I am doing that is because the user is going to save .java files. 我这样做的原因是因为用户将要保存.java文件。 That file should have the name of the given class. 该文件应具有给定类的名称。 Having the user type the name can only cause mistakes which will slow compiling and doesn't make sense. 让用户键入名称只会导致错误,这将导致编译缓慢且毫无意义。

This is what I have atm: 这是我的atm:

final JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int userSelection = fc.showSaveDialog(null);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File f = fc.getSelectedFile();
        String name = f.getAbsolutePath();
        File newFile = new File(name);//the text is written on this file.
    }

I dont know exactly, if that helps you. 我不完全知道,是否可以帮助您。 But I've had a similar problem and solved it like this: 但是我也遇到了类似的问题,并像这样解决了它:

final JFileChooser fc = new JFileChooser();
    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    int userSelection = fc.showSaveDialog(null);
    if (userSelection == JFileChooser.APPROVE_OPTION) {
        File f = fc.getSelectedFile();
        String name = f.getAbsolutePath();
        File newFile = new File(name + "\\" + yourfilenamehere);//the text is written on this file.
    }

I fear, that it sounds too easy... Just repace "yourfilenamehere" with the desired (fixed) filename. 我担心,这听起来太简单了……只需用所需的(固定的)文件名替换“ yourfilenamehere”即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM