2017-12-29 35 views
0
import java.awt.*; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import javax.swing.*; 
import javax.swing.text.*; 
import javax.swing.text.rtf.RTFEditorKit; 

public class Main extends JFrame { 
    public Main() throws Exception{ 
     setSize(400, 240); 
     JTextPane topPanel = new JTextPane(); 
     StyledDocument doc; 
     doc=topPanel.getStyledDocument(); 
     SimpleAttributeSet keyWord = new SimpleAttributeSet(); 
     topPanel.setLayout(new BorderLayout()); 
     getContentPane().add(topPanel, BorderLayout.CENTER); 
     RTFEditorKit rtf = new RTFEditorKit(); 
     JEditorPane editor = new JEditorPane(); 
     editor.setEditorKit(rtf); 
     topPanel.add(editor); 
     FileInputStream fi=new FileInputStream("test.rtf"); 
     FileOutputStream fo=new FileOutputStream("test2.rtf"); 
     keyWord.getAttribute(fi); 
     rtf.read(fi, editor.getDocument(), 0); 
     try { 
      doc.insertString(doc.getLength(), "Test\ntest2", keyWord); 
     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 
     rtf.write(fo, doc, 0, 0); 
     fo.close(); 
     fi.close(); 
    } 

    public static void main(String args[]) throws Exception{ 
     Main mainFrame = new Main(); 
     mainFrame.setVisible(true); 
    } 
} 

こんにちは! フォーマットデータの取得に関する質問があります。同じ書式設定データで新しい文字列を作成して "test2.rtf"に保存したいので、 "test.rtf"ファイルから "keyWord"オブジェクトに日付をフォーマットしたいと思います。どうやってするの?この例ではファイルからデータを読み込む

+0

具体的な問題は何ですか? – MrSmith42

+0

ファイルからデータフォーマットを取得できません。このフォーマットを新しいStringに使用したいからです。 –

+0

'test.rtf'から' test2.rtf'に 'keyWord'オブジェクト経由でテキストをコピーしますか?私はあなたの問題を理解していません。 – ToxicTV

答えて

0

List<String>は、それに取り付けられたSimpleAttributeSetを有するJTextPaneにコピーされています。

public class Stackoverflow 
{  
    public static void main(final String[] arguments) throws HeadlessException 
    { 
     final JFrame frame = new JFrame("Stackoverflow | Question"); 
     EventQueue.invokeLater(() -> 
     { 
      frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 
      frame.setSize(1280, 720); 
      frame.setLocationRelativeTo(null); 
      frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER)); 
      frame.setVisible(true); 
      final SimpleAttributeSet set = new SimpleAttributeSet(); 
      StyleConstants.setForeground(set, Color.RED); 
      final JTextPane pane = new JTextPane(); 
      pane.setPreferredSize(frame.getContentPane().getSize()); 
      final StyledDocument document = pane.getStyledDocument(); 
      try 
      { 
       int length = 0; 
       for (String string : Files.readAllLines(Paths.get("test.rtf")) 
       { 
        document.insertString(length, string, set); 
        length += string.length(); 
       } 
      } 
      catch (BadLocationException badLocationException) 
      { 
       badLocationException.printStackTrace(); 
      } 
      frame.getContentPane().add(pane); 
      frame.getContentPane().repaint(); 
     }); 
    } 
} 
関連する問題