java - 换行继续字符串的一部分

标签 java user-interface netbeans

String noun1 = jTextField1.getText();
        String noun2 = jTextField1.getText();
        String noun3 = jTextField1.getText();

        String verb1 = jTextField1.getText();
        String verb2 = jTextField1.getText();
        String verb3 = jTextField1.getText();

        String adj1 = jTextField1.getText();
        String adj2 = jTextField1.getText();
        String adj3 = jTextField1.getText();

        String story = "This is the story about a " + noun1;

        jTextArea1.setText(story);

我正在使用 JFrame 创建一个 Madlibs,需要找出如何在新行上继续字符串(我忘记了),例如 Story。我需要它继续,以便我可以添加其他句子。

最佳答案

使用行分隔符

String story = "This is the story about a" + System.lineSeparator() + noun1;

或者使用\n作为新行

String story = "This is the story about a\n" + noun1;

当您想要打印出来时,您还可以使用更多 System.out.println(...) 命令。

System.out.println("This is the story about a"); // Print the string inside and add a linebreak afterwards
System.out.println(noun1); // Print the value of noun1 variable

所有这些都会给你以下结果:

This is the story about a
"your story name"

更多信息在这里:Java String new line

关于java - 换行继续字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35276089/

相关文章:

Eclipse 与 Netbeans 基准测试

PHP 文档 block @return 类名

java - 使用基本函数查找最近的点对

java - 通过删除总和不为 17 的连续数字对来计算删除的数字

jQuery slideDown + CSS float

javascript - 《金融时报》如何在粘贴文本时添加免责声明?

java - 读取文件时表达式开始非法

java - 在 Android Studio 项目中使用 OSMDroid 时 OMSDroid.MapView NoClassDefFound

java - "Warning: Do not place Android context classes in static fields; this is a memory leak (and also breaks Instant Run)"

python - 在 PyDev 中查看函数文档字符串的热键?