swing中图文共存的问题

问题描述:

 我现在有个需求 在 JTextPane(或者别的组件)录入文字和图片(n张图片)然后把内容存到数据库

在别的界面从数据库中读出然后再用JTextPane(或者别的组件)显示
一开始的思路是提交的时候把JTextPane的内容转换为自定义的格式.其中的图片转化成[img id=xx]这样的
然后把图片存放到一张图片表中.与主表的内容关联.在读取的时候正则匹配然后利用JTextPane的insertIcon或者别的方法
插入图片到文本中.但是这种思路很麻烦.在插入图片的时候位置的偏移还有很多别的问题搞得我心烦不已.
请教高人解法..最好有关键代码.我问过N多人.思路都会说..代码没有.我自己也会想.........
再次感谢

帖一段我现在写的代码.把doc对象替换成包含ubb标签的文本,代码还有问题.如果能帮我看下更好.主要问题插入图片空格问题.还有换行处理的问题
[code="java"]
Map images = new HashMap();
StyledDocument doc = textPane_input.getStyledDocument();
int imgNum = 0;
if (doc.getLength() != 0) {
log.info("获得默认的根元素:" + doc.getDefaultRootElement());
int total = textPane_input.getStyledDocument().getRootElements()[0].getElementCount();
for (int p = 0; p < total; p++) {
int num = textPane_input.getStyledDocument().getRootElements()[0].getElement(p).getElementCount();
for (int i = 0; i < num; i++) {
log.info("检测第:" + (p + 1) + "段落下第" + (i + 1) + "个节点");
//取每个节点转换成Icon
Icon icon = StyleConstants.getIcon(doc.getRootElements()[0].getElement(p).getElement(i).getAttributes());
if (icon != null) {//转换成功
log.info("--------------发现图片---------------");
String id = UUID.randomUUID().toString();
int location = doc.getRootElements()[0].getElement(p).getElement(i).getStartOffset();
log.info("图片生成id:" + id + "图片路径:" + icon);
log.info("图片在文档中位置:" + location);
images.put(imgNum, new TextImage(location, id, icon.toString()));
imgNum++;
} else {
log.info("--------------未发现图片---------------");
}

            }
        }
    }
    log.info("--------------开始处理文档---------------");
    Document d = new DefaultStyledDocument();
    try {
        log.info("--------------插入字符串:" + textPane_input.getText() + "---------------");
        d.insertString(0, textPane_input.getText(), null);
    } catch (BadLocationException ex) {
        log.error("insert error", ex);
    }
    log.info("--------------开始处理文档中的图片---------------");
    int size = 0;
    for (Map.Entry<Integer, TextImage> one : images.entrySet()) {
        log.info("--------------插入第" + (size + 1) + "张图片---------------");
        TextImage t = one.getValue();
        try {
            log.info("未插入前的内容是:" + d.getText(0, d.getLength()));
            log.info("要插入的内容长度:" + d.getLength());
        } catch (BadLocationException ex) {
            log.error("getText error", ex);
        }
        String content = "[img id=" + t.getImageName() + "]";
        log.info("要插入的内容:" + content);
        int location = t.getLocation() + size * (content.length());
        log.info("获取插入位置:" + location);
        try {
            d.insertString(location, content, null);
            log.info("插入图片后的内容是:" + d.getText(0, d.getLength()));
        } catch (BadLocationException ex) {
            log.error("insert error", ex);
        }
        size++;
    }
    for (int i = 0; i < textPane_input.getText().length(); i++) {
        if (textPane_input.getStyledDocument().getCharacterElement(i).getName().equals("icon")) {
            log.info("你在第 " + i + " 位置插入了图片");
        }
    }
    log.info("map中的内容:" + images);
    String out = "";
    try {
        out = d.getText(0, d.getLength());
        log.info("获取的内容是:" + out);
        log.info("获取的内容长度是:" + out.length());
    } catch (BadLocationException ex) {
        log.error("getText error", ex);
    }
    String regEx = "\\[img id=(.{36})\\]";
    String[] all = out.split(regEx);
    log.info("数组中内容:" + Arrays.toString(all));
    Document outDoc = new DefaultStyledDocument();
    for (int i = 0; i < all.length; i++) {
        try {
            outDoc.insertString(outDoc.getLength(), all[i], null);
        } catch (BadLocationException ex) {
            log.error("Insert error", ex);
        }
    }
    textPane_output.setDocument(outDoc);
    int i = 0;
    for (Map.Entry<Integer, TextImage> one : images.entrySet()) {
        log.info("--------------插入第" + (i + 1) + "张图片---------------");
        TextImage t = one.getValue();
        Style style = textPane_output.addStyle("StyleName", null);

// ImageIcon icon = ImageHelper.loadImage((i+1)+".png");
String url = images.get(i).getUrl();
ImageIcon icon = new ImageIcon(url);
StyleConstants.setIcon(style, icon);
textPane_output.setCaretPosition(t.getLocation() + 1 * i);
textPane_output.insertIcon(icon);
i++;
}
}
[/code]

一个Doc,就是一个树状结构的东西。建议你自学一下DOM相关知识。

你现在的搞法虽然也许可行,但你每次改界面,这部分都要改。非常麻烦。而且容易出错。
我给你提个简单的办法吧。
直接在JTextPane中load HTML代码。这样存取非常方便。

[quote]我也想过这种方式.首先一点.我需要把JTextPane中get的doc对象转换成html文档.
不知道可行不.还有.你的这种方式是把每一道题都转化成一个html文件吗?然后存放到本地?那样太占用空间了[/quote]

是的,一个doc就是一个html文件。更标准化的,你可以用xml.

占空间是什么意思?html文件就是些字符,很小啊。你现在在做的相当于山寨版html