[英]JavaFX, switching panes in a root window and retaining memory
如标题中所述,我有fxml文件,我有一个UI,该UI的顶部设置了三个标签/按钮,而窗口的下半部分具有一个窗格。 每次单击标签/按钮时,窗格都必须切换到相应的fxml文件。 因此,换句话说,窗格必须始终位于同一位置,有点像选项卡式布局,但没有选项卡。
我知道我可以通过加载fxml文件的新实例来实现此目的,但是,我想避免这种情况,因为当用户单击以前使用的选项卡时,他应该能够看到其先前的输入。
我有一些main.java启动程序。 一些controller.java在首次加载UI时控制UI,还有一些与该初始视图相对应的fxml文件。 我该如何实现此过渡功能? PS:我是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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.