【问题标题】:Saving the font from a text pane java从文本窗格java保存字体
【发布时间】:2016-09-02 14:25:01
【问题描述】:

我正在制作一个文本编辑器作为一个辅助项目,到目前为止,我正在努力保存和加载文本的字体字母、颜色和装饰;换句话说,我只能保存纯文本。我的问题是,如何保存和加载所有这些?

private void Edit() {//This is the method where the program edits the text
    StyledDocument doc = this.tpText.getStyledDocument();
    Style estilo = this.tpText.addStyle("miEstilo", null);
    StyleConstants.setForeground(estilo, colour); //Color
    StyleConstants.setFontFamily(estilo, fontLetter);//Letter
    StyleConstants.setFontSize(estilo, size);//Size
    StyleConstants.setBold(estilo, bold);//Bold
    StyleConstants.setItalic(estilo, italics);//Italics
    StyleConstants.setUnderline(estilo, underline);//Underline
    doc.setCharacterAttributes(this.tpText.getSelectionStart(), this.tpText.getSelectionEnd() - this.tpText.getSelectionStart(), this.tpText.getStyle("miEstilo"), true);//The only text that will be edited is the one that the user highlights
}

private void comboxFontsActionPerformed(java.awt.event.ActionEvent evt) {//This is one of the methods in which the program edits the text                                            
        this.fontLetter = (String) this.comboxFonts.getSelectedItem();
        Edit();
}
private void mibtnSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//Save as... menu item button                                            
        int saveResult = fileSelect.showSaveDialog(null);
            if (saveResult == fileSelect.APPROVE_OPTION) {
                saveFile(fileSelect.getSelectedFile(), this.tpText.getText());
                this.mibtnSave.setEnabled(true);
            }
    }
public void saveFile(File file, String contents) {//Save File
    BufferedWriter writer = null;
    String filePath = file.getPath();

    try {
        writer = new BufferedWriter(new FileWriter(filePath));
        writer.write(contents);
        writer.close();
        this.tpText.setText(contents);
        currentFile = file;
    } catch (Exception e) {

    }
}

public void openFile(File file) {//Load File
    if (file.canRead()) {

        String filePath = file.getPath();
        String fileContents = "";

        if (filePath.endsWith(".txt")) {
            try {
                Scanner sc = new Scanner(new FileInputStream(file));
                while (sc.hasNextLine()) {
                    fileContents += sc.nextLine();
                }

                sc.close();
            } catch (FileNotFoundException e) {

            }
            this.tpText.setText(fileContents);
            currentFile = file;
        } else {
            JOptionPane.showMessageDialog(null, "Only .txt files are supported.");

        }
    } else {
        JOptionPane.showMessageDialog(null, "Could not open file...");
    }
}

【问题讨论】:

    标签: java text netbeans fonts text-editor


    【解决方案1】:

    我不知道我的问题是否正确,但如果您尝试保存纯文本以外的样式,您可以执行以下操作。
    将所有样式属性保存在文本文件旁边的另一个文件中,您可以在打开文本文件时恢复它。

    【讨论】:

    • 我也在考虑这个问题,但问题是:假设文本是“hello world”,“hello”部分是粗体,而“world”是斜体。 "hello world" 我想知道如何保存和加载这样的文本。
    • 好的,我的朋友,让我向您推荐一些东西。当任何用户尝试制作文本样式时,将该样式保存在字符串中,然后将其写入保存的纯文本旁边的文件中。示例:当单击粗体或斜体按钮以使 hello 粗体和 world 斜体时,您的字符串应类似于:“line X col Y word 'hello' bold” 其中X and Y 是文本中单词的位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-29
    • 2017-01-04
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 2015-11-05
    相关资源
    最近更新 更多