English 中文(简体)
Can t 取消JTextPane用于html内容的超线间隔
原标题:Can t remove extra line spacing in JTextPane for html content

如果我把内容类型设定为<代码>text/html的话,我可以把标题放在 Java中。 我祝愿它们在内容类型为<条码>、文字/解释<>/条码>、缺省时结束。

http://www.ohchr.org。

这是我的样本方案的产出,这表明,当一个超文本编辑处理翻译时,这些线路确实占用了更多的空间:

alt text http://lh6.ggpht.com/_Wx4sMDdKKdU/S8cYWIhPKhzI/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAQ/4zQAQQAQAQAQAQAQAQZFwFygmF/EBmgm

形成框架的法典是:

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JTextPane;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class DemoSimplestGui extends JFrame  {
    private static final long serialVersionUID = 1L;

    private static final int WINDOW_WIDTH = 800;
    private static final int WINDOW_HEIGHT = 130;

    private static final String PLAIN_TEXT = "" +
        "This is some <b>plain text</b>
" +
        "separated by backslash-n characters
" +
        "There s no empty space between lines
" +
        "which is exactly what we need.";

    private static final String DIV_BASED_HTML_TEXT = "" +
        "<div>This is some <b>html text</b></div>" +
        "<div>that usses DIV tags.</div>" +
        "<div>There s too much blank space</div>" +
        "<div>and that sucks for my application</div>";

    private static final String PRE_BASED_HTML_TEXT = "" +
        "<pre>This is some <b>html text</b></pre>" +
        "<pre>that usses PRE tags</pre>" +
        "<pre>There s too much blank space grr</pre>" +
        "<pre>and that sucks for my application</pre>";

    public static void main(String[] args) {
        final DemoSimplestGui frame = new DemoSimplestGui();
        frame.setPreferredSize(new Dimension(WINDOW_WIDTH, WINDOW_HEIGHT));
        frame.setSize(frame.getPreferredSize());
        frame.setMinimumSize(new Dimension(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2));
        frame.init();
        frame.setVisible(true);
    }

    public void init() {
        setLayout(new BorderLayout(10, 10));
        add(createPlainTextPane(), BorderLayout.WEST);
        add(createDivBasedHtmlTextPane(), BorderLayout.CENTER);
        add(createPreBasedHtmlTextPane(), BorderLayout.EAST);
    }

    private JTextPane createPlainTextPane() {
        final JTextPane textPane = new JTextPane();
        textPane.setContentType("text/plain");
        StyleConstants.setFontFamily(textPane.getInputAttributes(), "Courier New");
        textPane.setText(PLAIN_TEXT);
        return textPane;
    }

    private JTextPane createDivBasedHtmlTextPane() {
        final JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setEditorKit(configureHtmlEditorKit(textPane));
        textPane.setText(DIV_BASED_HTML_TEXT);
        return textPane;
    }

    private JTextPane createPreBasedHtmlTextPane() {
        final JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        textPane.setEditorKit(configureHtmlEditorKit(textPane));
        textPane.setText(PRE_BASED_HTML_TEXT);
        return textPane;
    }

    private HTMLEditorKit configureHtmlEditorKit(JTextPane textPane) {
        final HTMLEditorKit kit = (HTMLEditorKit) textPane.getEditorKit();
        final StyleSheet css = new StyleSheet();
        css.addRule("body { font-family: monospaced; margin-top: 0; margin-down: 0; line-height: 0; }");
        css.addRule("div, pre { margin-top: 0; margin-down: 0; line-height: 0; }");
        kit.setStyleSheet(css);
        return kit;
    }

}

我确实赞赏某些方面:D

问题回答

类似情况

StyledDocument doc= textPane.getStyledDocument();
MutableAttributeSet attr= new SimpleAttributeSet();
StyleConstants.setLineSpacing(attr, -0.2f); //NOTE: negative value.

就像你一样,看着窗户。 JDK/JRE的哪些版本正在使用?

Solved! 如果我使用<代码>Courier New,而不是monospace ,font-family,则行距与text-plain 版本完全相同。 感谢大家。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...