回答編集履歴

2

見直しキャンペーン中

2023/07/23 05:52

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,279 +1,137 @@
1
1
  アクセラレータがいいんじゃないでしょうか。
2
-
3
2
  シーンが複数ある場合は、おそらくシーンごとに設定するしかなさそうです。
4
3
 
5
-
6
-
7
- App.java
4
+ ```Java::App.java
8
-
9
- ```Java
10
-
11
5
  package sample;
12
6
 
13
-
14
-
15
7
  import javafx.application.Application;
16
-
17
8
  import javafx.fxml.FXMLLoader;
18
-
19
9
  import javafx.scene.Parent;
20
-
21
10
  import javafx.scene.Scene;
22
-
23
11
  import javafx.scene.input.KeyCodeCombination;
24
-
25
12
  import javafx.stage.Stage;
26
13
 
27
-
28
-
29
14
  public class App extends Application {
30
-
31
15
  @Override
32
-
33
16
  public void start(Stage primaryStage) throws Exception {
34
-
35
17
  FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
36
-
37
18
  Parent parent = loader.load();
38
-
39
19
  Scene scene = new Scene(parent);
40
-
41
20
  MainController controller = loader.<MainController>getController();
42
-
43
21
  scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
44
-
45
22
  primaryStage.setScene(scene);
46
-
47
23
  primaryStage.setTitle("メイン");
48
-
49
24
  primaryStage.show();
50
-
51
25
  }
52
26
 
53
-
54
-
55
27
  public static void main(String[] args) { launch(args); }
56
-
57
28
  }
58
-
59
29
  ```
60
30
 
61
-
62
-
63
- main.fxml
31
+ ```xml:main.fxml
64
-
65
- ```xml
66
-
67
32
  <?xml version="1.0" encoding="UTF-8"?>
68
33
 
34
+ <?import javafx.scene.control.Button?>
35
+ <?import javafx.scene.layout.BorderPane?>
69
36
 
37
+ <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
38
+ prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
39
+ fx:controller="sample.MainController">
40
+ <center>
41
+ <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="サブへ"
42
+ BorderPane.alignment="CENTER"/>
43
+ </center>
44
+ </BorderPane>
45
+ ```
46
+
47
+ ```Java:MainController.java
48
+ package sample;
49
+
50
+ import javafx.event.ActionEvent;
51
+ import javafx.fxml.FXML;
52
+ import javafx.fxml.FXMLLoader;
53
+ import javafx.scene.Node;
54
+ import javafx.scene.Parent;
55
+ import javafx.scene.Scene;
56
+ import javafx.scene.control.Button;
57
+ import javafx.scene.input.KeyCodeCombination;
58
+ import javafx.stage.Stage;
59
+ import javafx.stage.Window;
60
+
61
+ public class MainController {
62
+ @FXML
63
+ Button button;
64
+
65
+ @FXML
66
+ protected void onButtonClick(ActionEvent evt) throws Exception {
67
+ Scene s = ((Node) evt.getSource()).getScene();
68
+ Window w = s.getWindow();
69
+ w.hide();
70
+
71
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("sub.fxml"));
72
+ Parent parent = loader.load();
73
+ Scene scene = new Scene(parent);
74
+ SubController controller = loader.<SubController>getController();
75
+ scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
76
+ Stage stage = new Stage();
77
+ stage.setScene(scene);
78
+ stage.setTitle("サブ");
79
+ stage.show();
80
+ }
81
+ }
82
+ ```
83
+
84
+ ```xml:sub.fxml
85
+ <?xml version="1.0" encoding="UTF-8"?>
70
86
 
71
87
  <?import javafx.scene.control.Button?>
72
-
73
88
  <?import javafx.scene.layout.BorderPane?>
74
89
 
75
-
76
-
77
- <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
90
+ <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0"
78
-
79
- prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
91
+ prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
80
-
81
- fx:controller="sample.MainController">
92
+ fx:controller="sample.SubController">
82
-
83
93
  <center>
84
-
85
- <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="サブへ"
94
+ <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="メインへ"
86
-
87
95
  BorderPane.alignment="CENTER"/>
88
-
89
96
  </center>
90
-
91
97
  </BorderPane>
92
-
93
98
  ```
94
99
 
95
- MainController.java
100
+ ```Java:SubController.java
96
-
97
- ```Java
98
-
99
101
  package sample;
