Java by API/javax.swing.text.rtf/RTFEditorKit

Материал из Java эксперт
Перейти к: навигация, поиск

new RTFEditorKit()

  
import java.awt.BorderLayout;
import java.io.FileInputStream;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.rtf.RTFEditorKit;
public class Main extends JFrame {
  public Main() throws Exception{
    setSize(400, 240);
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);
    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);
    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);
    FileInputStream fi = new FileInputStream("test.rtf");
    rtf.read(fi, editor.getDocument(), 0);
  }
  public static void main(String args[]) throws Exception{
    Main mainFrame = new Main();
    mainFrame.setVisible(true);
  }
}





RTFEditorKit: read(InputStream in, Document doc, int pos)

  
import java.awt.BorderLayout;
import java.io.FileInputStream;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.text.rtf.RTFEditorKit;
public class Main extends JFrame {
  public Main() throws Exception{
    setSize(400, 240);
    JPanel topPanel = new JPanel();
    topPanel.setLayout(new BorderLayout());
    getContentPane().add(topPanel, BorderLayout.CENTER);
    RTFEditorKit rtf = new RTFEditorKit();
    JEditorPane editor = new JEditorPane();
    editor.setEditorKit(rtf);
    JScrollPane scroller = new JScrollPane();
    scroller.getViewport().add(editor);
    topPanel.add(scroller, BorderLayout.CENTER);
    FileInputStream fi = new FileInputStream("test.rtf");
    rtf.read(fi, editor.getDocument(), 0);
  }
  public static void main(String args[]) throws Exception{
    Main mainFrame = new Main();
    mainFrame.setVisible(true);
  }
}