当前位置: 首页 > 面试题库 >

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

柴兴修
2023-03-14
问题内容

我想在的标题上添加一个小图标TitledPane。因此,我设置了一个空标题并添加了一个HBox包含a
LabelImageViewas的图形。这样,图标显示在文本结尾附近。我希望它始终显示在的右侧边框旁边TitledPane。我怎样才能做到这一点?我也尝试使用a
BorderPane并将其添加Label到中间,ImageView在右侧添加,但是BorderPane没有获得TitledPane的最大大小。所以我试图将MaxWidth设置为Max-
Value,但这没有帮助

有人知道该怎么办吗?

编辑:我创建的“自定义”控件将在stage.setOnShown中调用的方法中初始化。

public class CustomTitledPane extends TitledPane {
private Image alert;
private Image registered;
private Image deleted;
private ImageView img;

public CustomTitledPane(String titleText, Node node) {
    super(titleText, node);
    setAnimated(true);
    setCollapsible(true);
    img = new ImageView();
    img.setFitHeight(10d);
    img.setPreserveRatio(true);
    img.setSmooth(true);
    setGraphic(img);
    setContentDisplay(ContentDisplay.RIGHT);
    // apply css and force layout of nodes
    applyCss();
    layout();

    // title region
    Node titleRegion = lookup(".title");
    // padding
    Insets padding = ((StackPane) titleRegion).getPadding();
    // image width
    double graphicWidth = img.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;
    System.out.println("w: " + graphicWidth + " " + arrowWidth + " "
            + labelWidth);
    graphicTextGapProperty().bind(widthProperty().subtract(nodesWidth));
    try {
        alert = new Image(new FileInputStream("img/Alert.png"));
        registered = new Image(new FileInputStream("img/Registered.png"));
        deleted = new Image(new FileInputStream("img/Deleted.png"));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
} }

这是TitledPane的CSS:

    .titled-pane {
    -fx-text-fill: #006FD8;
}

.titled-pane > .title {
    -fx-background-color: transparent;
    -fx-border-color: linear-gradient(to right, white 0%, grey 30%, grey 70%, white 100%) transparent transparent transparent;
}

.titled-pane:expanded > .title {
    -fx-border-color: grey transparent transparent transparent;
    -fx-background-color: linear-gradient(to bottom, #DCE7F5, white);
}

.titled-pane:expanded > *.content {
    -fx-border-width: 0;
}

问题答案:

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

@Override
public 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);
    }
}


 类似资料:
  • 我正在将以下数据作为赋值的一部分读取到二叉树(不是严格的二叉查找树)中: 它们被读取到python、和中的三个列表中,其中第一行具有整数是节点数。接下来的n行是键,左,右。其中左是父级左子级的键是,同样右子级的键是,因此例如第一行是4的键是根,意味着2是4的左子级,意味着5是4的右子级,以此类推,-1代表左右意味着这个键是叶子: 此示例的树结构 问题是正在添加根的左右子节点,但没有添加其中的任何子

  • 我想在布局的右侧和底部添加一个光影。我尝试使用,但它在布局的所有四面都添加了一个厚厚的阴影。我需要创建和设置什么绘图器作为背景?

  • 问题内容: 我试图在 UINavigationBar的* 右侧添加 自定义视图 。我尝试了以下操作,但是视图未显示!请帮助,在此先感谢! * 问题答案: //创建uiview并添加三个自定义按钮

  • 我需要从条形图中删除右侧图例。 这是我的密码。 谢谢

  • 我最近开始学习数据结构和算法。我正在创建一个二叉树,它只在树的左侧添加节点。我如何创建这样一种方式,即它应该在根节点的两侧添加节点,并如下所示: 以下是我编写的代码: 主要类别:

  • 我的问题是:如何确定已拆分的旋转矩形几何体的和是拆分该几何体的任意的“左”和“右”边? 对于这个问题,"左"和"右"被定义为从节点到节点"行走"时,按顺序的拆分器的左边和右边。 我创建了此函数,用于将任意几何体(非集合)拆分为两个面——“左”和“右”: 上面的想法在此处链接的笔记本中进行了说明(与上面的链接相同): http://nbviewer.jupyter.org/urls/dl.dropb