JTextPane本身是不能完成这种工作的,它与javax.swing.text.StyledDocument接口的文档一起联合工作。所以只要从textPane中将StyledDocument类提取出来传递就可以了。
StyledEditorKit还可以提供辅助配置信息(具体参阅API Docs文档)。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Test {
JFrame frame, frame2;
JTextPane textPane,textPane2;
File file;
Icon image; public Test(){
frame = new JFrame("JTextPane");
frame2 = new JFrame("提取的内容");
textPane = new JTextPane();
textPane2 = new JTextPane();
file = new File("icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
} public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str ="\n" + str ;
try {
doc.insertString(doc.getLength(), str, attrSet);
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet, true);
}//字体类型
StyleConstants.setFontSize(attrSet, fontSize);
//字体大小
insert(str, attrSet);
} public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字",Color.red,false,20);
setDocs("第二行的文字",Color.BLACK,true,25);
setDocs("第三行的文字",Color.BLUE,false,20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
frame2.getContentPane().add(textPane2, BorderLayout.CENTER);
frame.setSize(200,300);
frame.setVisible(true);
frame2.setLocation(200, 0);
frame2.setSize(200,300);
frame2.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
StyledDocument docs = test.textPane.getStyledDocument();//取得textPane中的StyledDocument类文档
test.textPane2.setStyledDocument(docs);//将StyledDocument类文档传给textPane2
}
}
<------ 树欲静而风不止 ------>
StyledEditorKit还可以提供辅助配置信息(具体参阅API Docs文档)。import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
import java.io.*;public class Test {
JFrame frame, frame2;
JTextPane textPane,textPane2;
File file;
Icon image; public Test(){
frame = new JFrame("JTextPane");
frame2 = new JFrame("提取的内容");
textPane = new JTextPane();
textPane2 = new JTextPane();
file = new File("icon.gif");
image = new ImageIcon(file.getAbsoluteFile().toString());
} public void insert(String str, AttributeSet attrSet) {
Document doc = textPane.getDocument();
str ="\n" + str ;
try {
doc.insertString(doc.getLength(), str, attrSet);
}
catch (BadLocationException e) {
System.out.println("BadLocationException: " + e);
}
} public void setDocs(String str,Color col,boolean bold,int fontSize) {
SimpleAttributeSet attrSet = new SimpleAttributeSet();
StyleConstants.setForeground(attrSet, col);
//颜色
if(bold==true){
StyleConstants.setBold(attrSet, true);
}//字体类型
StyleConstants.setFontSize(attrSet, fontSize);
//字体大小
insert(str, attrSet);
} public void gui() {
textPane.insertIcon(image);
setDocs("第一行的文字",Color.red,false,20);
setDocs("第二行的文字",Color.BLACK,true,25);
setDocs("第三行的文字",Color.BLUE,false,20);
frame.getContentPane().add(textPane, BorderLayout.CENTER);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}});
frame2.getContentPane().add(textPane2, BorderLayout.CENTER);
frame.setSize(200,300);
frame.setVisible(true);
frame2.setLocation(200, 0);
frame2.setSize(200,300);
frame2.setVisible(true);
}
public static void main(String[] args) {
Test test = new Test();
test.gui();
StyledDocument docs = test.textPane.getStyledDocument();//取得textPane中的StyledDocument类文档
test.textPane2.setStyledDocument(docs);//将StyledDocument类文档传给textPane2
}
}
<------ 树欲静而风不止 ------>
也可以发去我E-MAIL:[email protected] 收到立即给你分 好吗?
<------ 树欲静而风不止 ------>
<------ 树欲静而风不止 ------>