iteye_14756 2010-04-09 15:54
浏览 259
已采纳

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]

  • 写回答

3条回答 默认 最新

  • JavaHero080 2010-04-19 13:42
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器