问题描述
我已经在VBox
中创建了Horizontal Separator
.
I have created a Horizontal Separator
in a VBox
.
以下是代码:
Following is the code:
Separator s = new Separator(Orientation.HORIZONTAL);
s.setStyle(""
+ "-fx-border-width: 1px;"
+ "-fx-border-color: black;"
+ "-fx-padding: 0px;"
+ "");
getChildren().add(s);
我想删除Separator
内部的空间.因此,我设置了CSS属性-fx-padding: 0px;
,但似乎不起作用.
I want to remove the space inside the Separator
. So I set the CSS property -fx-padding: 0px;
but it doesn't seem to work.
我该怎么办??!
这是图片分隔符
Here is the imageSeparator
推荐答案
分隔符由两个元素组成.
The separator consists out of two Elements.
具有CSS类 separator 的 Separator 根节点以及具有css类 line
The Separator root node with css class separatorand an child element Region with css class line
如果要删除黑框中间的行,则必须修改region( line )子级并设置其边框 insets 和 width 到 0px
If you want to remove the line in the middle of the black box than you have to modify the region(line) child and set its border insets and width to 0px
例如:
Separator separator = new Separator();
separator.setStyle(""
+ "-fx-border-width: 1px;"
+ "-fx-border-color: black;"
+ "-fx-padding: 0px;"
+ "");
stage.show()
在stage.show()之后,您将可以通过查找或 getChildrenUnmodifiable()
And than after stage.show() you will have access to its childern via lookup or getChildrenUnmodifiable()
Node line = separator.lookup(".line");
line.setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
或
separator.getChildrenUnmodifiable().get(0).setStyle(""
+ "-fx-border-insets: 0px;"
+ "-fx-border-width: 0px;"
+ "");
和第三种带有指标的选项
FontMetrics metrics = Toolkit.getToolkit().getFontLoader().getFontMetrics(label.getFont());
label.setPadding(new Insets(-metrics.getDescent(), 0, 0, 0));
和带有边界的第四个选项,它不适用于所有节点,并且在这种情况下不起作用.
and fourth option of With Bounds which is not applyable for all Nodes and does not work in this case.
Text text = new Text();
text.setBoundsType(TextBoundsType.VISUAL);
最后,您可以添加一个 CSS 文件,该文件会更改 .line 类的样式
And as last option you could add an CSS file which changes the style of the .line class
App.css:
.line {
-fx-border-width: 0px;
-fx-border-insets: 0px;
}
.separator {
-fx-padding: 0px;
-fx-border-insets: 0px;
-fx-border-width: 1px;
-fx-border-color: black;
}
然后您只需要将此CSS应用于场景即可.
And than you just have to apply this css to your scene.
scene.getStylesheets().add("App.css");
如果您还不知道它,也许您应该结帐 Scenic View ,这是一个很好的选择工具,如果您想检查JavaFx应用程序.
If you dont know about it already maybe you should checkout Scenic View which is a good tool if you want to inspect JavaFx applications.
这篇关于如何移除分隔器内的空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!