问题
Is there a way to make the text in a JTextPane look similar to that of console output? By that I mean, basically, how each character is the same width, so that things like ASCII art, or spacing indentation would work correctly.
For example, currently, if I type "First" and then 5 spaces, and then on a new line "Second" and then 4 spaces, the two lines do not end in the same position, so if there was text following those spaces, the text would not be aligned.
I don't know if it changes anything, but JComponents will also be included in the JTextPane.
回答1:
set the font to a fixed width font such as courier, or use the Monospaced
font-family.
JTextPane text = new JTextPane();
Font font = new Font(Font.MONOSPACED, Font.PLAIN, 14);
text.setFont(font);
来源:https://stackoverflow.com/questions/3747860/style-a-jtextpane-to-have-console-like-formatting