JavaFX / SceneBuilder - 仅更改场景的一部分 [英] JavaFX/SceneBuilder - Changing only PART of a Scene

查看:152
本文介绍了JavaFX / SceneBuilder - 仅更改场景的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JavaFX的新手,我没有找到任何有关此事的教程/答案。

I'm a total newbie in JavaFX and I haven't found any tutorial/answer about this matter.

所以我基本上只想改变场景的一部分。我想在顶部保留一个静态菜单栏,只根据单击的菜单按钮更改底部。这意味着我需要为每个页面使用不同的FXML文件和Controller类。

So basically what I'd like to do is change only part of my Scene. I want to keep a static menu bar at the top and only change the bottom part according to which menu button is being clicked. Which means I need different FXML files and Controller classes for every "page".

看看可用的JavaFX功能,我认为SubScene可以完成这项工作。但经过进一步调查后,似乎SubScenes是为3D制作的?所以我真的不知道自己应该做些什么。

Looking at the available JavaFX features I thought SubScene would do the job. But after further investigation it seems that SubScenes are made for 3D stuff? So I really have no clue what I should do.

任何帮助都将不胜感激!

Any help would be greatly appreciated!

推荐答案

更改部分现场很容易。在主场景中,您放置了一个容器,您可以在其中动态加载不同的视图。以下是您对一般静态菜单的描述后的示例:

Changing parts of the scene is easy. In the main scene, you put a container in which you dynamically load different views. Here's an example following your description for a general "static" menu:

这是基本视图。在 mainView 中,加载了不同的视图(默认为view_a.fxml),并从View> Show View X菜单进行了更改。我分配给各个 MenuItem 的I​​D是要加载的 FXML 文件的名称。

This is the basic view. In mainView, the different views are loaded (default is view_a.fxml) and are changed from the View > Show View X menu. The IDs I have assigned to the individual MenuItem are the names of the FXML files to load.

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
    <top>
        <MenuBar BorderPane.alignment="CENTER">
            <menus>
                <Menu mnemonicParsing="false" text="File">
                    <items>
                        <MenuItem mnemonicParsing="false" text="Close" />
                    </items>
                </Menu>
                <Menu mnemonicParsing="false" text="View">
                    <items>
                        <MenuItem fx:id="view_a" mnemonicParsing="false" text="Show View A" onAction="#handleChangeView"/>
                        <MenuItem fx:id="view_b" mnemonicParsing="false" text="Show View B" onAction="#handleChangeView"/>
                    </items>
                </Menu>
            </menus>
        </MenuBar>
    </top>
    <center>
        <BorderPane fx:id="mainView">
            <center>
                <fx:include source="view_a.fxml"/>
            </center>
        </BorderPane>
    </center>
</BorderPane>

这是其中一个视图(view_a.fxml)。另一个是相同的,所以我不会表现出来。在这些视图中,有趣的是我没有指定控制器,因为我后来在主框架上使用控制器(这对于小东西来说很好,但对于大项目来说,使用不同的控制器很好。)

This is one of the views (view_a.fxml). The other is the same, so I will not show it. In these views, it's interesting that I do not specify a controller because I later use the controller on the main frame (this is good for small things, but for big projects it is nice to use different controllers).

<AnchorPane xmlns="http://javafx.com/javafx" xmlns:fx="http://javafx.com/fxml">
    <children>
        <Label text="View A"/>
    </children>
</AnchorPane>

控制器本身并不特别。它只是将一个.fxml添加到所选选项的ID中,加载带有结果名称的 FXML 文件,并将其放入 mainView center

The controller itself is nothing special. It just adds one ".fxml" to the ID of the option that is selected, loads the FXML file with the resulting name, and puts it in the mainView center

public class Controller {
    @FXML
    private BorderPane mainView;

    @FXML
    private void handleChangeView(ActionEvent event) {
        try {
            String menuItemID = ((MenuItem) event.getSource()).getId();

            FXMLLoader loader = new FXMLLoader(getClass().getResource(menuItemID + ".fxml"));
            loader.setController(this);

            mainView.setCenter(loader.load());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

这篇关于JavaFX / SceneBuilder - 仅更改场景的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