JavaFX 导航栏和内容 Pane

标签 java javafx navigationbar contentpane

我想在我的新项目中使用 JavaFX,并且想要像下面的屏幕截图那样的东西。

在左侧站点我需要一个导航栏,在右侧我的内容。所以,我会在左侧使用 VBox,在右侧使用 AnchorPane(或者更好的是 ScrollPane)。

当我点击“安全”按钮时,它应该在右侧加载我的“安全”场景。但是我怎么能做到呢。没有找到任何解决方案。

enter image description here

非常感谢

最佳答案

这是此类导航的示例性实现。这里默认加载 view_1.fxml 中描述的 View :

<BorderPane fx:id="mainBorderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
    <left>
        <VBox spacing="5">
            <Button text="btn 1" onAction="#handleShowView1"/>
            <Button text="btn 2" onAction="#handleShowView2"/>
            <Button text="btn 3" onAction="#handleShowView3"/>
        </VBox>
    </left>
    <center>
        <fx:include source="view_1.fxml"/>
    </center>
</BorderPane>

这是 Controller

public class Controller {

    @FXML
    private BorderPane mainBorderPane;

    @FXML
    private void handleShowView1(ActionEvent e) {
        loadFXML(getClass().getResource("/sample/view_1.fxml"));
    }

    @FXML
    private void handleShowView2(ActionEvent e) {
        loadFXML(getClass().getResource("/sample/view_2.fxml"));
    }

    @FXML
    private void handleShowView3(ActionEvent e) {
        loadFXML(getClass().getResource("/sample/view_3.fxml"));
    }

    private void loadFXML(URL url) {
        try {
            FXMLLoader loader = new FXMLLoader(url);
            mainBorderPane.setCenter(loader.load());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

更新

这是一种将 View 直接列在 FXML 文件中的转换

<BorderPane fx:id="mainBorderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
    <left>
        <VBox spacing="5">
            <Button text="btn 1" userData="/sample/view_1.fxml" onAction="#handleShowView"/>
            <Button text="btn 2" userData="/sample/view_2.fxml" onAction="#handleShowView"/>
            <Button text="btn 3" userData="/sample/view_3.fxml" onAction="#handleShowView"/>
        </VBox>
    </left>
    <center>
        <fx:include source="view_1.fxml"/>
    </center>
</BorderPane>

和 Controller

public class Controller {

    @FXML
    private BorderPane mainBorderPane;

    @FXML
    private void handleShowView(ActionEvent e) {
        String view = (String) ((Node)e.getSource()).getUserData();
        loadFXML(getClass().getResource(view));
    }

    private void loadFXML(URL url) {
        try {
            FXMLLoader loader = new FXMLLoader(url);
            mainBorderPane.setCenter(loader.load());
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
}

关于JavaFX 导航栏和内容 Pane ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48019690/

相关文章:

java - 如何使用 Mockito 在另一个 Mock 类中模拟 Spring 消息资源?

java - JavaFX 中的 TableView 中未显示项目

ios - 如何使用 phonegap for ios 将数据从一个页面传递到另一个页面

jquery - Bootstrap 3 事件状态导致导航栏上的错误

ios - 如何以编程方式创建 UINavigationBar

java - 测试中的 Class Hello

从回调类执行代码时,Java 性能测试会发生变化。 Java堆栈框架问题?

java - java中如何从字符串中获取值?

java - 动态 HexClock 程序

java - 为什么 JavaFX 应用程序和场景构建器显示乱码?