回答編集履歴

3

こういう可能性?

2020/04/13 03:52

投稿

TN8001
TN8001

スコア9432

test CHANGED
@@ -37,6 +37,10 @@
37
37
 
38
38
 
39
39
  import javafx.application.Application;
40
+
41
+ import javafx.event.ActionEvent;
42
+
43
+ import javafx.event.EventHandler;
40
44
 
41
45
  import javafx.geometry.Insets;
42
46
 
@@ -92,17 +96,29 @@
92
96
 
93
97
  final Button change = new Button("Change");
94
98
 
99
+
100
+
101
+ // textがどこかで変更されているんではないだろうか?
102
+
103
+ // 今のtextをtに保存
104
+
105
+ Text t = text;
106
+
95
107
  change.setOnAction(new EventHandler<ActionEvent>() {
96
108
 
97
109
  @Override public void handle(ActionEvent changeText) {
98
110
 
111
+ // 保存したtを使う
112
+
99
- text.setText("変更されたテキスト");
113
+ t.setText("変更されたテキスト");
114
+
115
+ // こちらは「新しいインスタンス」になっているので表示されている"最初のテキスト"は変わらない
116
+
117
+ // text.setText("変更されたテキスト");
100
118
 
101
119
  }
102
120
 
103
121
  });
104
-
105
-
106
122
 
107
123
  hb.getChildren().addAll(change);
108
124
 
@@ -116,12 +132,20 @@
116
132
 
117
133
  vbox.getChildren().addAll(text, hb);
118
134
 
135
+
136
+
119
137
  ((Group) scene.getRoot()).getChildren().addAll(vbox);
120
138
 
121
139
  stage.setScene(scene);
122
140
 
123
141
  stage.show();
124
142
 
143
+
144
+
145
+ // これならすぐ気づくがどこかで変更された というつもり
146
+
147
+ text = new Text("新しいインスタンス");
148
+
125
149
  }
126
150
 
127
151
  }

2

匿名クラス

2020/04/13 03:52

投稿

TN8001
TN8001

スコア9432

test CHANGED
@@ -92,7 +92,15 @@
92
92
 
93
93
  final Button change = new Button("Change");
94
94
 
95
+ change.setOnAction(new EventHandler<ActionEvent>() {
96
+
97
+ @Override public void handle(ActionEvent changeText) {
98
+
95
- change.setOnAction(e -> text.setText("変更されたテキスト"));
99
+ text.setText("変更されたテキスト");
100
+
101
+ }
102
+
103
+ });
96
104
 
97
105
 
98
106
 

1

全文追記

2020/04/13 03:25

投稿

TN8001
TN8001

スコア9432

test CHANGED
@@ -19,3 +19,103 @@
19
19
  //change.setOnAction(e -> text.setText("変更されたテキスト"));
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ ---
26
+
27
+
28
+
29
+ 全文追記
30
+
31
+
32
+
33
+ ```Java
34
+
35
+ import java.io.IOException;
36
+
37
+
38
+
39
+ import javafx.application.Application;
40
+
41
+ import javafx.geometry.Insets;
42
+
43
+ import javafx.scene.Group;
44
+
45
+ import javafx.scene.Scene;
46
+
47
+ import javafx.scene.control.Button;
48
+
49
+ import javafx.scene.layout.HBox;
50
+
51
+ import javafx.scene.layout.VBox;
52
+
53
+ import javafx.scene.text.Text;
54
+
55
+ import javafx.stage.Stage;
56
+
57
+
58
+
59
+
60
+
61
+ public class test extends Application {
62
+
63
+ final HBox hb = new HBox();
64
+
65
+ private static Text text = new Text();
66
+
67
+
68
+
69
+ public static void main(String[] args) {
70
+
71
+ launch(args);
72
+
73
+ }
74
+
75
+
76
+
77
+ @Override
78
+
79
+ public void start(Stage stage) throws IOException {
80
+
81
+ text.setText("最初のテキスト");
82
+
83
+ Scene scene = new Scene(new Group());
84
+
85
+ stage.setTitle("Test");
86
+
87
+ stage.setWidth(500);
88
+
89
+ stage.setHeight(500);
90
+
91
+
92
+
93
+ final Button change = new Button("Change");
94
+
95
+ change.setOnAction(e -> text.setText("変更されたテキスト"));
96
+
97
+
98
+
99
+ hb.getChildren().addAll(change);
100
+
101
+
102
+
103
+ final VBox vbox = new VBox();
104
+
105
+ vbox.setSpacing(5);
106
+
107
+ vbox.setPadding(new Insets(10, 0, 50, 10));
108
+
109
+ vbox.getChildren().addAll(text, hb);
110
+
111
+ ((Group) scene.getRoot()).getChildren().addAll(vbox);
112
+
113
+ stage.setScene(scene);
114
+
115
+ stage.show();
116
+
117
+ }
118
+
119
+ }
120
+
121
+ ```