JavaFX 8-在右侧的TitledPane中添加图形

JavaFX 8-在右侧的TitledPane中添加图形,第1张

JavaFX 8-在右侧的TitledPane中添加图形

根据OP在其已编辑问题上显示代码,此代码解决了以下事实:在显示阶段之前,在自定义类上在侦听器上创建了标题窗格。

@Overridepublic void start(Stage primaryStage) {    Scene scene = new Scene(new StackPane(), 300, 250);    primaryStage.setScene(scene);    primaryStage.setonShown(e -> {        CustomTitledPane customTitledPane = new CustomTitledPane("Title", new StackPane(new Label("Graphic to the Right")));        scene.setRoot(customTitledPane);        customTitledPane.applyCss();        customTitledPane.layout();        // title region        Node titleRegion=customTitledPane.lookup(".title");        // padding        Insets padding=((StackPane)titleRegion).getPadding();        // image width        double graphicWidth=customTitledPane.getGraphic().getLayoutBounds().getWidth();        // arrow        double arrowWidth=titleRegion.lookup(".arrow-button").getLayoutBounds().getWidth();        // text        double labelWidth=titleRegion.lookup(".text").getLayoutBounds().getWidth();        double nodesWidth = graphicWidth+padding.getLeft()+padding.getRight()+arrowWidth+labelWidth;        customTitledPane.graphicTextGapProperty().bind(customTitledPane.widthProperty().subtract(nodesWidth));    });    primaryStage.show();}class CustomTitledPane extends TitledPane {    public CustomTitledPane(String titleText, Node node) {        super(titleText, node);        setAnimated(true);        setCollapsible(true);        ImageView img = new ImageView(new Image(getClass().getResource("unlock24.png").toExternalForm()));        img.setFitHeight(10d);        img.setPreserveRatio(true);        img.setSmooth(true);        setGraphic(img);        setContentDisplay(ContentDisplay.RIGHT);    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5506511.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存