如何隐藏或关闭 javafx 中的文本部分

How to hide or close sections of text in javafx

您好,我正在尝试让包含“children”文本的文本按图片中的 箭头 可以分段 off/hidden。因此,如果箭头指向下方,它应该打开更多信息,如果它指向正确,它应该关闭它。我不知道这个方法或功能的名称,所以我不知道如何 google 正确 class 。如果有人可以帮助并告诉我 javafx class 能够产生这种效果,那将会很有帮助。

如果标题不好,请见谅。我不知道我想从图片中找到正确的词。

树视图就是您要查找的内容这是一个示例: Javafx TreeView

树视图

您可以像 html 中的嵌套列表一样使用 TreeView。 当您初始化 TreeView object 时,您需要提供通用类型 <t>,在本例中为 <String>。然后,每个 TreeView 都需要一个根,您可以在 setRoot() 方法中将 TreeItem object 传递给 Treeview。 Root 可以有自己的 TreeItem children,每个 children 也可以有 children,等等。 没有 child 的 TreeItem 称为“叶”,带有 children 的 TreeItems 称为“分支”。像真正的树。

这是一个 javaFX 应用程序,您可以尝试

public class App extends Application {
    
    @Override
    public void start(Stage stage) {
        TreeView<String> treeView = new TreeView<>();
        TreeItem<String> rootItem = new TreeItem<>("lore ipsum");
        treeView.setRoot(rootItem);
        TreeItem<String> leafItem = new TreeItem<>("dolor sit amet, consectetur adipiscing elit");
        TreeItem<String> leafItem1 = new TreeItem<>(" Aliquam justo ipsum, venenatis eget congue eu,");
        TreeItem<String> leafItem2 = new TreeItem<>("imperdiet a augue. Mauris elementum risus leo");
        TreeItem<String> leafItem3 = new TreeItem<>("id pharetra nulla mattis ut. Integer pulvinar nisl in velit ultrices");
        TreeItem<String> leafItem4 = new TreeItem<>("ac semper ante porttitor. Aenean vel ligula ipsum. Lorem ipsum dolor sit amet,");
        TreeItem<String> leafItem5 = new TreeItem<>("consectetur adipiscing elit. Mauris efficitur dapibus varius.");
        TreeItem<String> leafItem6 = new TreeItem<>("nteger orci ipsum, egestas sed justo sit amet, fermentum semper libero. ");
        TreeItem<String> branch = new TreeItem<>("lore ipsum II");
        TreeItem<String> branch1 = new TreeItem<>("lore ipsum III");
        TreeItem<String> branch2 = new TreeItem<>("lore ipsum IV");
        rootItem.getChildren().addAll(leafItem, leafItem1, leafItem2, leafItem3, leafItem4, leafItem5, leafItem6, branch, branch1, branch2);
        branch.getChildren().addAll(
                new TreeItem<>("dolor sit amet, consectetur adipiscing elit"),
                new TreeItem<>(" Aliquam justo ipsum, venenatis eget congue eu,"),
                new TreeItem<>("imperdiet a augue. Mauris elementum risus leo"),
                new TreeItem<>("id pharetra nulla mattis ut. Integer pulvinar nisl in velit ultrices"),
                new TreeItem<>("ac semper ante porttitor. Aenean vel ligula ipsum. Lorem ipsum dolor sit amet,"),
                new TreeItem<>("consectetur adipiscing elit. Mauris efficitur dapibus varius."),
                new TreeItem<>("nteger orci ipsum, egestas sed justo sit amet, fermentum semper libero. "));
        branch1.getChildren().addAll(
                new TreeItem<>("dolor sit amet, consectetur adipiscing elit"),
                new TreeItem<>(" Aliquam justo ipsum, venenatis eget congue eu,"),
                new TreeItem<>("imperdiet a augue. Mauris elementum risus leo"),
                new TreeItem<>("id pharetra nulla mattis ut. Integer pulvinar nisl in velit ultrices"),
                new TreeItem<>("ac semper ante porttitor. Aenean vel ligula ipsum. Lorem ipsum dolor sit amet,"),
                new TreeItem<>("consectetur adipiscing elit. Mauris efficitur dapibus varius."),
                new TreeItem<>("nteger orci ipsum, egestas sed justo sit amet, fermentum semper libero. "));
        branch2.getChildren().addAll(
                new TreeItem<>("dolor sit amet, consectetur adipiscing elit"),
                new TreeItem<>(" Aliquam justo ipsum, venenatis eget congue eu,"),
                new TreeItem<>("imperdiet a augue. Mauris elementum risus leo"),
                new TreeItem<>("id pharetra nulla mattis ut. Integer pulvinar nisl in velit ultrices"),
                new TreeItem<>("ac semper ante porttitor. Aenean vel ligula ipsum. Lorem ipsum dolor sit amet,"),
                new TreeItem<>("consectetur adipiscing elit. Mauris efficitur dapibus varius."),
                new TreeItem<>("nteger orci ipsum, egestas sed justo sit amet, fermentum semper libero. "));
        
        stage.setTitle("TreeView");
        Scene scene = new Scene(treeView, 640, 480);
        stage.setScene(scene);
        stage.show();
    }
    
    public static void main(String[] args) {
        launch();
    }
    
}