简体   繁体   English

如何将数据显示到 java 中的 JtextArea 与 txt 文件中的精确文本 alignment 相同

[英]How to display data to a JtextArea in java with exact text alignment same from the txt file

这是文本文件中的精确对齐

When I append the data to the JTextArea in Java it does not copy the alignment.当我 append 数据到 Java 中的 JTextArea 时,它不会复制 alignment。

这是文本区域的图像,对齐方式与txt文件有很大不同。

There could be 2 problems at hand.手头可能有2个问题。 If your original textfile is using Tabs instead of spaces to align its columns the way it does, you need to set the same tab size on your JTextArea component.如果您的原始文本文件使用制表符而不是空格来对齐其列,则需要在JTextArea组件上设置相同的制表符大小。

See JavaDoc for setTabSize(int)请参阅JavaDoc 了解 setTabSize(int)

Secondly, alignment can only really be achieved with a mono-sapced font (where every character has the same visual width).其次,alignment 只能使用单字字体(每个字符具有相同的视觉宽度)才能真正实现。

See JavaDoc for setFont(Font)请参阅JavaDoc 了解 setFont(Font)

I just found this answer by Guillaume Polet where he describes the way to get a general-purpose monospace font by simply specifying monospaced as the font's name:我刚刚找到了 Guillaume Polet 的答案,他在其中描述了通过简单地将monospaced指定为字体名称来获得通用等宽字体的方法:

JTextArea textArea = new JTextArea(24, 80);
textArea.setFont(new Font("monospaced", Font.PLAIN, 12));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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