我需要了解如何显示由 Main javaFX 应用程序加载的 FXML 文件中插入的元素,我的 JavaFX 应用程序 main 是:
// imports omitted
public class Main extends Application {
@Override
public void start(Stage window) throws IOException {
Parent root = FXMLLoader.load(getClass().getResource("Standard.fxml"));
Scene mainGraphic = new Scene(root,500,500);
window.setTitle("Prova con FXML");
window.setMinHeight(500);
window.setMinWidth(500);
window.setScene(mainGraphic);
window.show();
}
}
该文件可以正常工作并正确加载 FXML 文件Standard.fxml
,问题是它没有显示顶部矩形,这是 FXML 文件:
// imports omitted
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.myname.mypackage.Controller">
<stylesheets>
<URL value="@Standard.css"/>
</stylesheets>
<Rectangle id="ParteSuperiore"/>
</AnchorPane>
我显然已经创建了 CSS 文件并使用我想要的属性对元素进行了风格化,这是 CSS:
#AnchorPane {
-fx-background-color: rgb(224, 246, 255);
}
#ParteSuperiore {
-fx-fill: rgb(255, 145, 28);
-fx-arc-height: 100px;
-fx-arc-width: 100px;
}
这个文件有什么问题?我只能看到 AnchorPane 的背景颜色!我试图放置Rectangle
内部<children>
元素,但是我继续只看到 AnchorPane 的背景颜色,而我没有看到矩形!我应该使用区域而不是矩形吗?如果是,我如何给它宽度和高度?在JavaFX CSS 参考中,它没有给我设置宽度和高度的指令,比如-fx-arc-height
矩形。
您似乎混淆了 的arcHeight
/ arcWidth
properties 和height
/ width
properties Rectangle
。根据文件,该arcHeight
物业:
定义矩形四个角处圆弧的垂直直径。当且仅当弧宽和弧高属性都大于 时,矩形将具有圆角
0.0
。
和height
财产:
定义矩形的高度。
该arcWidth
和width
性能也有类似的文件。
无论是width
和height
属性都有一个默认值0.0
。由于您没有为您定义宽度或高度,Rectangle
因此无需渲染。查看JavaFX CSS 参考指南的文档Rectangle
以及Shape
和Node
,无法Rectangle
从 CSS 1设置 a 的宽度或高度。您需要在代码或 FXML 文件中执行此操作。例如:
// imports omitted
<AnchorPane id="AnchorPane" prefHeight="400.0" prefWidth="600.0" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.myname.mypackage.Controller">
<stylesheets>
<URL value="@Standard.css"/>
</stylesheets>
<Rectangle id="ParteSuperiore" width="100" height="100"/>
</AnchorPane>
并且您可能希望删除或至少更改CSS 文件中的-fx-arc-width
和-fx-arc-height
值。
1. 查看实现证实了这一点。既不是width
也不height
是 的实例StyleableProperty
,不像arcWidth
和arcHeight
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句