我想为TitledPane
的标题添加一个小图标。因此,我设置了一个空标题并添加一个HBox
作为图形,其中包含一个Label
和一个ImageView
。通过这种方式,图标显示在靠近文本的末尾。我希望它始终显示在TitledPane
的右边界旁边。 我该怎么做? 我也尝试使用BorderPane
并将Label
添加到中心,将ImageView
添加到右侧,但BorderPane
未获得TitledPane的最大大小。 所以我试图设置MaxWidth Max-Value,但这并没有帮助JavaFX 8 - 将图形添加到右侧的TitledPane
有谁知道该怎么办?
**编辑:**我创建的“自定义”控件将在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;
}
但是当我在场景中有另一个节点时,我无法将场景的根设置为标题栏,或者我错了吗? – 2014-11-24 13:34:18
我使用的是简单的根。您只需将您的标题栏作为孩子添加到适当的容器中即可。 – 2014-11-24 13:35:30
现在工作。非常感谢你的帮助! – 2014-11-24 13:41:07