java - 设置要创建的文件的路径

标签 java path text-editor

我目前正在使用 Java 开发文本编辑器。这是到目前为止的代码:

package com.thundercrust.applications;

public class TextEditor implements ActionListener{

private static Formatter format = new Formatter();

private static String[] fontOptions = {"Serif", "Agency FB", "Arial", "Calibri", "Cambrian", "Century Gothic", "Comic Sans MS", "Courier New", "Forte", "Garamond", "Monospaced", "Segoe UI", "Times New Roman", "Trebuchet MS", "Serif"};
private static String[] sizeOptions = {"8", "10", "12", "14", "16", "18", "20", "22", "24", "26", "28", "30", "32", "34", "36", "38", "40"};

ImageIcon newIcon = new ImageIcon("res/NewIcon.png");
ImageIcon saveIcon = new ImageIcon("res/SaveIcon.png");
ImageIcon openIcon = new ImageIcon("res/OpenIcon.png");
ImageIcon fontIcon = new ImageIcon("res/FontIcon.png");
ImageIcon changeFontIcon = new ImageIcon("res/ChangeFontIcon.png");

JButton New = new JButton(newIcon);
JButton Save = new JButton(saveIcon);
JButton Open = new JButton(openIcon);
JButton changeFont = new JButton(changeFontIcon);

JLabel fontLabel = new JLabel(fontIcon);
JLabel fontLabelText = new JLabel("Font: ");
JLabel fontSizeLabel = new JLabel("Size: ");

JComboBox <String> fontName = new JComboBox<>(fontOptions);
JComboBox <String> fontSize = new JComboBox<>(sizeOptions);

JToolBar tool = new JToolBar();

JTextArea texty = new JTextArea();
JScrollPane scroll = new JScrollPane(texty);

private static final int WIDTH = 1366;
private static final int HEIGHT = WIDTH / 16 * 9;

private static String name = "Text Editor"; 

private static JFrame frame = new JFrame();

public void Display() {
    frame.setTitle(name);
    frame.setSize(WIDTH, HEIGHT);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLocationRelativeTo(null);
    frame.setResizable(false);
    frame.setVisible(true);

    New.addActionListener(this);
    New.setToolTipText("Creates a new File");
    Save.addActionListener(this);
    Save.setToolTipText("Saves the current File");
    Open.addActionListener(this);
    Open.setToolTipText("Opens a file");
    changeFont.addActionListener(this);
    changeFont.setToolTipText("Change the Font");

    fontLabel.setToolTipText("Font");

    fontLabelText.setToolTipText("Set the kind of Font");
    fontSizeLabel.setToolTipText("Set the size of the Font");

    tool.add(New);
    tool.addSeparator();
    tool.add(Save);
    tool.addSeparator();
    tool.add(Open);
    tool.addSeparator();
    tool.addSeparator();
    tool.addSeparator();
    tool.add(fontLabel);
    tool.addSeparator();
    tool.add(fontLabelText);
    tool.add(fontName);
    tool.addSeparator();
    tool.add(fontSizeLabel);
    tool.add(fontSize);
    tool.addSeparator();
    tool.add(changeFont);

    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    pane.add(tool, "North");
    pane.add(scroll, "Center");
    frame.setContentPane(pane);
}

public static void main(String args[]) {
    TextEditor editor = new TextEditor();
    editor.Display();
}

public void actionPerformed(ActionEvent evt) {
    String fontNameSet;
    String fontSizeSetTemp;
    int fontSizeSet;
    Object source = evt.getSource();
    if(source == New) {
        texty.setText("");
    }
    else if(source == changeFont) {
        fontNameSet = (String) fontName.getSelectedItem();
        fontSizeSetTemp = (String) fontSize.getSelectedItem();
        fontSizeSet = Integer.parseInt(fontSizeSetTemp);
        texty.setFont(new Font(fontNameSet,  Font.PLAIN, fontSizeSet));
    }
    else if(source == Save) {
        String fileName = JOptionPane.showInputDialog("Enter the name of the file: "); 
        System.out.println(fileName);

        String path = JOptionPane.showInputDialog("Enter the path of the file: ");
        System.out.println(path);

        try {
            format = new Formatter(fileName + ".txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        format.format(texty.getText());

        format.close();
    }

}
}

如您所见,我已经实现了路径系统。

但是,我无法将文件的路径设置为用户输入的路径。

有什么办法可以做到这一点吗?

最佳答案

如果您想让用户选择保存文本文件的路径,我认为您应该使用 jFileChooser (Swing) 或 Jfiledialog (AWT)。

使用jFileChooser的示例:

//file f;  
   JFileChooser fc = new JFileChooser();
     fc.showSaveDialog(this);
      f=fc.getSelectedFile();
      if(f!=null)
    {
        try {
            PrintWriter pw = new PrintWriter(f);
            System.out.println(jTextArea1.getText());
            pw.print(jTextArea1.getText());
            pw.close();
        } catch (FileNotFoundException ex) {}
}

关于java - 设置要创建的文件的路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066504/

相关文章:

java - 检查java数据库中的用户名和密码,如果错误则给出错误密码消息

java - 是否可以*转换*多态 Hibernate/JPA 实体的类型?

powershell - 使用 PowerShell 设置特殊文件夹的位置

java - 我可以在 ant 文件集中仅包含带注释的类来使用吗?

java - Thymeleaf + Bootstrap 配置

Android-从 URI 获取文件路径?

python - 检查路径是否是 Python 2.7 中的套接字

javascript - 如何在文本编辑器中实时协作处理单个文档

text-editor - 哪些文本编辑器可以正确处理 Windows 和 Unix 样式的换行符?

c++ - 想要在 c/c++ 中以编程方式在某些文本编辑器中打开文本文件