Getting JTextArea to display fixed-width font without antialiasing

后端 未结 2 811
清歌不尽
清歌不尽 2020-12-10 13:46

Does anybody know how to get JTextArea to display a fixed size font on all platforms?

I want to make a simple code editor with save/open functionality, which is simp

2条回答
  •  死守一世寂寞
    2020-12-10 14:02

    I came close to a solution by using JTextPane instead of JTextArea by setting the content type of the JTextPane to text/html and setting the text as a html content.

    JtextPane textPane;
    String text;
    
    // initialization
    
    textPane.setContentType("text/html");
    textPane.setText(""
            + ""
            + ""
            + "

    " + ""+text.replaceAll(" ", " ").replaceAll("\n", "
    ")+"
    " + "

    " + "" + "" + "");

    tag is making the text monospaced and all white spaces and new line characters of the text have replaced with html space entity and
    tag, respectively. I tested the output in both Windows and Ubuntu OSes and they were same.

提交回复
热议问题