本文介绍了如何从的JTextPane获取样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有格式的文本JTextPane中。我需要复制完整的风格和属性
从这个文本传输到其他的JTextPane。有一个例子或code片段
看看它是如何工作的?

I have a JtextPane with formatted text. I need to copy the complete style and attributesfrom this text to transfer it to another JtextPane. Is there an example or code snippet to see how it works?

好吧,这里是一个code,我发现,我已经改变了一点点:

ok, here is a code I found and I've changed a little bit:

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

public class Main  {

private JTextPane textPane1;
private JTextPane textPane2;
private Document doc1;
private Document doc2;
private JFrame frame1;
private JFrame frame2;

private MutableAttributeSet black;    
private MutableAttributeSet red;
private AttributeSet attribute;    

public Main() {
    textPane1 = new JTextPane();

    black = new SimpleAttributeSet();
    red = new SimpleAttributeSet();
    StyleConstants.setForeground(black, Color.black);
    StyleConstants.setForeground(red, Color.red);
    textPane1.setEditorKit(new StyledEditorKit());                    
    doc1 = textPane1.getDocument();       

    append1("This is a Test!\n");

    //set color = red
    attribute = red;
    append1("Hello world! Hello Stackoverflow\n");        

    //set color = black
    attribute = black;
    append1("the text is black again\n");     

    StyledDocument styledDocument = textPane1.getStyledDocument();

    textPane2 = new JTextPane();
    textPane2.setEditorKit(new StyledEditorKit());
    doc2 = textPane2.getDocument();
    String text = textPane1.getText();
    append2(text);

    //transfer format data of text in frame1 to frame2
    int docLength = doc1.getLength();
    Element element;        
    AttributeSet attribSet;
    for(int i=0;i<docLength;i++) {            
        element = styledDocument.getCharacterElement(i);            
        attribSet = element.getAttributes();            
        StyleConstants.setForeground(red, Color.red);
    }

    createFrames();        
}

private void append1(String text){
    try {
        doc1.insertString(doc1.getLength(), text, attribute);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

private void append2(String text) {
    try {
        doc2.insertString(doc2.getLength(), text, attribute);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

private void createFrames() {
    frame1 = new JFrame("frame 1");     
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(400, 300);
    frame1.setLocationRelativeTo(null);
    frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
    frame1.setVisible(true);

    frame2 = new JFrame("frame 2");
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(400, 300);
    frame2.setLocation(300,0);
    frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
    frame2.setVisible(true);
}

public static void main(String args[]) {
    new Main();     
}
}

我也想传输格式样式和属性第二帧。

I also want to transfer format styles and attributes to the second frame.

推荐答案

我已经解决了。

结果
    进口的java.awt。*;


import java.awt.*;

import javax.swing.*;
import javax.swing.text.*;

public class Main  {

private JTextPane textPane1;
private JTextPane textPane2;
private Document doc1;
private Document doc2;
private JFrame frame1;
private JFrame frame2;

private MutableAttributeSet black;    
private MutableAttributeSet red;
private AttributeSet attribute;    

public Main() throws BadLocationException {
    textPane1 = new JTextPane();

    black = new SimpleAttributeSet();
    red = new SimpleAttributeSet();
    StyleConstants.setForeground(black, Color.black);
    StyleConstants.setForeground(red, Color.red);
    textPane1.setEditorKit(new StyledEditorKit());                    
    doc1 = textPane1.getDocument();       

    append1("This is a Test!\n");

    //set color = red
    attribute = red;
    append1("Hello world! Hello Stackoverflow\n");        

    //set color = black
    attribute = black;
    append1("the text is black again\n");     


    //IMPORTANT PART: attribute of each character from the styled text will 
    //be transfered to the second textpanel
    StyledDocument styledDocument = textPane1.getStyledDocument();   
    Element element; 

    textPane2 = new JTextPane();
    textPane2.setEditorKit(new StyledEditorKit());

    doc2 = textPane2.getDocument(); 
    for(int i=0; i<styledDocument.getLength();i++) {
        element = styledDocument.getCharacterElement(i);
        AttributeSet attributeNew = element.getAttributes();   
        System.out.println(i);
        append2(styledDocument.getText(i, 1), attributeNew);    
    }

    createFrames();        
}

private void append1(String text){
    try {
        doc1.insertString(doc1.getLength(), text, attribute);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

private void append2(String text, AttributeSet attributeNew) {
    try {
        doc2.insertString(doc2.getLength(), text, attributeNew);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
}

private void createFrames() {
    frame1 = new JFrame("frame 1");     
    frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame1.setSize(400, 300);
    frame1.setLocationRelativeTo(null);
    frame1.getContentPane().add(new JScrollPane(textPane1), BorderLayout.CENTER);
    frame1.setVisible(true);

    frame2 = new JFrame("frame 2");
    frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame2.setSize(400, 300);
    frame2.setLocation(300,0);
    frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
    frame2.setVisible(true);
}

public static void main(String args[]) throws BadLocationException {
    new Main();     
}
}

这篇关于如何从的JTextPane获取样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 10:11