Java - 如何在JTextPane中将html标签的大小修改为其内容?

时间:2022-05-04 11:44:10

I have a JTextPane, with html content :

我有一个带有html内容的JTextPane:

    Pane = new JTextPane();
    Pane.setEditable(false);
    Pane.setContentType("text/html");
    Pane.setEditorKit(kit);
    Pane.setText("<html><body><div id='content'></div></body></html>");
    doc = (HTMLDocument) Pane.getStyledDocument();
    content = doc.getElement("content");

I add my html element in runtime with :

我在运行时添加我的html元素:

doc.insertBeforeEnd(content, "<p class = 'text'>"+text+"</p>"); // text is input from user

here is CSS that I use :

这是我使用的CSS:

    styleSheet = doc.getStyleSheet();
    styleSheet.addRule("body{background-color:#FFFFFF;}");
    styleSheet.addRule("#content {margin: 0% 10%; width : 80%; }");
    styleSheet.addRule("img {height : 10px; width : 10px;  }");
    styleSheet.addRule(".text{display: inline-block;background-color:#E2EAF3;margin : 2px;padding : 1px 10px;; border-radius: 4px;border-width: 1px;border-style: solid;border-color: #D5D3CD;text-align: right;clear: both;float: right;}");

I know that the float,clear,... property not rendered yet. (Class CSS)

我知道浮动,清除,...属性还没有呈现。 (CSS类)

because of limited support of CSS property, all the <p> elements that I insert have the same length, but I want to set max size for the width, so if the text size is longer it wraps, and if it is shorter then the background fixed to its content.

由于CSS属性的支持有限,我插入的所有

元素都具有相同的长度,但是我想为宽度设置最大大小,所以如果文本大小更长,它会包装,如果它更短,那么背景固定其内容。

Untitled1 in img.rar is the screenshot.

img.rar中的Untitled1是截图。

EDIT1:

I add Pane to the JPanel with BorderLayout.

我使用BorderLayout将Pane添加到JPanel。

panel = new JPanel(new BorderLayout());

EDIT2:

I change my html code for inserting text to following :

我更改了我的html代码,用于将文本插入以下内容:

doc.insertBeforeEnd(content, "<table class = 'text'><tr><td>"+text+"</td></tr></table>");

now it fix to content (Untitled2 in img.rar) but I want to set max width, and put tables in center of html.

现在它修复了内容(img.rar中的Untitled2),但我想设置最大宽度,并将表放在html的中心。

screenshot's pictures

1 个解决方案

#1


0  

There are 2 part of the problem.

问题有2部分。

The first one how to define the width property and how to get it from model Element. Try to investigate the HTMLDocument tree Elements and try to get the 400.

第一个如何定义width属性以及如何从模型Element获取它。尝试调查HTMLDocument树元素并尝试获取400。

The second part is relatively easier. If you can read the width setting from the Element (either from attributes or from style) you can override javax.swing.text.html.ParagraphView in the HTMLEditorKit's ViewFactory and return 400 (the width prom property/style/attribte) for getPreferredSpan/getMinumumSpan/getMaximumSpan and X_AXIS

第二部分相对容易一些。如果你可以从Element中读取宽度设置(来自属性或样式),你可以覆盖HTMLEditorKit的ViewFactory中的javax.swing.text.html.ParagraphView并返回getPreferredSpan /的400(宽度prom属性/ style / attribte) getMinumumSpan / getMaximumSpan和X_AXIS

#1


0  

There are 2 part of the problem.

问题有2部分。

The first one how to define the width property and how to get it from model Element. Try to investigate the HTMLDocument tree Elements and try to get the 400.

第一个如何定义width属性以及如何从模型Element获取它。尝试调查HTMLDocument树元素并尝试获取400。

The second part is relatively easier. If you can read the width setting from the Element (either from attributes or from style) you can override javax.swing.text.html.ParagraphView in the HTMLEditorKit's ViewFactory and return 400 (the width prom property/style/attribte) for getPreferredSpan/getMinumumSpan/getMaximumSpan and X_AXIS

第二部分相对容易一些。如果你可以从Element中读取宽度设置(来自属性或样式),你可以覆盖HTMLEditorKit的ViewFactory中的javax.swing.text.html.ParagraphView并返回getPreferredSpan /的400(宽度prom属性/ style / attribte) getMinumumSpan / getMaximumSpan和X_AXIS