为什么控制台从 JTextArea (Swing) 打印错误文本
Why the console prints error text from the JTextArea (Swing)
问题是:
当我将 JTextArea
中的文本打印到控制台时:
System.out.println(textArea.toString());
我得到这样的输出:
javax.swing.JTextArea[,0,0,522x170,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@43413332,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],colums=0,columWidth=0,rows=0,rowHeight=0,word=false,wrap=false]
谁能帮我解决这个问题?
使用getText()
方法获取显示的文字,而不是toString()
。
System.out.println(textArea.toString());
即显示文本区域的属性,而不是文本区域中的文本。
Java 中的大多数对象都有自定义 toString()
方法来显示有关对象属性的信息。
你想要:
System.out.println(textArea.getText());
问题是:
当我将 JTextArea
中的文本打印到控制台时:
System.out.println(textArea.toString());
我得到这样的输出:
javax.swing.JTextArea[,0,0,522x170,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=javax.swing.plaf.basic.BasicBorders$MarginBorder@43413332,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],disabledTextColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],selectedTextColor=sun.swing.PrintColorUIResource[r=51,g=51,b=51],selectionColor=javax.swing.plaf.ColorUIResource[r=184,g=207,b=229],colums=0,columWidth=0,rows=0,rowHeight=0,word=false,wrap=false]
谁能帮我解决这个问题?
使用getText()
方法获取显示的文字,而不是toString()
。
System.out.println(textArea.toString());
即显示文本区域的属性,而不是文本区域中的文本。
Java 中的大多数对象都有自定义 toString()
方法来显示有关对象属性的信息。
你想要:
System.out.println(textArea.getText());