回答編集履歴

1

見直しキャンペーン中

2023/07/18 21:30

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,127 +1,64 @@
1
- まずTimerTaskでnew Fishing()はよくないですね。
1
+ まず`TimerTask``new Fishing()`はよくないですね。
2
+ staticなものがあるので結果的には動いていますが、無駄に`JFrame`を作っています。
3
+ 内部クラスにすれば`Fishing`のメンバが見えるので中に入れました。
2
4
 
3
- staticなものがあるので結果的には動いていますが、無駄にJFrameを作っています。
4
-
5
- 内部クラスにすればFishingのメンバが見えるので中に入れました。
6
-
7
-
8
-
9
- サイズで表示非表示を表現するなら、ボタンは全部Addされていないといけません。
5
+ サイズで表示非表示を表現するなら、ボタンは全部`Add`されていないといけません。
10
-
11
6
  ```Java
12
-
13
7
  public class Fishing extends JFrame {
14
-
15
8
  private static final long serialVersionUID = 1L;
16
9
 
17
-
18
-
19
10
  private Random random = new Random();
20
-
21
11
  private JButton[] Bbt = {
22
-
23
12
  new JButton("しーん"),
24
-
25
13
  new JButton("!"),
26
-
27
14
  new JButton("!!"),
28
-
29
15
  new JButton("!!!"),
30
-
31
16
  new JButton("?"),
32
-
33
17
  };
34
-
35
18
  private JButton CHANGEBt = Bbt[0];
36
-
37
19
  private JPanel p4 = new JPanel();
38
20
 
39
-
40
-
41
21
  public void ROLL() {
42
-
43
22
  int x = Atrandom();
44
-
45
23
  CHANGEBt.setBounds(0, 0, 0, 0);
46
-
47
24
  CHANGEBt = Bbt[x];
48
-
49
25
  CHANGEBt.setBounds(378, 200, 375, 280);
50
-
51
26
  }
52
-
53
-
54
27
 
55
28
  private static final int[] Ra = { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 };
56
29
 
57
-
58
-
59
30
  public int Atrandom() {
60
-
61
31
  return Ra[random.nextInt(Ra.length)];
62
-
63
32
  }
64
33
 
65
-
66
-
67
34
  class TASK extends TimerTask {
68
-
69
35
  public void run() {
70
-
71
36
  SwingUtilities.invokeLater(() -> ROLL());
72
-
73
37
  }
74
-
75
38
  }
76
39
 
77
-
78
-
79
40
  public void MAINGAME() {
80
-
81
41
  setBounds(550, 100, 770, 800);
82
-
83
42
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
84
-
85
43
  p4.setLayout(null);
86
-
87
44
  p4.add(Bbt[0]);
88
-
89
45
  p4.add(Bbt[1]);
90
-
91
46
  p4.add(Bbt[2]);
92
-
93
47
  p4.add(Bbt[3]);
94
-
95
48
  p4.add(Bbt[4]);
96
49
 
97
-
98
-
99
50
  CHANGEBt.setBounds(378, 200, 375, 280);
100
-
101
51
  add(p4);
102
-
103
52
  setVisible(true);
104
53
 
105
-
106
-
107
54
  int R = 100 * (random.nextInt(50) + 1);
108
-
109
55
  Timer timer = new Timer(true);
110
-
111
56
  timer.schedule(new TASK(), R);
112
-
113
57
  }
114
58
 
115
-
116
-
117
59
  public static void main(String[] args) {
118
-
119
60
  Fishing set = new Fishing();
120
-
121
61
  set.MAINGAME();
122
-
123
62
  }
124
-
125
63
  }
126
-
127
64
  ```