Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
有没有办法让 JTextPane 中的文本看起来类似于控制台输出?我的意思是,基本上,每个字符的宽度如何相同,这样 ASCII 艺术或间距缩进之类的东西才能正常工作。
例如,目前,如果我输入“First”,然后输入 5 个空格,然后在新的一行输入“Second”,然后输入 4 个空格,这两行的结尾位置不同,所以如果这些空格后面有文本,文本不会对齐。
我不知道它是否会改变任何东西,但 JComponents 也将包含在 JTextPane 中。
将字体设置为固定宽度的字体,例如 courier,或使用Monospacedfont-family。
Monospaced
JTextPane text = new JTextPane(); Font font = new Font(Font.MONOSPACED, Font.PLAIN, 14); text.setFont(font);