如何在 JavaFX 应用程序中加载的 FXML 文件中显示元素?
How to show elements on FXML file loaded in JavaFX application?
我需要了解如何显示插入到主 javaFX 应用程序加载的 FXML 文件中的元素,我的 JavaFX 应用程序主要是:
// 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 reference 中,它没有给我设置宽度和高度的说明,例如矩形的 -fx-arc-height
。
您似乎混淆了 arcHeight
/arcWidth
属性和 Rectangle
的 height
/width
属性。根据文档,arcHeight
属性:
Defines the vertical diameter of the arc at the four corners of the rectangle. The rectangle will have rounded corners if and only if both of the arc width and arc height properties are greater than 0.0
.
和 height
属性:
Defines the height of the rectangle.
arcWidth
和 width
属性有类似的文档。
width
和 height
属性都有默认值 0.0
。由于您没有为 Rectangle
定义宽度或高度,因此没有任何内容可以渲染。查看 JavaFX CSS Reference Guide 的 Rectangle
文档以及 Shape
和 Node
,无法设置 Rectangle
的宽度或高度CSS1。您需要在代码或 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.查看 implementation 证实了这一点。 width
和 height
都不是 StyleableProperty
的实例,不像 arcWidth
和 arcHeight
。
我需要了解如何显示插入到主 javaFX 应用程序加载的 FXML 文件中的元素,我的 JavaFX 应用程序主要是:
// 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 reference 中,它没有给我设置宽度和高度的说明,例如矩形的 -fx-arc-height
。
您似乎混淆了 arcHeight
/arcWidth
属性和 Rectangle
的 height
/width
属性。根据文档,arcHeight
属性:
Defines the vertical diameter of the arc at the four corners of the rectangle. The rectangle will have rounded corners if and only if both of the arc width and arc height properties are greater than
0.0
.
和 height
属性:
Defines the height of the rectangle.
arcWidth
和 width
属性有类似的文档。
width
和 height
属性都有默认值 0.0
。由于您没有为 Rectangle
定义宽度或高度,因此没有任何内容可以渲染。查看 JavaFX CSS Reference Guide 的 Rectangle
文档以及 Shape
和 Node
,无法设置 Rectangle
的宽度或高度CSS1。您需要在代码或 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.查看 implementation 证实了这一点。 width
和 height
都不是 StyleableProperty
的实例,不像 arcWidth
和 arcHeight
。