Не удается удалить дополнительный межстрочный интервал в JTextPane для HTML-контента
Я не могу сжать строки в Java JTextPane
если я установлю тип контента на text/html
, Я хотел бы, чтобы они были так близко друг к другу, как они, когда тип контента text/plain
, по умолчанию.
line-height
, top-margin
Свойства CSS, похоже, не помогают:(.
Это вывод моей программы-примера, который показывает, что строки занимают больше места, когда редактор HTML обрабатывает рендеринг:
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 <b>plain text</b>\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 = "" +
"<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
3 ответа
Это может зависеть от платформы:
Попробуйте что-то вроде этого
StyledDocument doc= textPane.getStyledDocument();
MutableAttributeSet attr= new SimpleAttributeSet();
StyleConstants.setLineSpacing(attr, -0.2f); //NOTE: negative value.
Похоже, у вас запущены окна. Какая версия JDK/JRE используется?
Решено! Если я использую Courier New
вместо monospace
для font-family
межстрочный интервал точно такой же, как в text-plain
версия под винду. Спасибо всем!