Java 如何在JTextArea中格式化文本

Java 如何在JTextArea中格式化文本,java,swing,jframe,jtextarea,Java,Swing,Jframe,Jtextarea,我试图输出多行文本来创建ASCII艺术。但是当我使用JFrame和JTextArea时,它没有正确对齐。我正在尝试打印,但当我在新窗口中打印时,这是我的当前代码(ASCII艺术中会有一些无用的字符): 公共类语言圣诞节{ 公共静态void main(字符串参数[]){ UIManager.put(“swing.boldMetal”,Boolean.FALSE); JFrame f=新JFrame(“圣诞快乐”); f、 addWindowListener(新的WindowAdapter(){ p

我试图输出多行文本来创建ASCII艺术。但是当我使用JFrame和JTextArea时,它没有正确对齐。我正在尝试打印,但当我在新窗口中打印时,这是我的当前代码(ASCII艺术中会有一些无用的字符):

公共类语言圣诞节{
公共静态void main(字符串参数[]){
UIManager.put(“swing.boldMetal”,Boolean.FALSE);
JFrame f=新JFrame(“圣诞快乐”);
f、 addWindowListener(新的WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
});
JTextArea text=新的JTextArea(100,50);
{
text.append(“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”+“\n”);
text.append(“/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
text.append(“/\\/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
text.append(“/Y\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
text.append(“\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\;
text.append(“\\/\\/\/\\/\\/\\/\/\/\/\/\/”+“\n”);
}
JScrollPane=新的JScrollPane(文本);
窗格。设置首选尺寸(新尺寸(500400));
f、 添加(“中心”,窗格);
f、 包装();
f、 setVisible(真);
}
}`

我四处搜索,没有找到这个问题的解决方案。任何帮助都是有用的。

问题是,默认情况下,文本区域使用可变宽度字体。将字体更改为单间距字体将解决问题,例如

   text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
   text.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));