简体   繁体   English

如何使用循环添加 JavaFX 元素?

[英]How do i add JavaFX elements with a loop?

I am creating a mini social media with JavaFX and SQL, where users can create posts.我正在使用 JavaFX 和 SQL 创建一个迷你社交媒体,用户可以在其中创建帖子。 I have each post in a Pane element, that includes the name of the user, date of the post and the text in the post.我在 Pane 元素中包含每个帖子,其中包括用户姓名、帖子日期和帖子中的文本。 I would like to create a loop, that inserts however many posts that exist.我想创建一个循环,插入存在的许多帖子。 I simply need to know, how I can create a loop that inserts for an example 5 identical pane elements.我只需要知道,如何创建一个循环,为示例 5 个相同的窗格元素插入。 I am writing my code in a fxml file.我在 fxml 文件中编写我的代码。 I also have a controller where I can write functions.我还有一个控制器,我可以在其中编写函数。 I can't write java code in the fxml file where the javafx elements exists, so I need to insert the pane elements via a loop in the controller somehow.我无法在存在 javafx 元素的 fxml 文件中编写 java 代码,因此我需要以某种方式通过控制器中的循环插入窗格元素。

Example of code: I haven't writted the code, i'm just asking for what approach I should take.代码示例:我没有写代码,我只是问我应该采取什么方法。 Here is what the code is going to look like:下面是代码的样子:

<AnchorPane id="AnchorPane">
    <VBox layoutY="170.0" prefWidth="557.0">
        // Start Loop
          <Pane>
             <children>
                  <Label text="Test Person" />
                <Label text="Date of Post" />
                <TextArea text="Text in Post"/>
             </children>
          </Pane>
       // End Loop
    </VBox>
</AnchorPane>

Here is an image of how the posts look:这是帖子的外观图像:

在此处输入图片说明

Example:例子:

String template = "" +
            "<AnchorPane id=\"AnchorPane\">\n" +
            "    <VBox layoutY=\"170.0\" prefWidth=\"557.0\">\n" +
            "        // Start Loop\n" +
            "          <Pane>\n" +
            "             <children>\n" +
            "                  <Label text=\"%text person%\" />\n" +
            "                <Label text=\"%date of post%\" />\n" +
            "                <TextArea text=\"%text in post%\"/>\n" +
            "             </children>\n" +
            "          </Pane>\n" +
            "       // End Loop\n" +
            "    </VBox>\n" +
            "</AnchorPane>";

    StringBuilder sb = new StringBuilder();
    //Loop through the posts
    for (int i = 0; i<postNumber; i++) {
        sb.append(
                template
                        .replaceAll("%text person%", thePerson)
                        .replaceAll("%date of post%", theDate)
                        .replaceAll("%text in post%", text));
    }
    String outer =
            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
            "\n" +
            "<?import java.lang.*?>\n" +
            "<?import java.util.*?>\n" +
            "<?import javafx.scene.*?>\n" +
            "<?import javafx.scene.control.*?>\n" +
            "<?import javafx.scene.layout.*?>\n" +
            "\n" +
            "<AnchorPane xmlns=\"http://javafx.com/javafx\"\n" +
            "            xmlns:fx=\"http://javafx.com/fxml\"\n" +
            "            fx:controller=\"me.mkdomain.deeplearning.D\"\n" +
            "            prefHeight=\"400.0\" prefWidth=\"600.0\">\n" +
            "%content%\n" +
            "</AnchorPane>";
    String done = outer.replaceAll("%content%", sb.toString());
    FileOutputStream out = new FileOutputStream("myfile.fxml");
    out.write(done.getBytes());
    out.close();
    FXMLLoader loader = new FXMLLoader();
    loader.load(new FileInputStream("myfile.fxml"));

You can use StringBuilder to make fxml.您可以使用 StringBuilder 来制作 fxml。 Then you can write it into a temp file, and then you can load it.然后你可以把它写成一个临时文件,然后你就可以加载它了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM