程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在 FXML 中定义 Stage?

发布于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 文件中使用来“外包”它,但这只是给了我一个“意外的结束标签:场景”错误。

  1. 如何在 FXML 中定义 Stage?
  2. Stage 是 JFrame 的 JFX 类比吗?

解决方案


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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

13 0
收藏该文
已收藏

评论内容:(最多支持255个字符)