发布于2021-10-15 01:24 阅读(1299) 评论(0) 点赞(13) 收藏(2)
我想完全在 FXML 中定义我的 GUI。我从随处可见的 JavaFX 模板开始,从 Oracle 文档到 Netbeans 模板。在这些模板中,FXML 中没有定义舞台,只有其中包含 UI 控件的实际场景。就像是:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fxskuska.FXMLDocumentController">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69" fx:id="label" />
</children>
</AnchorPane>
这似乎没问题,直到我想改变的第一件事 - 设置窗口的名称。那时我意识到 Scene 不是 Window (或 JFrame 类比),但 Stage 是。当我试图将所有这些都包装在一个元素中时,我无法将该fx:controller
属性设置为 AnchorPane,因为它不再是根元素。我什至试图通过fx:include
在 Stage 文件中使用来“外包”它,但这只是给了我一个“意外的结束标签:场景”错误。
FXML 本质上只是定义了创建对象的方法(通常通过无参数构造函数)并setXXX
在这些对象上调用方法。请参阅文档中的“类实例元素”和“属性元素”。所以你可以很容易地实施
new Scene()
和
<Scene>...</Scene>
和
new Stage()
和
<Stage>...</Stage>
和
Stage stage = new Stage();
Scene scene = new Scene();
scene.setRoot(new AnchorPane(...));
stage.setScene(scene);
和
<Stage>
<scene>
<Scene>
<root>
<AnchorPane ><!--...--></AnchorPane>
</root>
</Scene>
</scene>
<Stage>
该fx:controller
属性必须在根元素中,命名空间信息也应如此。
所以:
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.stage.Stage ?>
<Stage title="My Application" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="FXMLDocumentController">
<scene>
<Scene>
<root>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320">
<children>
<Button layoutX="126" layoutY="90" text="Click Me!"
onAction="#handleButtonAction" fx:id="button" />
<Label layoutX="126" layoutY="120" minHeight="16" minWidth="69"
fx:id="label" />
</children>
</AnchorPane>
</root>
</Scene>
</scene>
</Stage>
然后你会加载这个
import java.io.IOException;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
public class FXMLStageTest extends Application {
@Override
public void start(Stage primaryStage) throws IOException {
Stage stage = FXMLLoader.load(getClass().getResource("Stage.fxml"));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
如果您希望控制器专门应用于窗格,您可以对Stage
和使用一个 FXML 文件Scene
,然后对其他部分使用fx:include
元素。这样您就可以将 UI 拆分为多个 FXML 控制器对。仅将 FXML 文件用于 a 感觉有点多余Stage
(当然,您也可以在 Java 中执行此部分),但有可能:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.*?>
<?import javafx.stage.Stage ?>
<Stage title="My Application" xmlns:fx="http://javafx.com/fxml/1">
<scene>
<Scene>
<root>
<fx:include source="RootPane.fxml"/>
</root>
</Scene>
</scene>
</Stage>
然后你的原始 FXML 文件是RootPane.fxml
. fx:include
如果需要,您可以类似地使用更多标签对其进行分解。
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/301756/574caac480a35cbc5e24/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!