回答編集履歴
1
コード等追加
test
CHANGED
@@ -12,3 +12,93 @@
|
|
12
12
|
>lst.add(resultlb);
|
13
13
|
|
14
14
|
の resultlb が null なようです。
|
15
|
+
|
16
|
+
---
|
17
|
+
イベントハンドラをラムダ式にしたり、計算クラスを作ったり、各オブジェクトの生成順を調整したりで、チェックボックスその他を配列・フィールドや変数に入れたりが不要になったりします。
|
18
|
+
|
19
|
+
また、まだ仕方ないのかもしれませんが、
|
20
|
+
```
|
21
|
+
//ステージ表示
|
22
|
+
stage.show();
|
23
|
+
```
|
24
|
+
のようなコメントはコードを日本語訳しているだけで、コメントの意味がありません。
|
25
|
+
|
26
|
+
```java
|
27
|
+
import javafx.application.Application;
|
28
|
+
import javafx.collections.ObservableList;
|
29
|
+
import javafx.event.ActionEvent;
|
30
|
+
import javafx.geometry.Insets;
|
31
|
+
import javafx.scene.Node;
|
32
|
+
import javafx.scene.Scene;
|
33
|
+
import javafx.scene.control.*;
|
34
|
+
import javafx.scene.layout.VBox;
|
35
|
+
import javafx.stage.Stage;
|
36
|
+
|
37
|
+
public class Otameshi extends Application {
|
38
|
+
private static final int CHECKBOX_COUNT = 15;
|
39
|
+
|
40
|
+
public static void main(String[] args) {
|
41
|
+
launch(args);
|
42
|
+
}
|
43
|
+
|
44
|
+
public void start(Stage stage) throws Exception {
|
45
|
+
TextField scoretf = new TextField();
|
46
|
+
Label resultlb = new Label();
|
47
|
+
|
48
|
+
Calculator calculator = new Calculator(scoretf, resultlb, CHECKBOX_COUNT);
|
49
|
+
scoretf.addEventHandler(ActionEvent.ANY, e -> calculator.addCount(0));
|
50
|
+
|
51
|
+
VBox vb = new VBox();
|
52
|
+
ObservableList<Node> lst = vb.getChildren();
|
53
|
+
|
54
|
+
lst.add(new Label("課題提出の配点を入力してください"));
|
55
|
+
lst.add(scoretf);
|
56
|
+
lst.add(new Label("課題を提出した講義を選択してください"));
|
57
|
+
|
58
|
+
for(int i=0; i<CHECKBOX_COUNT; i++) {
|
59
|
+
CheckBox cb = new CheckBox("第"+(i+1)+"回課題");
|
60
|
+
cb.setSelected(false);
|
61
|
+
cb.addEventHandler(ActionEvent.ANY, e -> calculator.addCount(cb.isSelected()?1:-1));
|
62
|
+
lst.add(cb);
|
63
|
+
}
|
64
|
+
|
65
|
+
lst.add(new Label(" "));
|
66
|
+
lst.add(resultlb);
|
67
|
+
|
68
|
+
vb.setPadding(new Insets(10));
|
69
|
+
vb.setSpacing(15);
|
70
|
+
|
71
|
+
stage.setScene(new Scene(vb));
|
72
|
+
stage.setTitle("成績計算");
|
73
|
+
stage.show();
|
74
|
+
}
|
75
|
+
|
76
|
+
private static class Calculator {
|
77
|
+
private final TextField scoretf;
|
78
|
+
private final Label resultlb;
|
79
|
+
private final int n;
|
80
|
+
private int count; //選択した数
|
81
|
+
|
82
|
+
Calculator(TextField scoretf, Label resultlb, int n) {
|
83
|
+
this.scoretf = scoretf;
|
84
|
+
this.resultlb = resultlb;
|
85
|
+
this.n = n;
|
86
|
+
|
87
|
+
count = 0;
|
88
|
+
resultlb.setText("0");
|
89
|
+
}
|
90
|
+
|
91
|
+
void addCount(int x) {
|
92
|
+
count += x;
|
93
|
+
try {
|
94
|
+
double score = Double.parseDouble(scoretf.getText());
|
95
|
+
resultlb.setText("" + (score * count / n));
|
96
|
+
} catch(NumberFormatException ignore) {
|
97
|
+
resultlb.setText("score error");
|
98
|
+
}
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
```
|
103
|
+
実行結果
|
104
|
+

|