简体   繁体   中英

How to make JEditorpane keep whitespaces

In a JEditorPane for formatting purposes i am using Courier New font type. However the whitespaces are being removed as HTML is white-space insensitive. Therefore If I try to print something like this: "1 3 4", it removes the extra spaces and in practice prints "1 3 4". Any thoughts around this?

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

我得到了答案:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM