- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我想在 TitledPane
的标题中添加一个小图标。因此,我设置了一个空标题并添加了一个包含 Label
和 ImageView
的 HBox
作为图形。通过这种方式,图标显示在靠近文本末尾的位置。我希望它始终显示在 TitledPane
的右边框旁边。我怎样才能做到这一点?我还尝试使用 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 在他编辑的问题上显示的代码,此代码解决了以下事实:带标题的 Pane 是在显示舞台之前在自定义类上的监听器上创建的。
@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);
}
}
关于JavaFX 8 - 向右侧的 TitledPane 添加图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27100556/
有没有办法将 TitledPane 标题的大小设置为 GridPane 的大小? 这是 TitledPane 的代码 // --- GridPane container fin
关于 JavaFX: How to change the focus traversal policy? Alexander Kirov 展示了如何为 JavaFX 应用程序自定义焦点遍历策略。它工作
是否可以在 TitledPane 的标签上添加 JavaFX.Button? 已找到解决方案!来源:javawiki.sowas.com/doku.php?id=javafx:titledpane-w
我正在使用 TitledPanes ScrollPanes 和 TableViews,并且我遇到了问题,当我折叠 titledPane 时,水平 TableView 的 >ScrollBar 重置。
我有一个 TitledPane ,里面有 ListView ,当我折叠 titledPane 并展开它后,列表不再可见,但它仍然在某处。如果我调整 titledPane 的大小(通过调整应用程序窗口的
我需要在运行时根据一些传入值更改 TitledPane 标题背景,否则重置它。 我所有的 TitledPane 都在附加到场景的 CSS 上设置了样式。 换个背景没问题。问题是当鼠标在背景更改后进入标
我有两个不同的组件:TableView 和表格旁边的 TitledPane。我想做的是重新调整表格 View 的尺寸,但仅在标题 Pane 展开或折叠时进行。当 titledpane 折叠时,表格 V
我想知道,如何在展开和折叠 TitledPane 时调整 Scene 的大小。 我试过只有 1 个 TitledPane 1 个标题 Pane package fxinswing; import ja
有没有办法设置 titledpane 的动画速度?我什么也没找到。事实上有两个问题。第一的:展开的动画比内容本身的展开更快。您会看到圆圈的速度比第二个标题 Pane 中的条向下移动的速度稍慢。 第二:
我遇到了一个奇怪的问题/错误,第一次单击时 TitledPane 不会展开,它需要多次单击才能展开。除此之外,嵌套的 TitledPanes 根本不会展开,除非您多次单击它们并在选项卡之间切换。 所以
我有一个 View ,其中 SplitPane 中有两个垂直方向的 TitledPane。我希望当我折叠其中一个时,将另一个的大小调整为场景的高度。这是我的 .fxml 文件的代码:
是否可以完全删除 TitledPane 的标题? 我设法使用 css 文件中的以下代码使其不可见: .titled-pane > .title{ -fx-background-color: r
有没有办法在 javafx 中设置 TitledPane 之间的边距? 我创建了多个带标题的 Pane 并将它们放在一个 VBox 中。我希望它在每个带标题的 Pane 之间有一个间隙。 我查了set
我已经设法使用 setGraphic 在 TitledPane 中放置一个按钮,但我希望它位于右侧;这是代码: @Override public void start(Stage primaryS
我正在尝试将自定义工具栏添加到通用 TitledPane 中。我的代码似乎可以工作(下面是最小的示例),但我在布局方面遇到问题。具体来说:我使用 setGraphic() 添加工具栏,但它似乎具有固定
有人知道如何在 JavaFX 中以编程方式折叠 TitledPane 吗?我知道 setExpanded 和 setCollapsible 方法可用,但它仅启用该属性。我想在没有任何鼠标交互的情况下折
所以,这是我现在拥有的代码: SplitPane sp = new SplitPane(); final StackPane sp1 = new StackPane(); fi
我目前有一行添加到 JFrame 的 JPanel。我需要这些面板能够展开和折叠,以尽量减少 JFrame 上的困惑。 现在,我只是创建 JPanel,然后将它们添加到 JFrame。 public
我有这个代码: public void start(Stage primaryStage){ TitledPane titledPane = new TitledPane("TITLE", n
我想在 TitledPane 的标题中添加一个小图标。因此,我设置了一个空标题并添加了一个包含 Label 和 ImageView 的 HBox 作为图形。通过这种方式,图标显示在靠近文本末尾的位置。
我是一名优秀的程序员,十分优秀!