Java 如何从JtextPane获取样式?

Java 如何从JtextPane获取样式?,java,swing,attributes,styles,jtextpane,Java,Swing,Attributes,Styles,Jtextpane,我有一个带有格式化文本的JtextPane。我需要复制完整的样式和属性 从该文本将其传输到另一个JtextPane。是否有示例或代码片段 看看它是如何工作的 好的,这是我找到的一个代码,我做了一点修改: import java.awt.*; import javax.swing.*; import javax.swing.text.*; public class Main { private JTextPane textPane1; private JTextPane textPane2;

我有一个带有格式化文本的JtextPane。我需要复制完整的样式和属性 从该文本将其传输到另一个JtextPane。是否有示例或代码片段 看看它是如何工作的

好的,这是我找到的一个代码,我做了一点修改:

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();     
}
}
import java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共班机{
私有JTextPane textPane1;
私有JTextPane textPane2;
私人文件doc1;
私人文件doc2;
私有JFrame框架1;
私有JFrame框架2;
私有可变属性集黑色;
私人可变属性设置红色;
私有属性集合属性;
公用干管(){
textPane1=新的JTextPane();
黑色=新的SimpleAttributeSet();
红色=新的SimpleAttributeSet();
设置前景(黑色,彩色,黑色);
设置前景(红色,彩色,红色);
textPane1.setEditorKit(新的StyledEditorKit());
doc1=textPane1.getDocument();
附录1(“这是一项测试!\n”);
//设置颜色=红色
属性=红色;
附录1(“你好,世界!你好”\n”);
//设置颜色=黑色
属性=黑色;
附录1(“文本再次为黑色\n”);
StyledDocument StyledDocument=textPane1.getStyledDocument();
textPane2=新的JTextPane();
textPane2.setEditorKit(新的StyledEditorKit());
doc2=textPane2.getDocument();
String text=textPane1.getText();
附录2(文本);
//将frame1中文本的格式数据传输到frame2
int docLength=doc1.getLength();
元素;
属性集属性集;
对于(inti=0;i我已经解决了它


导入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();     
}
}
import javax.swing.*;
导入javax.swing.text.*;
公共班机{
私有JTextPane textPane1;
私有JTextPane textPane2;
私人文件doc1;
私人文件doc2;
私有JFrame框架1;
私有JFrame框架2;
私有可变属性集黑色;
私人可变属性设置红色;
私有属性集合属性;
public Main()引发BadLocationException{
textPane1=新的JTextPane();
黑色=新的SimpleAttributeSet();
红色=新的SimpleAttributeSet();
设置前景(黑色,彩色,黑色);
设置前景(红色,彩色,红色);
textPane1.setEditorKit(新的StyledEditorKit());
doc1=textPane1.getDocument();
附录1(“这是一项测试!\n”);
//设置颜色=红色
属性=红色;
附录1(“你好,世界!你好”\n”);
//设置颜色=黑色
属性=黑色;
附录1(“文本再次为黑色\n”);
//重要部分:样式文本中每个字符的属性将
//被转移到第二个文本面板
StyledDocument StyledDocument=textPane1.getStyledDocument();
元素;
textPane2=新的JTextPane();
textPane2.setEditorKit(新的StyledEditorKit());
doc2=textPane2.getDocument();

对于(int i=0;i我想对此解决方案进行一些改进。getParagraphElement(int-pos)可以捕获具有相同样式的文本,而不是捕获每个字符。第一个元素从pos 0开始,然后是下一个:element.getEndOffset()+1

但是我们必须遍历子元素树以获得所有角色属性

package demo;

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

public class TextPaneDemo {

  private JTextPane textPane1;
  private JTextPane textPane2;
  private StyledDocument doc1;
  private StyledDocument doc2;
  Private JFrame frame1;
  private JFrame frame2;


  public TextPaneDemo () {

    MutableAttributeSet black = new SimpleAttributeSet();
    StyleConstants.setForeground(black, Color.black);
    MutableAttributeSet red = new SimpleAttributeSet();
    StyleConstants.setForeground(red, Color.red);
    MutableAttributeSet blue = new SimpleAttributeSet();
    StyleConstants.setForeground(blue, Color.blue);
    StyleConstants.setBold(blue, true);

    textPane1 = new JTextPane();
    textPane1.setEditorKit(new StyledEditorKit());                    
    doc1 = textPane1.getStyledDocument();       

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

    createFrames();

    append(doc1, black, "This is a Test!\n");
    append(doc1, red,   "Hello world! Hello Stackoverflow\n");        
    append(doc1, blue,  "Hello ");        
    append(doc1, red,   "Stackoverflow\n");        
    append(doc1, black, "the text is black again\n");     

    copyDoc (doc1, doc1.getParagraphElement(0), doc2);
  }

  private static void append(StyledDocument doc, AttributeSet style, String text) {
    try {
        doc.insertString(doc.getLength(), text, style);
    } catch (BadLocationException ex) {
        ex.printStackTrace();
    }
  }

  private static int copyDoc(StyledDocument doc1, Element elem, StyledDocument doc2)  {
    int pos = elem.getStartOffset();
    while( pos < doc1.getLength()) {       
      Element element = doc1.getParagraphElement(pos);
      pos = copyElem(doc1, element, doc2);
    }
    return pos;
  }

  private static int copyElem(StyledDocument doc1, Element elem, StyledDocument doc2)  {
    int pos = elem.getStartOffset();
    if (elem.getElementCount() <= 1) {
      int length = elem.getEndOffset() - elem.getStartOffset();
      AttributeSet attribSet = elem.getElementCount() == 0 ?
         elem.getAttributes() : 
         elem.getElement(0).getAttributes();
      try {
        String text = doc1.getText(elem.getStartOffset(), length);
        append(doc2, attribSet, text);
      } catch(BadLocationException e) {      }
      pos = elem.getEndOffset();
    } else {
      for (int sub = 0; sub < elem.getElementCount(); sub++) {
         Element sElem = elem.getElement(sub);
         pos = copyElem(doc1, sElem, doc2);
      }
      pos = elem.getEndOffset();
    }
    return pos;
  }

  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(frame1.getLocation().x + frame1.getWidth(), frame1.getLocation().y);
    frame2.getContentPane().add(new JScrollPane(textPane2), BorderLayout.CENTER);
    frame2.setVisible(true);
  }

  public static void main(String args[]) {
    new TextPaneDemo();
  }
}
软件包演示;
导入java.awt.*;
导入javax.swing.*;
导入javax.swing.text.*;
公共类TextPaneDemo{
私有JTextPane textPane1;
私有JTextPane textPane2;
私人风格文件doc1;
私人风格文件doc2;
私有JFrame框架1;
私有JFrame框架2;
public TextPaneDemo(){
MutableAttributeSet black=新的SimpleAttributeSet();
设置前景(黑色,彩色,黑色);
MutableAttributeSet red=新的SimpleAttributeSet();
设置前景(红色,彩色,红色);
MutableAttributeSet blue=新的SimpleAttributeSet();
setForeground(蓝色,Color.blue);
StyleConstants.setBold(蓝色,true);
textPane1=新的JTextPane();
textPane1.setEditorKit(新的StyledEditorKit());
doc1=textPane1.getStyledDocument();
textPane2=新的JTextPane();
textPane2.setEditorKit(新的StyledEditorKit());
doc2=textPane2.getStyledDocument();
createFrames();
追加(doc1,黑色,“这是一个测试!\n”);
追加(doc1,红色,“你好世界!你好堆栈溢出”\n);
附加(doc1,蓝色,“Hello”);
追加(doc1,红色,“Stackoverflow\n”);
追加(doc1,黑色,“文本再次为黑色\n”);
copyDoc(doc1,doc1.getParagraphElement(0),doc2);
}
私有静态void append(样式文档文档、属性集样式、字符串文本){
试一试{
doc.insertString(doc.getLength(),文本,样式);
}捕获(BadLocationException ex){
例如printStackTrace();
}
}
私有静态int copyDoc(样式文档doc1、元素元素、样式文档doc2){
int pos=elem.getStartOffset();
而(pos如果(elem.getElementCount())看一下这里:@Anto:也许有一个例子……我对Java比较陌生。这个问题似乎离题了,因为它是关于OP请求一个代码样本,而不是实际花费精力来解决这个问题。@Andrew:我花了时间。现在它工作了,请看我的回答谢谢!正是我想要的!