简体   繁体   English

JavaFX,在同一窗口中打开一个屏幕

[英]JavaFX, Open a Screen in same Window

I'm trying to implement an application, which has a simple navigation. 我正在尝试实现一个具有简单导航的应用程序。 One Main Menu, 3 Submenus, with another 3 Submenus each. 一个主菜单,三个子菜单,每个菜单另外三个子菜单。

I need the application open every submenu recursively in the same Window with the Mainmenu as the root screen. 我需要应用程序在与Mainmenu作为根屏幕的同一窗口中递归地打开每个子菜单。 I must be able to return to that menu by going via the "Back" Button on each Submenu. 通过每个子菜单上的“返回”按钮,我必须能够返回该菜单。

I implemented a Main class, a Controller Class and a FXML-file for EACH (!) Menu and Submenu. 我为每个(!)菜单和子菜单实现了Main类,Controller类和FXML文件。

Eg my Main Menu 例如我的主菜单

      package application;

import org.apache.log4j.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;

public class Main extends Application {

    // Initialize Logger
    private static final Logger logger = Logger.getLogger(Main.class);

    @Override
    public void start(Stage primaryStage)
    {
        try
        {
            AnchorPane root = (AnchorPane)FXMLLoader.load(getClass().getResource("MainFrame.fxml"));
            Scene scene = new Scene(root,1000,500);

            primaryStage.setScene(scene);
            primaryStage.show();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args)
    {
        logger.info("Starting application.");
        launch(args);
    }
}

My MainController 我的主控制器

    package application;

import org.apache.log4j.Logger;


import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class MainFrameController
{

    private static final Logger logger = Logger.getLogger(MainFrameController.class);

    @FXML
    private Button btn_random1;
    @FXML
    private Button btn_random2;
    @FXML
    private Button btn_random3;
    @FXML
    private Button btn_random4;

    public void initialize()
    {
        //mainService = new MainService();
    }

    @FXML
    private void onRandomButton1() throws Exception
    {
        logger.info("onRandomButton1Clicked");

        Stage stage = new Stage();
        AnchorPane root;
        root = (AnchorPane)FXMLLoader.load(getClass().getResource("RandomFXML1.fxml"));
        Scene scene = new Scene(root,1000,500);
        stage.setScene(scene);
        stage.show();
    }

    @FXML
    private void onRandomButton2()
    {
        logger.info("onRandomButton1");
    }

    @FXML
    private void onRandomButton3()
    {
        logger.info("onRandomButton2");

    }
    @FXML
    private void onRandomButton4()
    {
        Platform.exit();
        logger.info("onRandomButton3");
    }
}

Is there a way to simply change my code, so it does open in the same window? 有没有一种方法可以简单地更改我的代码,使其在同一窗口中打开?

I took a look at several tutorials with relatively complex ways of solving this, I'd like to stick to my code and not changing too much, otherwise I'd have to start all over again. 我看了一些使用相对复杂的方法来解决这个问题的教程,我想坚持我的代码并且不要做太多改动,否则我将不得不重新开始。

Pls note, that this is only one of many Main/Controller/FXML combinations, I have about 10 screens and "subscreens", which are being navigated like this (by java opening a new window). 请注意,这只是许多Main / Controller / FXML组合中的一种,我大约有10个屏幕和“子屏幕”,它们正像这样被导航(通过Java打开一个新窗口)。

Ideas anyone? 有任何想法吗? Or maybe a relatively simple tutorial (for which I dont have to change my whole code)? 还是一个相对简单的教程(我不必更改整个代码)?

Thanks! 谢谢!

Have an empty controller at the root (or perhaps with a single empty anchorpane) and have it open the other controllers and add it to the current pane? 在根目录处有一个空的控制器(或者可能有一个空的锚定窗格),并使其打开其他控制器并将其添加到当前窗格中?

I currently have a similar setup but with a tab pane: each module is loaded into a separate tab. 我目前有一个类似的设置,但是有一个选项卡窗格:每个模块都加载到一个单独的选项卡中。 Each module itself has an fxml file, a controller etc. The core code dynamically creates new tabs etc for each module and loads them. 每个模块本身都有一个fxml文件,一个控制器等。核心代码为每个模块动态创建新选项卡等并加载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM