JTextPane的问题求救

yuziyu 2005-05-20 06:19:59
实现类似QQ的聊天对话框,有两个JTextPane
按发送按钮把下面一个JTextPane的内容(包括文字+图片)复制到上面一个Pane里面去
怎么实现?
求教高手……
...全文
119 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhudonhua 2005-05-22
  • 打赏
  • 举报
回复
楼主一定要自己写,不然等同于纸上谈兵,学不到东西的
yuziyu 2005-05-22
  • 打赏
  • 举报
回复
每日一顶
naner_china 2005-05-21
  • 打赏
  • 举报
回复
http://search.csdn.net/Expert/topic/732/732956.xml?temp=.8993189
viewtifuljoe 2005-05-21
  • 打赏
  • 举报
回复
留名
cyicecream 2005-05-21
  • 打赏
  • 举报
回复
呵呵,楼主要举一反三啊,直接给你个可用的,自己不想学不到知识的
yuziyu 2005-05-21
  • 打赏
  • 举报
回复
crazer2003 2005-05-21
  • 打赏
  • 举报
回复
呵呵 在这里又看见楼主拉!看来学校里高手还是少拉点
yuziyu 2005-05-21
  • 打赏
  • 举报
回复
每日一顶
mq612 2005-05-20
  • 打赏
  • 举报
回复
这只是要告诉你JTextPane中的内容变化是document在起作用,想要插入新的内容就需要操纵document来实现,同样也得学会如何从document中提取元素。
yuziyu 2005-05-20
  • 打赏
  • 举报
回复
to: mq612(理想)
你把第一个的文档提取给第二个,这样的话两个pane就共用一个document了,不是复制内容啊
这样的话改两个pane 等于成为一个一模一样的pane了啊
mq612 2005-05-20
  • 打赏
  • 举报
回复
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("./classes/test/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
}
}

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