100
-
101
-
102
-
103
- import javafx.event.ActionEvent;
104
-
105
- import javafx.fxml.FXML;
106
-
107
- import javafx.fxml.FXMLLoader;
108
-
109
- import javafx.scene.Node;
110
-
111
- import javafx.scene.Parent;
112
-
113
- import javafx.scene.Scene;
114
-
115
- import javafx.scene.control.Button;
116
-
117
- import javafx.scene.input.KeyCodeCombination;
118
-
119
- import javafx.stage.Stage;
120
-
121
- import javafx.stage.Window;
122
-
123
-
124
-
125
- public class MainController {
126
-
127
- @FXML
128
-
129
- Button button;
130
-
131
-
132
-
133
- @FXML
134
-
135
- protected void onButtonClick(ActionEvent evt) throws Exception {
136
-
137
- Scene s = ((Node) evt.getSource()).getScene();
138
-
139
- Window w = s.getWindow();
140
-
141
- w.hide();
142
-
143
-
144
-
145
- FXMLLoader loader = new FXMLLoader(getClass().getResource("sub.fxml"));
146
-
147
- Parent parent = loader.load();
148
-
149
- Scene scene = new Scene(parent);
150
-
151
- SubController controller = loader.<SubController>getController();
152
-
153
- scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
154
-
155
- Stage stage = new Stage();
156
-
157
- stage.setScene(scene);
158
-
159
- stage.setTitle("サブ");
160
-
161
- stage.show();
162
-
163
- }
164
-
165
- }
166
-
167
- ```
168
-
169
-
170
-
171
- sub.fxml
172
-
173
- ```xml
174
-
175
- <?xml version="1.0" encoding="UTF-8"?>
176
-
177
-
178
-
179
- <?import javafx.scene.control.Button?>
180
-
181
- <?import javafx.scene.layout.BorderPane?>
182
-
183
-
184
-
185
- <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0"
186
-
187
- prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
188
-
189
- fx:controller="sample.SubController">
190
-
191
- <center>
192
-
193
- <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="メインへ"
194
-
195
- BorderPane.alignment="CENTER"/>
196
-
197
- </center>
198
-
199
- </BorderPane>
200
-
201
- ```
202
-
203
- SubController.java
204
-
205
- ```Java
206
-
207
- package sample;
208
-
209
-
210
102
 
211
103
  import java.io.IOException;
212
104
 
213
-
214
-
215
105
  import javafx.event.ActionEvent;
216
-
217
106
  import javafx.fxml.FXML;
218
-
219
107
  import javafx.fxml.FXMLLoader;
220
-
221
108
  import javafx.scene.Node;
222
-
223
109
  import javafx.scene.Parent;
224
-
225
110
  import javafx.scene.Scene;
226
-
227
111
  import javafx.scene.control.Button;
228
-
229
112
  import javafx.scene.input.KeyCodeCombination;
230
-
231
113
  import javafx.stage.Stage;
232
-
233
114
  import javafx.stage.Window;
234
115
 
235
-
236
-
237
116
  public class SubController {
117
+ @FXML
118
+ Button button;
238
119
 
239
120
  @FXML
240
-
241
- Button button;
242
-
243
-
244
-
245
- @FXML
246
-
247
121
  protected void onButtonClick(ActionEvent evt) throws IOException {
248
-
249
122
  Scene s = ((Node) evt.getSource()).getScene();
250
-
251
123
  Window w = s.getWindow();
252
-
253
124
  w.hide();
254
125
 
255
-
256
-
257
126
  FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
258
-
259
127
  Parent parent = loader.load();
260
-
261
128
  Scene scene = new Scene(parent);
262
-
263
129
  MainController controller = loader.<MainController>getController();
264
-
265
130
  scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
266
-
267
131
  Stage stage = new Stage();
268
-
269
132
  stage.setScene(scene);
270
-
271
133
  stage.setTitle("メイン");
272
-
273
134
  stage.show();
274
-
275
135
  }
276
-
277
136
  }
278
-
279
137
  ```

1

2ウィンドウ

2020/09/16 04:21

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
 
6
6
 
7
+ App.java
8
+
7
9
  ```Java
8
10
 
9
11
  package sample;
@@ -24,24 +26,26 @@
24
26
 
25
27
 
26
28
 
