Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Khác biệt giữa JTextPane và JEditorPane trong Java

JTextPane mở rộng JEditorPane cung cấp tính năng xử lý văn bản như font, style, màu sắc. Nếu cần xử lý văn bản nặng thì dùng lớp này, trong khi JEditorPane hỗ trợ hiển thị/chỉnh sửa HTML và RTF và có thể mở rộng bằng EditorKit tùy chỉnh.

JTextPane

  • JTextPane là subclass của JEditorPane.
  • Dùng cho document có style với nhúng hình ảnh và component.
  • Component văn bản có thể markup bằng thuộc tính biểu diễn đồ họa, dùng DefaultStyledDocument làm model mặc định.
  • Phương thức quan trọng: addStyle(), getCharacterAttributes(), getStyledDocument(), setDocument(), setEditorKit(), setStyledDocument().

Ví dụ JTextPane

import java.awt.*;
import javax.swing.*;
import javax.swing.text.*;

public class JTextPaneTest {
    public static void main(String args[]) throws BadLocationException {
        JFrame frame = new JFrame("JTextPane Test");
        Container cp = frame.getContentPane();
        JTextPane pane = new JTextPane();
        SimpleAttributeSet set = new SimpleAttributeSet();
        StyleConstants.setBold(set, true);
        pane.setCharacterAttributes(set, true);
        pane.setText("Welcome to");
        set = new SimpleAttributeSet();
        StyleConstants.setItalic(set, true);
        StyleConstants.setForeground(set, Color.blue);
        Document doc = pane.getStyledDocument();
        doc.insertString(doc.getLength(), " Tutorials ", set);
        set = new SimpleAttributeSet();
        StyleConstants.setFontSize(set, 20);
        doc.insertString(doc.getLength(), " Point", set);
        JScrollPane scrollPane = new JScrollPane(pane);
        cp.add(scrollPane, BorderLayout.CENTER);
        frame.setSize(375, 250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

Kết quả

Khác biệt giữa JTextPane và JEditorPane trong Java

JEditorPane

  • JEditorPane là loại text area hiển thị nhiều định dạng văn bản.
  • Mặc định hỗ trợ HTML và RTF (Rich Text Format), có thể xây EditorKit riêng cho loại nội dung cụ thể.
  • Dùng setContentType() chọn document muốn hiển thị và setEditorKit() thiết lập editor tùy chỉnh cho JEditorPane.

Ví dụ JEditorPane

import javax.swing.*;

public class JEditorPaneTest extends JFrame {
    public JEditorPaneTest() {
        setTitle("JEditorPane Test");
        JEditorPane editorPane = new JEditorPane();
        editorPane.setContentType("text/html");
        editorPane.setText("

Java

is a general-purpose computer programming language that is concurrent, class-based, object-oriented, and specifically designed to have as few implementation dependencies as possible.

"); setSize(350, 275); setContentPane(editorPane); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLocationRelativeTo(null); setVisible(true); } public static void main(String[] a) { new JEditorPaneTest(); } }

Kết quả

Khác biệt giữa JTextPane và JEditorPane trong Java