質問するログイン新規登録

質問編集履歴

3

タイトルを変更しました。

2020/08/13 07:12

投稿

JavaTakashi
JavaTakashi

スコア2

title CHANGED
@@ -1,1 +1,1 @@
1
- Swingにおけるコンポネント描画イベント処理の方法について
1
+ Swingにおけるコンポネント描画イベント処理の方法で行き詰まり
body CHANGED
File without changes

2

ソースコードをより見やすくしました

2020/08/13 07:12

投稿

JavaTakashi
JavaTakashi

スコア2

title CHANGED
File without changes
body CHANGED
@@ -79,8 +79,7 @@
79
79
  frame.setLayout(null);
80
80
  frame.setVisible(true);
81
81
  contentPane = frame.getContentPane();
82
-
83
- ////////////////////////////////////////// ゲーム開始直後のタイトル画面。スタートボタンを押すと「マイホーム」画面遷移
82
+ //// ゲーム開始直後のタイトル画面。スタートボタンを押すと「マイホーム」画面遷移
84
83
  titleNamePanel = new JPanel();
85
84
  titleNamePanel.setBounds(100,300,1000,200);
86
85
  titleNamePanel.setBackground(Color.black);
@@ -98,14 +97,14 @@
98
97
  startButton.setForeground(Color.white);
99
98
  startButton.setBorder(null); //ボタンの枠線を消す
100
99
  startButton.setFont(normalFont);
101
- startButton.addActionListener(startButtonHandler); //スタートボタンを押すと、画面が「マイホーム」に切り替わる
100
+ startButton.addActionListener(startButtonHandler);
102
101
  startButton.setFocusPainted(false);
103
102
  startButtonPanel.add(startButton);
104
103
 
105
104
  contentPane.add(titleNamePanel);
106
105
  contentPane.add(startButtonPanel);
107
106
  }
108
- //////////////////////////////////////////////////////////////////////////////////// ここからはマイホーム
107
+ ////////////////////////////////////////////// ここからはマイホーム
109
108
 
110
109
  public void myHome(){
111
110
  position = "myHome";
@@ -150,8 +149,8 @@
150
149
  choiceButtonPanel.add(choice1);
151
150
 
152
151
  choice2 = new JButton("何か食べる");
153
- choice2.setBackground(Color.black);//ボタンの色は背景と同じ黒
152
+ choice2.setBackground(Color.black);
154
- choice2.setForeground(Color.white);//文字の色は白
153
+ choice2.setForeground(Color.white);
155
154
  choice2.setFont(normalFont);
156
155
  choice2.setFocusPainted(false);
157
156
  choice2.addActionListener(choiceHandler);
@@ -159,8 +158,8 @@
159
158
  choiceButtonPanel.add(choice2);
160
159
 
161
160
  choice3 = new JButton("買い物に行く");
162
- choice3.setBackground(Color.black);//ボタンの色は背景と同じ黒
161
+ choice3.setBackground(Color.black);
163
- choice3.setForeground(Color.white);//文字の色は白
162
+ choice3.setForeground(Color.white);
164
163
  choice3.setFont(normalFont);
165
164
  choice3.setFocusPainted(false);
166
165
  choice3.addActionListener(choiceHandler);
@@ -168,7 +167,7 @@
168
167
  choiceButtonPanel.add(choice3);
169
168
  }
170
169
 
171
- /////////////////////////////////////////////////////////////////////////////////// 本を読む
170
+ /////////////////////////////////////////////////////// 本を読む
172
171
  public void book(){
173
172
  position = "book";
174
173
 
@@ -179,7 +178,7 @@
179
178
  choice3.setText("やめる");
180
179
  }
181
180
 
182
- /////////////////////////////////////////////////////////////////////////////////// 何か食べる
181
+ /////////////////////////////////////////////////////// 何か食べる
183
182
  public void eat(){
184
183
  position = "eat";
185
184
 
@@ -190,7 +189,7 @@
190
189
  choice3.setText("ああ、おなかいっぱい");
191
190
  }
192
191
 
193
- /////////////////////////////////////////////////////////////////////////////////// 買い物に行く
192
+ ///////////////////////////////////////////////////// 買い物に行く
194
193
  public void shop(){
195
194
  position = "shop";
196
195
 
@@ -203,14 +202,14 @@
203
202
  choice3.setText("店を出る");
204
203
  }
205
204
 
206
- /////////////////////////////////////////////////////////////////////////////////// スタートボタンを押した時の動作
205
+ /////////////////////////////////////// スタートボタンを押した時の動作
207
206
  public class StartButtonHandler implements ActionListener{
208
207
  public void actionPerformed(ActionEvent event){
209
208
  myHome();
210
209
  }
211
210
  }
212
211
 
213
- /////////////////////////////////////////////////////////////////////////////////// 選択肢のボタンを押した時の動作
212
+ /////////////////////////////////////// 選択肢のボタンを押した時の動作
214
213
  public class ChoiceHandler implements ActionListener{
215
214
  public void actionPerformed(ActionEvent event){
216
215
 

1

ソースコードの書き方が間違っていました。

2020/08/13 07:07

投稿

JavaTakashi
JavaTakashi

スコア2

title CHANGED
File without changes
body CHANGED
@@ -12,7 +12,7 @@
12
12
 
13
13
  ### 該当のソースコード
14
14
 
15
-
15
+ ```ここに言語を入力
16
16
  import java.awt.Color;
17
17
  import java.awt.Container;
18
18
  import java.awt.Font;
@@ -250,10 +250,9 @@
250
250
 
251
251
  }
252
252
 
253
+ ```
253
254
 
254
255
 
255
-
256
-
257
256
  ### 試したこと
258
257
 
259
258
  ネットにて症状を検索。どうやら複数の別個のスレッドから描写の変更を命令したことによる競合が原因であると推測し、SwingUtilities、SwingWorker、EDTなど調べてみたのですが、それらを当該コードにどう適用すればいいのか分からず独学の限界を感じております。