訂正:
元の回答のプログラムは適当すぎました。push/popと画面の対応がおかしくなりますね(次の質問に上げておられるとおりです)失礼しました。
簡単に動かしてみたものを改めて掲げます。少々実装を変えてしまってますが雰囲気はつかんでいただけると思います。
java
1// 以下はMainコントローラーとしてApplicationクラスの中に実装してしまってます。
2
3public void showPage1(String text) {
4 pushContent(); // 現在の画面をpushしてから
5 replaceContent(new Page1(text)); // 新たな画面を表示する
6}
7
8public void showPage2(String text) {
9 pushContent();
10 replaceContent(new Page2(text));
11}
12
13Deque<Parent> stack = new ArrayDeque<>();
14Stage stage;
15
16// Applicationクラスのstartメソッドです
17@Override
18public void start(Stage stage) throws Exception{
19 this.stage = stage;
20 showPage1("page1");
21 stage.show();
22}
23
24// 現在表示中の画面をpush
25public void pushContent() {
26 Scene sc = stage.getScene();
27 if (sc != null) {
28 Parent content = sc.getRoot();
29 assert content != null;
30 stack.addFirst(content);
31 }
32}
33
34// スタックから以前の画面をpopして表示
35public void popContent() {
36 if (!stack.isEmpty()) {
37 Parent content = stack.removeFirst();
38 replaceContent(content);
39 }
40}
41
42// Sceneの表示内容を置き換える
43void replaceContent(Parent content) {
44 Scene sc = stage.getScene();
45 if (sc == null) {
46 sc = new Scene(content);
47 stage.setScene(sc);
48 } else {
49 sc.setRoot(content);
50 }
51}
一例を考えてみました。
(追記1:最初new Deque<>()としてました。すみません。new ArrayDeque<>()に変えました。
コンパイルしてないので他に間違いあったらご容赦を。)
(追記2:isEmpty()の判定が逆になってたので修正しました。)
java
1class MainController {
2 //
3 Deque<Parent> contentStack = new ArrayDeque<>();
4
5 public void sendPage1(...) {
6 ...
7 Page1 controller = Page1(...);
8 pushAndShowContent(controller); // 変更
9 }
10
11 public void sendpage2(String label, String _text){
12 this.text = _text;
13
14 Page2 controller = new Page2(label, text);
15 pushAndShowContent(controller); // 変更
16 }
17
18 // 新たな画面をpushして表示
19 void pushAndShowContent(Parent controller) {
20 contentStack.addFirst(controller);
21 replaceSceneContent(controller);
22 }
23
24 // 元の画面へ戻る(最初の画面だったら何もしない)
25 public void popAndShowContent() {
26 if (!contentStack.isEmpty()) {
27 replaceSceneContent(contentStack.removeFirst());
28 }
29 }
30
31 // 指定された画面を表示
32 private void replaceSceneContent(Parent controller) {
33 ... 元の実装と同じ
34 }
35}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/02/03 11:36 編集
2017/02/03 11:24
2017/02/03 11:52
2017/02/03 12:05 編集
2017/02/03 12:31
2017/02/03 13:21