单击时,我的文本可能会在下面显示更多文本?

时间:2018-06-11 11:31:25

标签: javafx

在我的应用程序中,我正在创建一个帮助页面,我想创建一个文本,当单击文本时,它会在下面显示更多描述文本,当它再次单击标题/问题时,它会隐藏文本。这常见于许多FAQ页面,但我很难找到在JavaFX上执行此操作的方法。

2 个答案:

答案 0 :(得分:1)

您可以使用TitledPane s:

Map<String, String> questions = new HashMap();
questions.put("Question 1", "Lorem ipsum dolor sit amet, consectetur"
        + "adipiscing elit, sed do eiusmod tempor incididunt ut labore"
        + "et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud"
        + " exercitation ullamco laboris nisi ut aliquip ex ea commodo"
        + " consequat. Duis aute irure dolor in reprehenderit in voluptate"
        + " velit esse cillum dolore eu fugiat nulla pariatur. Excepteur"
        + " sint occaecat cupidatat non proident, sunt in culpa qui"
        + " officia deserunt mollit anim id est laborum.");
questions.put("Question 2", "Not answered yet");

VBox content = new VBox();
questions.forEach((key, value) -> {
    Label label = new Label(value);
    TitledPane tp = new TitledPane(key, label);
    tp.setExpanded(false);
    label.setWrapText(true);
    content.getChildren().add(tp);
});

您可能希望使用CSS样式表更改标题的样式,但可以将以下样式表添加到场景中:

.titled-pane > .title {
    -fx-background-color: transparent transparent transparent;
}

答案 1 :(得分:0)

您可以通过扩展Text类的行为来自行实现它,但javafx具有sniproxy并结合Accordion提供您正在寻找的功能。

如何使用它的示例代码:

@Override
public void start(Stage primaryStage) {
    TitledPane tp1 = new TitledPane("Text 1", new Text("Your text goes here....."));
    TitledPane tp2 = new TitledPane("Text 2", new Text("Another text goes here....."));
    TitledPane tp3 = new TitledPane("Text 3", new Text("And yet another text goes here....."));

    Accordion accordion = new Accordion();
    accordion.getPanes().addAll(tp1, tp2, tp3);

    Scene scene = new Scene(accordion, 300, 250);  
    primaryStage.setTitle("AccordionExample");
    primaryStage.setScene(scene);
    primaryStage.show();
}