如何在另一个下打印几个字符串?

时间:2014-07-25 21:30:49

标签: java string swing

当我制作JOptionPane.showMessageDialog(null, abc)时,它会并排打印这两个字符串,但我想将它们打印在另一个之下。我怎么能这样做?

import javax.swing.*;

public class abc {
    public static void main(String args[]) {
        String abc ="Highschool 1" + "Highschool 2";
        JOptionPane.showMessageDialog(null, abc);
    }
}

5 个答案:

答案 0 :(得分:2)

待办事项

String abc = "Highschool 1" + "\n" + "Highschool 2";

\n是换行符,会在Highschool 1

之后换行

答案 1 :(得分:2)

你也可以使用HTML(基本上是HTML 3.2),这很酷,因为你可以添加颜色和其他格式:

public static void main(String[] args) {
    String msg = "<html><font color='red'>hello</font><br><big>goodbye</big></html>";
    JOptionPane.showMessageDialog(null, msg);
}

答案 2 :(得分:1)

String abc ="Highschool 1" + "\n" + "Highschool 2";

\n是回车符。

答案 3 :(得分:1)

您需要使用新行正则表达式"\n"

String abc ="Highschool 1" + "\n" + "Highschool 2";

如果你打印abc,那就是:

Highschool 1
Highschool 2

答案 4 :(得分:0)

使用String api

的另一种方式是连接

只是为了给你另一种味道

代码:

  public class LambdaTest     
{
    public static void main (String[] args)
    {
         String s1 = "Highschool 1";
         String s2 = "Highschool 2";

         String s = s1.concat("\n" + s); <-- \n That means a new line is printed
         System.out.println(s);
    } 
}

另一种方法是在每行中打印每一行并使用println

代码:

  System.out.println("HighSchool 1");<-- \n That means a new line is printed
                                            It is implicit in println function 
  System.out.println("HighSchool 2");

输出:

Highschool 1
Highschool 2