27
- public class Main extends Application {
29
+ public class App extends Application {
28
30
 
29
31
  @Override
30
32
 
31
33
  public void start(Stage primaryStage) throws Exception {
32
34
 
33
- FXMLLoader loader = new FXMLLoader(getClass().getResource("sample.fxml"));
35
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
34
-
36
+
35
- Parent root = loader.load();
37
+ Parent parent = loader.load();
36
-
38
+
37
- Scene scene = new Scene(root);
39
+ Scene scene = new Scene(parent);
38
-
40
+
39
- SampleController controller = loader.<SampleController>getController();
41
+ MainController controller = loader.<MainController>getController();
40
42
 
41
43
  scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
42
44
 
43
45
  primaryStage.setScene(scene);
44
46
 
47
+ primaryStage.setTitle("メイン");
48
+
45
49
  primaryStage.show();
46
50
 
47
51
  }
@@ -54,6 +58,42 @@
54
58
 
55
59
  ```
56
60
 
61
+
62
+
63
+ main.fxml
64
+
65
+ ```xml
66
+
67
+ <?xml version="1.0" encoding="UTF-8"?>
68
+
69
+
70
+
71
+ <?import javafx.scene.control.Button?>
72
+
73
+ <?import javafx.scene.layout.BorderPane?>
74
+
75
+
76
+
77
+ <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
78
+
79
+ prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
80
+
81
+ fx:controller="sample.MainController">
82
+
83
+ <center>
84
+
85
+ <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="サブへ"
86
+
87
+ BorderPane.alignment="CENTER"/>
88
+
89
+ </center>
90
+
91
+ </BorderPane>
92
+
93
+ ```
94
+
95
+ MainController.java
96
+
57
97
  ```Java
58
98
 
59
99
  package sample;
@@ -64,11 +104,25 @@
64
104
 
65
105
  import javafx.fxml.FXML;
66
106
 
107
+ import javafx.fxml.FXMLLoader;
108
+
109
+ import javafx.scene.Node;
110
+
111
+ import javafx.scene.Parent;
112
+
113
+ import javafx.scene.Scene;
114
+
67
115
  import javafx.scene.control.Button;
68
116
 
69
-
117
+ import javafx.scene.input.KeyCodeCombination;
118
+
70
-
119
+ import javafx.stage.Stage;
120
+
121
+ import javafx.stage.Window;
122
+
123
+
124
+
71
- public class SampleController {
125
+ public class MainController {
72
126
 
73
127
  @FXML
74
128
 
@@ -78,9 +132,33 @@
78
132
 
79
133
  @FXML
80
134
 
81
- protected void onButtonClick(ActionEvent evt) {
135
+ protected void onButtonClick(ActionEvent evt) throws Exception {
136
+
82
-
137
+ Scene s = ((Node) evt.getSource()).getScene();
138
+
139
+ Window w = s.getWindow();
140
+
141
+ w.hide();
142
+
143
+
144
+
145
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("sub.fxml"));
146
+
147
+ Parent parent = loader.load();
148
+
149
+ Scene scene = new Scene(parent);
150
+
151
+ SubController controller = loader.<SubController>getController();
152
+
153
+ scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
154
+
155
+ Stage stage = new Stage();
156
+
157
+ stage.setScene(scene);
158
+
83
- System.out.println("onButtonClick");
159
+ stage.setTitle("サブ");
160
+
161
+ stage.show();
84
162
 
85
163
  }
86
164
 
@@ -88,6 +166,10 @@
88
166
 
89
167
  ```
90
168
 
169
+
170
+
171
+ sub.fxml
172
+
91
173
  ```xml
92
174
 
93
175
  <?xml version="1.0" encoding="UTF-8"?>
@@ -100,15 +182,15 @@
100
182
 
101
183
 
102
184
 
103
- <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0"
185
+ <BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="300.0"
104
-
186
+
105
- prefWidth="600.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
187
+ prefWidth="400.0" xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1"
106
-
188
+
107
- fx:controller="sample.SampleController">
189
+ fx:controller="sample.SubController">
108
190
 
109
191
  <center>
110
192
 
111
- <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="Button"
193
+ <Button fx:id="button" mnemonicParsing="false" onAction="#onButtonClick" text="メインへ"
112
194
 
113
195
  BorderPane.alignment="CENTER"/>
114
196
 
@@ -117,3 +199,81 @@
117
199
  </BorderPane>
118
200
 
119
201
  ```
202
+
203
+ SubController.java
204
+
205
+ ```Java
206
+
207
+ package sample;
208
+
209
+
210
+
211
+ import java.io.IOException;
212
+
213
+
214
+
215
+ import javafx.event.ActionEvent;
216
+
217
+ import javafx.fxml.FXML;
218
+
219
+ import javafx.fxml.FXMLLoader;
220
+
221
+ import javafx.scene.Node;
222
+
223
+ import javafx.scene.Parent;
224
+
225
+ import javafx.scene.Scene;
226
+
227
+ import javafx.scene.control.Button;
228
+
229
+ import javafx.scene.input.KeyCodeCombination;
230
+
231
+ import javafx.stage.Stage;
232
+
233
+ import javafx.stage.Window;
234
+
235
+
236
+
237
+ public class SubController {
238
+
239
+ @FXML
240
+
241
+ Button button;
242
+
243
+
244
+
245
+ @FXML
246
+
247
+ protected void onButtonClick(ActionEvent evt) throws IOException {
248
+
249
+ Scene s = ((Node) evt.getSource()).getScene();
250
+
251
+ Window w = s.getWindow();
252
+
253
+ w.hide();
254
+
255
+
256
+
257
+ FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
258
+
259
+ Parent parent = loader.load();
260
+
261
+ Scene scene = new Scene(parent);
262
+
263
+ MainController controller = loader.<MainController>getController();
264
+
265
+ scene.getAccelerators().put(KeyCodeCombination.valueOf("F2"), () -> controller.button.fire());
266
+
267
+ Stage stage = new Stage();
268
+
269
+ stage.setScene(scene);
270
+
271
+ stage.setTitle("メイン");
272
+
273
+ stage.show();
274
+
275
+ }
276
+
277
+ }
278
+
279
+ ```