回答編集履歴
8
コード修正
test
CHANGED
@@ -33,7 +33,7 @@
|
|
33
33
|
//コンストラクタ内でオブジェクトの利用記述が完結してしまえば変数をフィールドにする必要は無いし、コンポーネントをキャッシュするような処理も要らない
|
34
34
|
JLabel label = new JLabel(INIT_VALUE); //起動時のラベルの数字も 1 → 初期表示はコンストラクトパラメータで設定できる
|
35
35
|
|
36
|
-
int addends
|
36
|
+
int[] addends = new int[]{ 1, 10, 100 }; //加算数. この配列を修正するとボタンも変わる
|
37
37
|
JPanel buttonPanel = new JPanel(new GridLayout(1, addends.length + 1)); //JPanel のレイアウトはコンストラクトパラメータで設定できる
|
38
38
|
//「+1 ボタン」「+10 ボタン」「+100 ボタン」はそれぞれラベルの数字を 1,10,100 だけ増加させる.
|
39
39
|
for(int addend : addends) buttonPanel.add(createAdditionButton(label, addend));
|
7
コード修正
test
CHANGED
@@ -33,11 +33,10 @@
|
|
33
33
|
//コンストラクタ内でオブジェクトの利用記述が完結してしまえば変数をフィールドにする必要は無いし、コンポーネントをキャッシュするような処理も要らない
|
34
34
|
JLabel label = new JLabel(INIT_VALUE); //起動時のラベルの数字も 1 → 初期表示はコンストラクトパラメータで設定できる
|
35
35
|
|
36
|
+
int addends[] = new int[]{ 1, 10, 100 }; //加算数. この配列を修正するとボタンも変わる
|
36
|
-
JPanel buttonPanel = new JPanel(new GridLayout(1,
|
37
|
+
JPanel buttonPanel = new JPanel(new GridLayout(1, addends.length + 1)); //JPanel のレイアウトはコンストラクトパラメータで設定できる
|
37
38
|
//「+1 ボタン」「+10 ボタン」「+100 ボタン」はそれぞれラベルの数字を 1,10,100 だけ増加させる.
|
38
|
-
buttonPanel.add(createAdditionButton(label, 1));
|
39
|
-
buttonPanel.add(createAdditionButton(label, 10));
|
40
|
-
buttonPanel.add(createAdditionButton(label,
|
39
|
+
for(int addend : addends) buttonPanel.add(createAdditionButton(label, addend));
|
41
40
|
//「リセットボタン」はラベルの数字を 1 に初期化
|
42
41
|
buttonPanel.add(createButton("リセットボタン", e -> label.setText(INIT_VALUE))); //ActionListener は lamda 式で書ける
|
43
42
|
|
@@ -53,7 +52,7 @@
|
|
53
52
|
* @param addend 増加させる数
|
54
53
|
*/
|
55
54
|
private JButton createAdditionButton(JLabel label, int addend) {
|
56
|
-
return createButton("+"
|
55
|
+
return createButton(String.format("%+dボタン", addend), e -> addTo(label, addend)); //addend をラベルとアクションの両方に使用することで、ラベルと動作を一致させる(バグ防止)
|
57
56
|
}
|
58
57
|
/**
|
59
58
|
* ボタンの生成
|
6
コード修正・変更
test
CHANGED
@@ -13,42 +13,33 @@
|
|
13
13
|
他にも色々(クセのある)書き方を整理すると、以下のようなコードにもなります。
|
14
14
|
```java
|
15
15
|
import java.awt.GridLayout;
|
16
|
+
import java.awt.event.ActionListener;
|
16
17
|
|
17
18
|
import javax.swing.*; //import は * で纏めることができる
|
18
19
|
|
19
|
-
public class Kihon4 extends JFrame { //クラスをただの箱にするくらいなら役割を持たせる
|
20
|
+
public class Kihon4Frame extends JFrame { //クラスをただの箱にするくらいなら役割を持たせる
|
20
21
|
public static void main(String[] args) {
|
21
|
-
SwingUtilities.invokeLater(() -> new Kihon4().setVisible(true)); //Runnable は lamda 式で書ける
|
22
|
+
SwingUtilities.invokeLater(() -> new Kihon4Frame().setVisible(true)); //Runnable は lamda 式で書ける
|
22
23
|
}
|
23
24
|
|
25
|
+
private static final String INIT_VALUE = "1"; //ラベル初期値
|
26
|
+
|
24
|
-
Kihon4() {
|
27
|
+
Kihon4Frame() {
|
25
28
|
super("基本課題4"); //ウィンドウのタイトルに「基本課題 4」と表示 → タイトルは JFrame のコンストラクトパラメータで設定できる
|
26
29
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
27
30
|
setSize(640, 240); //ウィンドウサイズは横 640 画素,縦 240 画素
|
28
31
|
setLocationRelativeTo(null); //プログラムの起動時にディスプレイの中央に表示
|
29
32
|
|
30
33
|
//コンストラクタ内でオブジェクトの利用記述が完結してしまえば変数をフィールドにする必要は無いし、コンポーネントをキャッシュするような処理も要らない
|
31
|
-
JLabel label = new JLabel(
|
34
|
+
JLabel label = new JLabel(INIT_VALUE); //起動時のラベルの数字も 1 → 初期表示はコンストラクトパラメータで設定できる
|
32
|
-
|
33
|
-
//「+1 ボタン」「+10 ボタン」「+100 ボタン」はそれぞれラベルの数字を 1,10,100 だけ増加させる.
|
34
|
-
JButton plus1Button = new JButton("+1ボタン"); //ボタンの表示テキストはコンストラクトパラメータで設定できる
|
35
|
-
plus1Button.addActionListener(e -> addTo(label, 1)); //ActionListener は lamda 式で書ける
|
36
|
-
|
37
|
-
JButton plus10Button = new JButton("+10ボタン");
|
38
|
-
plus10Button.addActionListener(e -> addTo(label, 10));
|
39
|
-
|
40
|
-
JButton plus100Button = new JButton("+100ボタン");
|
41
|
-
plus100Button.addActionListener(e -> addTo(label, 100));
|
42
|
-
|
43
|
-
//「リセットボタン」はラベルの数字を 1 に初期化
|
44
|
-
JButton resetButton = new JButton("リセットボタン");
|
45
|
-
resetButton.addActionListener(e -> label.setText("1"));
|
46
35
|
|
47
36
|
JPanel buttonPanel = new JPanel(new GridLayout(1, 4)); //JPanel のレイアウトはコンストラクトパラメータで設定できる
|
48
|
-
buttonPanel.add(plus1Button);
|
49
|
-
buttonPanel.add(plus10Button);
|
50
|
-
|
37
|
+
//「+1 ボタン」「+10 ボタン」「+100 ボタン」はそれぞれラベルの数字を 1,10,100 だけ増加させる.
|
51
|
-
buttonPanel.add(re
|
38
|
+
buttonPanel.add(createAdditionButton(label, 1));
|
39
|
+
buttonPanel.add(createAdditionButton(label, 10));
|
40
|
+
buttonPanel.add(createAdditionButton(label, 100));
|
41
|
+
//「リセットボタン」はラベルの数字を 1 に初期化
|
42
|
+
buttonPanel.add(createButton("リセットボタン", e -> label.setText(INIT_VALUE))); //ActionListener は lamda 式で書ける
|
52
43
|
|
53
44
|
JPanel contentPane = new JPanel(new GridLayout(2, 1));
|
54
45
|
contentPane.add(label);
|
@@ -56,15 +47,31 @@
|
|
56
47
|
|
57
48
|
setContentPane(contentPane);
|
58
49
|
}
|
59
|
-
|
50
|
+
/**
|
51
|
+
* ラベル加算ボタンの生成
|
52
|
+
* @param label ラベル
|
53
|
+
* @param addend 増加させる数
|
54
|
+
*/
|
55
|
+
private JButton createAdditionButton(JLabel label, int addend) {
|
56
|
+
return createButton("+" + addend + "ボタン", e -> addTo(label, addend)); //addend をラベルとアクションの両方に使用することで、ラベルと動作を一致させる(バグ防止)
|
57
|
+
}
|
58
|
+
/**
|
59
|
+
* ボタンの生成
|
60
|
+
* @param text ボタンテキスト
|
61
|
+
* @param l アクションリスナ
|
62
|
+
*/
|
63
|
+
private JButton createButton(String text, ActionListener l) {
|
64
|
+
JButton button = new JButton(text); //ボタンの表示テキストはコンストラクトパラメータで設定できる
|
65
|
+
button.addActionListener(l);
|
66
|
+
return button;
|
67
|
+
}
|
60
68
|
/**
|
61
69
|
* ラベルの数字を増加させる
|
62
70
|
* @param label ラベル
|
63
|
-
* @param a
|
71
|
+
* @param addend 増加させる数
|
64
72
|
*/
|
65
|
-
private void addTo(JLabel label, int a
|
73
|
+
private void addTo(JLabel label, int addend) {
|
66
|
-
//ラベルの表示を数値化→増加→文字列してラベルに設定すれば、別途「値」を変数として持つ必要は無い
|
67
|
-
label.setText("" + (Integer.parseInt(label.getText()) + a
|
74
|
+
label.setText("" + (Integer.parseInt(label.getText()) + addend)); //ラベルの表示を数値化→増加→文字列してラベルに設定すれば、別途「値」を変数として持つ必要は無い
|
68
75
|
}
|
69
76
|
}
|
70
77
|
```
|
5
コード追加
test
CHANGED
@@ -8,3 +8,63 @@
|
|
8
8
|
|
9
9
|
JPanel に add 出来るのは Component です。 JButton も JLabel も JPanel も Component です。
|
10
10
|
ボタンのパネルに『ボタンを横に4つ並べられている』のですから、パネルに『ラベルとパネルを縦に2つ並べる』ことも出来るはずです。
|
11
|
+
|
12
|
+
---
|
13
|
+
他にも色々(クセのある)書き方を整理すると、以下のようなコードにもなります。
|
14
|
+
```java
|
15
|
+
import java.awt.GridLayout;
|
16
|
+
|
17
|
+
import javax.swing.*; //import は * で纏めることができる
|
18
|
+
|
19
|
+
public class Kihon4 extends JFrame { //クラスをただの箱にするくらいなら役割を持たせる
|
20
|
+
public static void main(String[] args) {
|
21
|
+
SwingUtilities.invokeLater(() -> new Kihon4().setVisible(true)); //Runnable は lamda 式で書ける
|
22
|
+
}
|
23
|
+
|
24
|
+
Kihon4() {
|
25
|
+
super("基本課題4"); //ウィンドウのタイトルに「基本課題 4」と表示 → タイトルは JFrame のコンストラクトパラメータで設定できる
|
26
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
27
|
+
setSize(640, 240); //ウィンドウサイズは横 640 画素,縦 240 画素
|
28
|
+
setLocationRelativeTo(null); //プログラムの起動時にディスプレイの中央に表示
|
29
|
+
|
30
|
+
//コンストラクタ内でオブジェクトの利用記述が完結してしまえば変数をフィールドにする必要は無いし、コンポーネントをキャッシュするような処理も要らない
|
31
|
+
JLabel label = new JLabel("1"); //起動時のラベルの数字も 1 → 初期表示はコンストラクトパラメータで設定できる
|
32
|
+
|
33
|
+
//「+1 ボタン」「+10 ボタン」「+100 ボタン」はそれぞれラベルの数字を 1,10,100 だけ増加させる.
|
34
|
+
JButton plus1Button = new JButton("+1ボタン"); //ボタンの表示テキストはコンストラクトパラメータで設定できる
|
35
|
+
plus1Button.addActionListener(e -> addTo(label, 1)); //ActionListener は lamda 式で書ける
|
36
|
+
|
37
|
+
JButton plus10Button = new JButton("+10ボタン");
|
38
|
+
plus10Button.addActionListener(e -> addTo(label, 10));
|
39
|
+
|
40
|
+
JButton plus100Button = new JButton("+100ボタン");
|
41
|
+
plus100Button.addActionListener(e -> addTo(label, 100));
|
42
|
+
|
43
|
+
//「リセットボタン」はラベルの数字を 1 に初期化
|
44
|
+
JButton resetButton = new JButton("リセットボタン");
|
45
|
+
resetButton.addActionListener(e -> label.setText("1"));
|
46
|
+
|
47
|
+
JPanel buttonPanel = new JPanel(new GridLayout(1, 4)); //JPanel のレイアウトはコンストラクトパラメータで設定できる
|
48
|
+
buttonPanel.add(plus1Button);
|
49
|
+
buttonPanel.add(plus10Button);
|
50
|
+
buttonPanel.add(plus100Button);
|
51
|
+
buttonPanel.add(resetButton);
|
52
|
+
|
53
|
+
JPanel contentPane = new JPanel(new GridLayout(2, 1));
|
54
|
+
contentPane.add(label);
|
55
|
+
contentPane.add(buttonPanel);
|
56
|
+
|
57
|
+
setContentPane(contentPane);
|
58
|
+
}
|
59
|
+
|
60
|
+
/**
|
61
|
+
* ラベルの数字を増加させる
|
62
|
+
* @param label ラベル
|
63
|
+
* @param augend 増加させる数
|
64
|
+
*/
|
65
|
+
private void addTo(JLabel label, int augend) {
|
66
|
+
//ラベルの表示を数値化→増加→文字列してラベルに設定すれば、別途「値」を変数として持つ必要は無い
|
67
|
+
label.setText("" + (Integer.parseInt(label.getText()) + augend));
|
68
|
+
}
|
69
|
+
}
|
70
|
+
```
|
4
修正
test
CHANGED
@@ -2,7 +2,8 @@
|
|
2
2
|
jf.setContentPane(getJContentPane());
|
3
3
|
jf.setContentPane(getJContentPane2());
|
4
4
|
```
|
5
|
-
コンテントペイン
|
5
|
+
コンテントペインとして設定できるのは一つだけです。 (set~ というメソッドは大抵は一つだけです。)
|
6
|
+
|
6
7
|
作るべき画面のようにするにはどのような構造にすれば良いのかを考えることも課題の一つなのでは無いでしょうか。
|
7
8
|
|
8
9
|
JPanel に add 出来るのは Component です。 JButton も JLabel も JPanel も Component です。
|
3
修正
test
CHANGED
@@ -4,4 +4,6 @@
|
|
4
4
|
```
|
5
5
|
コンテントペインに設定できるのは一つだけです。
|
6
6
|
作るべき画面のようにするにはどのような構造にすれば良いのかを考えることも課題の一つなのでは無いでしょうか。
|
7
|
+
|
8
|
+
JPanel に add 出来るのは Component です。 JButton も JLabel も JPanel も Component です。
|
7
|
-
ボタンのパネル
|
9
|
+
ボタンのパネルに『ボタンを横に4つ並べられている』のですから、パネルに『ラベルとパネルを縦に2つ並べる』ことも出来るはずです。
|
2
追加
test
CHANGED
@@ -3,4 +3,5 @@
|
|
3
3
|
jf.setContentPane(getJContentPane2());
|
4
4
|
```
|
5
5
|
コンテントペインに設定できるのは一つだけです。
|
6
|
-
|
6
|
+
作るべき画面のようにするにはどのような構造にすれば良いのかを考えることも課題の一つなのでは無いでしょうか。
|
7
|
+
ボタンのパネルは『ボタンを横に4つ並べられている』のですから、パネルに『パネルを縦に2つ並べる』ことも出来るはずです。
|
1
修正
test
CHANGED
@@ -3,4 +3,4 @@
|
|
3
3
|
jf.setContentPane(getJContentPane2());
|
4
4
|
```
|
5
5
|
コンテントペインに設定できるのは一つだけです。
|
6
|
-
二つ設定するにはどうすれば良いのかを考えることも課題の一つなのでは無いでしょうか。
|
6
|
+
二つ設定するにはどのような構造にすれば良いのかを考えることも課題の一つなのでは無いでしょうか。
|