Style a JTextPane to have console-like formatting?

人盡茶涼 提交于 2020-01-04 04:45:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!