回答編集履歴
5
import java.awt.Rectangleを削除
test
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
ボタンをクリックしたらランダムな位置に移動するサンプルコードです。
|
2
2
|
|
3
3
|
```Java
|
4
|
-
|
5
|
-
import java.awt.Rectangle;
|
6
4
|
|
7
5
|
import java.util.Random;
|
8
6
|
|
4
moveToメソッドを追加
test
CHANGED
@@ -26,9 +26,9 @@
|
|
26
26
|
|
27
27
|
}
|
28
28
|
|
29
|
-
private JButton bt;
|
29
|
+
private final JButton bt = new JButton("●");
|
30
30
|
|
31
|
-
private Random rnd = new java.util.Random();
|
31
|
+
private final Random rnd = new java.util.Random();
|
32
32
|
|
33
33
|
public Q107092() {
|
34
34
|
|
@@ -38,23 +38,11 @@
|
|
38
38
|
|
39
39
|
setSize(700, 600);
|
40
40
|
|
41
|
-
setLayout(null);
|
41
|
+
setLayout(null);
|
42
|
-
|
43
|
-
int xran = rnd.nextInt(650);
|
44
|
-
|
45
|
-
int yran = rnd.nextInt(550);
|
46
|
-
|
47
|
-
bt = new JButton("●");
|
48
42
|
|
49
43
|
bt.addActionListener((e) -> {
|
50
44
|
|
51
|
-
Rectangle rect = bt.getBounds();
|
52
|
-
|
53
|
-
rect.x = rnd.nextInt(650);
|
54
|
-
|
55
|
-
rect.y = rnd.nextInt(550);
|
56
|
-
|
57
|
-
|
45
|
+
moveTo();
|
58
46
|
|
59
47
|
//コンポーネントの再配置のみならrepaintよりrevalidate
|
60
48
|
|
@@ -62,17 +50,25 @@
|
|
62
50
|
|
63
51
|
});
|
64
52
|
|
65
|
-
// ボタンのサイズを5➾50に変更
|
66
|
-
|
67
|
-
|
53
|
+
moveTo();
|
68
54
|
|
69
55
|
add(bt);
|
70
56
|
|
71
57
|
}
|
72
58
|
|
59
|
+
private void moveTo(){
|
60
|
+
|
61
|
+
int x = rnd.nextInt(650);
|
62
|
+
|
63
|
+
int y = rnd.nextInt(550);
|
64
|
+
|
65
|
+
// ボタンのサイズを5➾50に変更
|
66
|
+
|
67
|
+
bt.setBounds(x, y, 50, 50);
|
68
|
+
|
69
|
+
}
|
70
|
+
|
73
71
|
}
|
74
|
-
|
75
|
-
|
76
72
|
|
77
73
|
```
|
78
74
|
|
3
コメントを追加
test
CHANGED
@@ -56,6 +56,8 @@
|
|
56
56
|
|
57
57
|
bt.setBounds(rect);
|
58
58
|
|
59
|
+
//コンポーネントの再配置のみならrepaintよりrevalidate
|
60
|
+
|
59
61
|
revalidate();
|
60
62
|
|
61
63
|
});
|
2
参考情報にrevalidateを追記
test
CHANGED
@@ -79,3 +79,5 @@
|
|
79
79
|
□参考情報
|
80
80
|
|
81
81
|
[java.util.Random#nextInt](https://docs.oracle.com/javase/jp/8/docs/api/java/util/Random.html#nextInt-int-)
|
82
|
+
|
83
|
+
[JComponent#revalidate](https://docs.oracle.com/javase/jp/6/api/javax/swing/JComponent.html#revalidate())
|
1
参考情報にjava.util.Random#nextIntを追加
test
CHANGED
@@ -73,3 +73,9 @@
|
|
73
73
|
|
74
74
|
|
75
75
|
```
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
□参考情報
|
80
|
+
|
81
|
+
[java.util.Random#nextInt](https://docs.oracle.com/javase/jp/8/docs/api/java/util/Random.html#nextInt-int-)
|