如何使JEditorpane保持空白

时间:2012-09-12 23:52:16

标签: java swing jeditorpane

在用于格式化目的的JEditorPane中,我使用的是Courier New字体类型。然而,由于HTML对空白不敏感,因此删除了空格。因此,如果我尝试打印这样的东西:“1 3 4”,它会删除多余的空格,并在实践中打印“1 3 4”。有什么想法吗?

  public  JEditorPane  editorPane;
  editorPane.setEditable(false);
  editorPane.setContentType("text/html");
  // add a HTMLEditorKit to the editor pane
  HTMLEditorKit kit = new HTMLEditorKit();
  editorPane.setEditorKit(kit);
   //...
   //...
  // add some styles to the html
  StyleSheet styleSheet = kit.getStyleSheet();
  styleSheet.addRule("body {color:#0f0; font-family:times; margin: 1px; }");
  styleSheet.addRule("h1 {color:blue;}");
  styleSheet.addRule("h2 {color:#ff0000;}");
  styleSheet.addRule("."+StyleCourierNew+" {font-family:\"Courier New\"; font: 11px monaco; color: black; }");

1 个答案:

答案 0 :(得分:1)

我得到了答案:

sString=sString.replaceAll(" ", " ");