JavaFX,在根窗口中切换窗格并保留内存 [英] JavaFX, switching panes in a root window and retaining memory

查看:239
本文介绍了JavaFX,在根窗口中切换窗格并保留内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题中所述,我有fxml文件,我有一个设置了顶部有三个标签/按钮的UI,窗口的下半部分有一个窗格。每次单击标签/按钮时,窗格必须切换到相应的fxml文件。换句话说,窗格必须始终位于相同的位置,有点像标签布局但没有标签。

As stated in the title, I have fxml files, I have a UI that is set up with three labels/buttons up top and the lower half of the window has a pane. Every time a label/button is clicked, the pane must switch to that corresponding fxml file. So in other words, the pane must always be in the same position, kind of like a tabbed layout but without tabs.

我知道我只需加载一个fxml文件的新实例但是,我想避免这种情况,因为当用户点击他之前所使用的标签时,他应该能够看到他之前的输入。

I know I can achieve this with just loading a new instance of an fxml file but, I want to avoid that because when a user click on a tab he previously was on, he should be able to see his earlier input.

我有一些启动程序的main.java。一些controller.java在首次加载时控制UI,以及一些与该初始视图对应的fxml文件。如何实现此转换功能?附:我是JavaFX的新手。

I have some main.java that starts the program. Some controller.java that controls the UI when it is first loaded, and some fxml file corresponding to that initial view. How can I go about implementing this transition functionality? P.S. I am very novice at JavaFX.

推荐答案

这是 MCVE 如何实现它。

它当然可以使用 FXML实现

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class StageTest extends Application{

    private Pane pane1, pane2, mainPane;

    @Override
    public void start(Stage stage) throws Exception {

        stage.setTitle("Switch Panes");
        Button button1 = new Button("Show Pane 1");
        button1.setOnAction(e -> showPane1());
        Button button2 = new Button("Show Pane 2");
        button2.setOnAction(e -> showPane2());

        HBox buttonsPane = new HBox(5.);
        buttonsPane.getChildren().addAll(button1, button2);

        pane1 = getPane("PANE ONE");
        pane2 = getPane("PANE TWO");
        mainPane = new StackPane(pane1);

        BorderPane root = new BorderPane();
        root.setTop(buttonsPane);
        root.setCenter(mainPane);

        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
    }


    private void showPane1() {
        mainPane.getChildren().clear();
        mainPane.getChildren().add(pane1);
    }

    private void showPane2() {
        mainPane.getChildren().clear();
        mainPane.getChildren().add(pane2);
    }

    private Pane getPane(String txt) {

        VBox pane = new VBox();
        pane.getChildren().addAll(new TextArea(txt+" add text here: "));
        return pane;
    }

    public static void main(String[] args) {
        launch(args);
    }
}

这篇关于JavaFX,在根窗口中切换窗格并保留内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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