2017-06-21 4 views
0

これは可能かどうかわかりませんが、私がしたいのは、.docファイルにスタイル付きドキュメント(ユーザーはテキストを太字、下線、斜体、3サイズのフォントに変更できます)を保存することです彼は後で、スタイル付きのテキストをサポートする他のテキストエディタで自分自身で開くことができます。TextPaneからStyledDocumentを.docファイルに保存するにはどうすればよいですか?

私は以下のコードを書いています...エディタが動作しますが、テキストにスタイルを適用できますが、保存するとスタイルがなく黒いテキストが保存されます。私はどこに問題があるのか​​分かりません。アクションが保存されない可能性があります。私は作家とバッファリングされた作家と一緒に試みましたが、うまくいきませんでした。また、HTMLエディタキットを使用しようとしましたが、全く動作しませんでした。空のドキュメントが保存されました。

多分どのように私はスタイルを保存することができますか?助けに感謝:)

public class EditFrame extends javax.swing.JFrame { 

JFrame frameEdit = this; 
File file; //A file I would like to save to -> fileName.doc 
StyledDocument doc; 
HashMap<Object, Action> actions; 
StyledEditorKit kit; 

public EditFrame() { 
    super(); 
    initComponents(); 
    JMenu editMenu = createEditMenu(); 
} 

protected JMenu createEditMenu() { 
    JMenu menu = editMenu; 

    Action action = new StyledEditorKit.BoldAction(); 
    action.putValue(Action.NAME, "Bold"); 
    menu.add(action); 

    action = new StyledEditorKit.ItalicAction(); 
    action.putValue(Action.NAME, "Italic"); 
    menu.add(action); 

    //... 

    return menu; 
} 

//I'm guessing this doesn't work correctly too (doesn't read styles), but this is another subject :) 
public void readFile(File f) { 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f), "windows-1250")); 
     textPane.read(reader, null); 
     textPane.requestFocus(); 
    } catch (IOException ex) { 
     Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 
} 

//SAVE METHOD 
private void save(java.awt.event.ActionEvent evt) {      
    try { 
     BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file)); 
     kit = (StyledEditorKit) textPane.getEditorKit(); 
     doc = (StyledDocument) textPane.getDocument(); 
     kit.write(out, doc, 0, doc.getLength()); 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (UnsupportedEncodingException | BadLocationException ex) { 
     Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(EditFrame.class.getName()).log(Level.SEVERE, null, ex); 
    } 
}      

public static void main(String args[]) { 
    java.awt.EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      new EditFrame().setVisible(true); 
     } 
    }); 
} 
} 

答えて

2

あなたはRich Text Format(RTF)をサポートする、RTFEditorKitを使用することができます。 MS Wordを含む多くのワープロは、この形式で動作します。 write()を「OutputStream」に貼り付けると、「この種のコンテンツハンドラに適した形式」が書き込まれます。 Writerを使用するもう1つは、「指定されたストリームにプレーンテキストとして」書き込みます。

なぜStyledEditorKitが機能しないのですか?

StyledEditorKitDefaultEditorKitからそのwrite()実装、取得する "プレーンテキストとしてテキストを扱います。" StyledEditorKitはスタイル付きテキストを内部的に保存しますが、外部フォーマットについては認識しません。デフォルトのwrite()を取得するには、サブクラスHTMLEditorKitまたはRTFEditorKitのいずれかに移動する必要があります。オーバーライドされたメソッドは、内部形式をRTFのような外部形式に変換する方法を知っています。

+0

本当に別の方法(この場合はRTF形式)を使用する方法はありませんか? StyledEditorKitが機能しないのはなぜですか?私はちょうど正しい方法で何かを保存していないと思う... – Ves

+0

私はそれが設計されている方法だと思います。私は上記で説明しようとした。 –

+0

カタリナ島の説明に感謝します。まずはHTMLで試してみて、問題があるかどうかを教えてください。 – Ves

関連する問題