回答編集履歴

3

訂正

2017/02/03 20:15

投稿

KSwordOfHaste
KSwordOfHaste

スコア18394

test CHANGED
@@ -1,3 +1,125 @@
1
+ 訂正:
2
+
3
+
4
+
5
+ 元の回答のプログラムは適当すぎました。push/popと画面の対応がおかしくなりますね(次の質問に上げておられるとおりです)失礼しました。
6
+
7
+ 簡単に動かしてみたものを改めて掲げます。少々実装を変えてしまってますが雰囲気はつかんでいただけると思います。
8
+
9
+
10
+
11
+ ```java
12
+
13
+ // 以下はMainコントローラーとしてApplicationクラスの中に実装してしまってます。
14
+
15
+
16
+
17
+ public void showPage1(String text) {
18
+
19
+ pushContent(); // 現在の画面をpushしてから
20
+
21
+ replaceContent(new Page1(text)); // 新たな画面を表示する
22
+
23
+ }
24
+
25
+
26
+
27
+ public void showPage2(String text) {
28
+
29
+ pushContent();
30
+
31
+ replaceContent(new Page2(text));
32
+
33
+ }
34
+
35
+
36
+
37
+ Deque<Parent> stack = new ArrayDeque<>();
38
+
39
+ Stage stage;
40
+
41
+
42
+
43
+ // Applicationクラスのstartメソッドです
44
+
45
+ @Override
46
+
47
+ public void start(Stage stage) throws Exception{
48
+
49
+ this.stage = stage;
50
+
51
+ showPage1("page1");
52
+
53
+ stage.show();
54
+
55
+ }
56
+
57
+
58
+
59
+ // 現在表示中の画面をpush
60
+
61
+ public void pushContent() {
62
+
63
+ Scene sc = stage.getScene();
64
+
65
+ if (sc != null) {
66
+
67
+ Parent content = sc.getRoot();
68
+
69
+ assert content != null;
70
+
71
+ stack.addFirst(content);
72
+
73
+ }
74
+
75
+ }
76
+
77
+
78
+
79
+ // スタックから以前の画面をpopして表示
80
+
81
+ public void popContent() {
82
+
83
+ if (!stack.isEmpty()) {
84
+
85
+ Parent content = stack.removeFirst();
86
+
87
+ replaceContent(content);
88
+
89
+ }
90
+
91
+ }
92
+
93
+
94
+
95
+ // Sceneの表示内容を置き換える
96
+
97
+ void replaceContent(Parent content) {
98
+
99
+ Scene sc = stage.getScene();
100
+
101
+ if (sc == null) {
102
+
103
+ sc = new Scene(content);
104
+
105
+ stage.setScene(sc);
106
+
107
+ } else {
108
+
109
+ sc.setRoot(content);
110
+
111
+ }
112
+
113
+ }
114
+
115
+ ```
116
+
117
+
118
+
119
+ ---
120
+
121
+
122
+
1
123
  一例を考えてみました。
2
124
 
3
125
  (追記1:最初new Deque<>()としてました。すみません。new ArrayDeque<>()に変えました。
@@ -79,3 +201,7 @@
79
201
  }
80
202
 
81
203
  ```
204
+
205
+
206
+
207
+

2

コード修正

2017/02/03 20:15

投稿

KSwordOfHaste
KSwordOfHaste

スコア18394

test CHANGED
@@ -1,8 +1,10 @@
1
1
  一例を考えてみました。
2
2
 
3
- (追記:最初new Deque<>()としてました。すみません。new ArrayDeque<>()に変えました。コンパイルしてないので他に間違いあったらご容赦を。)
3
+ (追記1:最初new Deque<>()としてました。すみません。new ArrayDeque<>()に変えました。
4
4
 
5
+ コンパイルしてないので他に間違いあったらご容赦を。)
5
6
 
7
+ (追記2:isEmpty()の判定が逆になってたので修正しました。)
6
8
 
7
9
  ```java
8
10
 
@@ -56,7 +58,7 @@
56
58
 
57
59
  public void popAndShowContent() {
58
60
 
59
- if (contentStack.isEmpty()) {
61
+ if (!contentStack.isEmpty()) {
60
62
 
61
63
  replaceSceneContent(contentStack.removeFirst());
62
64
 

1

訂正

2017/02/03 12:27

投稿

KSwordOfHaste
KSwordOfHaste

スコア18394

test CHANGED
@@ -1,6 +1,6 @@
1
1
  一例を考えてみました。
2
2
 
3
-
3
+ (追記:最初new Deque<>()としてました。すみません。new ArrayDeque<>()に変えました。コンパイルしてないので他に間違いあったらご容赦を。)
4
4
 
5
5
 
6
6
 
@@ -10,7 +10,7 @@
10
10
 
11
11
  //
12
12
 
13
- Deque<Parent> contentStack = new Deque<>();
13
+ Deque<Parent> contentStack = new ArrayDeque<>();
14
14
 
15
15
 
16
16