无法在JTextPane中删除html内容中的额外行间距

如果我将content-type设置为text/html我无法将Java JTextPane的行拼凑在一起。 当内容类型是text/plain ,我希望它们尽可能地靠近它们,默认情况下。

line-heighttop-margin ,… CSS属性似乎没有帮助:(。

这是我的示例程序的输出,它表明当HTML编辑器处理渲染时,这些行确实占用了更多空间:

alt text http://lh6.ggpht.com/_Wx4sMDdKKdU/S8cYWIPKhzI/AAAAAAAAAig/4QzFwygmEBs/simpleTextPane.PNG

生成框架的代码是:

 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 plain text\n" + "separated by backslash-n characters\n" + "There's no empty space between lines\n" + "which is exactly what we need."; private static final String DIV_BASED_HTML_TEXT = "" + "
This is some html text
" + "
that usses DIV tags.
" + "
There's too much blank space
" + "
and that sucks for my application
"; private static final String PRE_BASED_HTML_TEXT = "" + "
This is some html text

" + "

that usses PRE tags

" + "

There's too much blank space grr

" + "

and that sucks for my application

"; 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

这可能取决于平台:

DemoSimplestGui http://sofzh.miximages.com/java/2d0hu1t.png

尝试这样的事情

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

看起来你正在运行Windows。 使用什么版本的JDK / JRE?

解决了! 如果我使用Courier New而不是monospace作为font-family ,则行间距与Windows下text-plain版本完全相同。 感谢大家!