Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用“打印”在新行上打印文本\";_Java - Fatal编程技术网

Java 如何使用“打印”在新行上打印文本\";

Java 如何使用“打印”在新行上打印文本\";,java,Java,我尝试创建一个大的字符串,按照我想要的格式,并使用PrinterJob类打印它。以下是字符串的代码: String bigtext = "The name\n" + "some another text"; Graphics2D's_object.drawString(bigtext, 50, 50); 但是它在一行中打印为“另一个文本的名称”,“\n”不起作用,而我想在另一行中打印“另一个文本” 另外,我试着用打印机打印大文本 已解决:以下是解决方案:。(长时间故障后:)换行符\n在某些操作

我尝试创建一个大的
字符串
,按照我想要的格式,并使用
PrinterJob
类打印它。以下是字符串的代码:

String bigtext = "The name\n" + "some another text";
Graphics2D's_object.drawString(bigtext, 50, 50);
但是它在一行中打印为“另一个文本的名称”,
“\n”
不起作用,而我想在另一行中打印
“另一个文本”

另外,我试着用打印机打印大文本


已解决:以下是解决方案:。(长时间故障后:)

换行符
\n
在某些操作系统中不是行分隔符使用
\r\n
。此外,我建议使用
StringBuilder
,而不是使用
+

编辑:您可以使用
System.getProperty(“line.separator”)也是

昨天问了一个类似的问题,这是有帮助的:

您遇到的问题是因为您正在硬编码的“行分隔符”。最好使用以下设备获取系统的行分隔符:

System.getProperty("line.separator");
因此,您的代码如下所示:

   String lineseparator=System.getProperty("line.separator");
   // I'd suggest putting this as a class variable, so that it only gets called once
   // rather than everytime you call the addLine() method

  String bigtext = "The name" + lineseparator + "some another text";

 //If you wanted an empty line in between them, then add the lineseparator twice
  String bigtext = "The name" + lineseparator + lineseparator + "some another text";

看来大多数人都不明白这个问题。我试过印刷工的工作,但也不适合我

我找到了一个解决方案,但没有验证:


你应该考虑在你显示信息的时候添加代码,这样我们可以帮助你提供一些代码片段吗?我们知道如何连接字符串,但如何打印它们?以格式化的方式提供当前输出和预期输出。我看不出这两者有什么不同。帮我看一下它的工作原理。这是你的代码还是不明白“打印机”的意思。您有一个
PrinterJob
。您有s
字符串要打印。然后你做什么?不幸的是,这没有帮助。你能发布代码的其余部分吗,这样我们就可以帮助你了?