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

質問編集履歴

5

書式の改善を行いました。

2019/12/14 09:56

投稿

rutus
rutus

スコア7

title CHANGED
File without changes
body CHANGED
@@ -2,14 +2,18 @@
2
2
  Eclipseでゲームを作っております。
3
3
  標準出力でコンソールに出てくる内容をGUIに反映させたい。
4
4
  ### 発生している問題
5
- ネットで調べても知識不足とSwingきることがわかっておらず
5
+ ネットで調べ「コンソール内容をtextAreaに出力できるやり方」など
6
+ 調べましたが実装がうまく行かずお手上げ状態です。
7
+ 全く解決の糸口が見えず納期に間に合わせることが
6
- 明確に調べる方法と理解ない
8
+ できず焦り募ってす。
7
9
 
8
10
 
9
11
  ### 該当のソースコード
10
12
  ```ここに言語を入力
13
+
11
- package aaa;
14
+ package game;
12
15
  import java.awt.BorderLayout;
16
+ import java.awt.Font;
13
17
  import java.awt.event.ActionEvent;
14
18
  import java.awt.event.ActionListener;
15
19
 
@@ -18,19 +22,22 @@
18
22
  import javax.swing.JFrame;
19
23
  import javax.swing.JLabel;
20
24
  import javax.swing.JPanel;
25
+ import javax.swing.JTextArea;
21
26
 
22
- public class GameScreen extends JFrame implements ActionListener {
27
+ public class GameScreenE extends JFrame implements ActionListener {
23
28
 
24
29
  int answer[]= new int[3];
25
30
 
26
31
  int count = 0, hit =0 ,blow =0;
27
32
 
33
+
34
+
28
35
  JComboBox<Integer> btn1 = new JComboBox<Integer>();
29
36
  JComboBox<Integer> btn2 = new JComboBox<Integer>();
30
37
  JComboBox<Integer> btn3 = new JComboBox<Integer>();
38
+
39
+      public void init() {
31
40
 
32
- public void init() {
33
-
34
41
  this.setTitle("game");
35
42
 
36
43
  //フレームの設定(タイトル・サイズ・位置
@@ -39,120 +46,137 @@
39
46
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
40
47
 
41
48
 
42
- JPanel ps = new JPanel();
49
+ JPanel ps = new JPanel();
43
- JPanel pn = new JPanel();
50
+ JPanel pn = new JPanel();
51
+ JPanel pc = new JPanel();
44
52
 
45
53
 
46
- JLabel label1 = new JLabel("?");
54
+ JLabel label1 = new JLabel("?");
47
- JLabel label2 =new JLabel("?");
55
+ JLabel label2 =new JLabel("?");
48
- JLabel label3 =new JLabel("?");
56
+ JLabel label3 =new JLabel("?");
49
- JLabel label4 = new JLabel("");
57
+ label1.setFont(new Font("MS ゴシック", Font.BOLD, 50));
50
- label4.setText(String.valueOf(hit));
51
- label4.setText(String.valueOf(blow));
58
+ label2.setFont(new Font("MS ゴシック", Font.BOLD, 50));
59
+ label3.setFont(new Font("MS ゴシック", Font.BOLD, 50));
52
60
 
61
+ JLabel label4 = new JLabel("<html>※○は場所も数字も合ってます。"
62
+ + "<br>△は数字だけ合ってます。(" + count + ")");
63
+ label4.setFont(new Font("MS ゴシック", Font.BOLD, 10));
53
64
 
65
+
66
+ pn.add(label1);
67
+ pn.add(label2);
68
+ pn.add(label3);
69
+ this.getContentPane().add(pn,BorderLayout.NORTH);
70
+ pc.add(label4);
71
+ this.getContentPane().add(pc,BorderLayout.CENTER);
54
72
 
55
-
56
73
 
74
+ for(int i=1; i<=6; i++) {
57
- pn.add(label1);
75
+ btn1.addItem(i);
58
- this.getContentPane().add(pn,BorderLayout.NORTH);
59
- pn.add(label2);
76
+ btn2.addItem(i);
60
- this.getContentPane().add(pn,BorderLayout.NORTH);
61
- pn.add(label3);
77
+ btn3.addItem(i);
62
- this.getContentPane().add(pn,BorderLayout.NORTH);
63
- ps.add(label4);
78
+ }
64
- this.getContentPane().add(pn,BorderLayout.SOUTH);
65
79
 
66
-
67
- for(int i=1; i<=6; i++) {
80
+ JButton btn4 = new JButton("チェック");
68
- btn1.addItem(i);
81
+ btn4.addActionListener(this);
69
- btn2.addItem(i);
70
- btn3.addItem(i);
71
- }
72
82
 
73
- JButton btn4 = new JButton("チェック");
74
- btn4.addActionListener(this);
83
+ ps.add(btn1);
84
+ ps.add(btn2);
85
+ ps.add(btn3);
86
+ ps.add(btn4);
75
87
 
88
+ this.getContentPane().add(ps,BorderLayout.SOUTH);
76
- ps.add(btn1);
89
+ this.setVisible(true);
77
- ps.add(btn2);
78
- ps.add(btn3);
79
- ps.add(btn4);
80
90
 
91
+ JPanel p = new JPanel();
92
+ JTextArea area1 = new JTextArea();
93
+ p.add(area1);
81
- this.getContentPane().add(ps,BorderLayout.SOUTH);
94
+ this. getContentPane().add(p, BorderLayout.CENTER);
82
95
 
83
- this.setVisible(true);
84
96
 
97
+
98
+
99
+
100
+
101
+
85
- for (int i = 0; i < answer.length; i++) {
102
+ for (int i = 0; i < answer.length; i++) {
86
- boolean sw = false;
103
+ boolean sw = false;
87
- answer[i] = (int) (Math.random() * 6 + 1);
104
+ answer[i] = (int) (Math.random() * 6 + 1);
88
- do { sw = false;
105
+ do { sw = false;
89
- for (int j = i - 1; j >= 0; j--) {
106
+ for (int j = i - 1; j >= 0; j--) {
90
- if (answer[i] == answer[j]) {
107
+ if (answer[i] == answer[j]) {
91
- sw= true;
108
+ sw= true;
92
- answer[i] = (int) (Math.random() * 6 + 1);
109
+ answer[i] = (int) (Math.random() * 6 + 1);
93
- }
110
+ }
94
- }
95
- } while (sw == true);
96
- }
97
111
  }
112
+ } while (sw == true);
113
+ }
114
+ }
98
115
 
116
+
99
117
  @Override
100
118
  public void actionPerformed(ActionEvent e) {
101
119
  // TODO 自動生成されたメソッド・スタブ
102
120
 
121
+
103
122
  JButton btn = (JButton)e.getSource();
104
123
  String txt = btn.getText();
105
124
  if (txt.equals("チェック")) {
106
-
107
-
108
-
109
-
110
125
 
111
126
  //入力された値を取り出す
112
127
  Integer data1 = (Integer)btn1.getSelectedItem();
113
128
  Integer data2 = (Integer)btn2.getSelectedItem();
114
129
  Integer data3 = (Integer)btn3.getSelectedItem();
130
+ //答えの数字3つと、入力された数字3つを答え合わせする。
115
131
 
132
+ //配列設定
133
+ int datas [] = new int[3];
134
+ datas[0] = data1;
135
+ datas[1] = data2;
136
+ datas[2] = data3;
116
137
 
138
+ hit = 0;
139
+ blow = 0;
117
140
 
141
+ for (int i = 0; i < answer.length; i++) {
142
+ for (int j = 0; j < answer.length; j++) {
143
+ if (i == j && datas[i] == answer[j]) {
144
+ hit++;
145
+ }
146
+ else if (datas[i] == answer[j]) {
147
+ blow++;
148
+ }
149
+ }
150
+ }
151
+ }
152
+ count++;
153
+
154
+ System.out.println(count+"回目 \n○ " + hit + " △" + blow);
155
+
156
+
118
157
 
119
- //答えの数字3つと、入力さた数字3つを答合わせする。
158
+ if (hit == 3) {//分岐点。 終わば画面4 超れば画面5
159
+ ClearScreen cls = new ClearScreen();
160
+ cls.init();//初期化。
161
+ }
120
162
 
163
+ if(count == 15) {//Easy
164
+ OverScreen ovs =new OverScreen();
165
+ ovs.init();
166
+
167
+
168
+ }
169
+ }
170
+
121
171
 
122
- //配列設定
123
- int datas [] = new int[3];
124
- datas[0] = data1;
125
- datas[1] = data2;
126
- datas[2] = data3;
127
-
128
- hit = 0;
129
- blow = 0;
130
-
131
-
132
- for (int i = 0; i < answer.length; i++) {
133
- for (int j = 0; j < answer.length; j++) {
134
- if (i == j && datas[i] == answer[j]) {
135
- hit++;
136
- } else if (datas[i] == answer[j]) {
137
- blow++;
138
- }
172
+ }
139
- }
140
- }
141
- System.out.println("ヒット" + hit + " ブロー" + blow+"回数"+count);
142
- //ここをGUIに反映させたい
143
-
144
- if (hit == 3) {//分岐点。 終われば画面4 超えれば画面5
145
- ClearScreen cls = new ClearScreen();
146
- cls.init();//初期化。
147
- }else {
148
-
149
- }
150
- }
151
- }
152
- }
153
173
  ```
154
174
 
155
175
 
156
176
 
157
177
  ### 補足情報(FW/ツールのバージョンなど)
158
- Eclipse2019-09を使用しています。
178
+ Eclipse2019-09を使用しています。
179
+
180
+ 先日にも掲示したのですが頼れるのはここの方々だけです。
181
+ よろしくお願い致します。
182
+ 当方も一生懸命理解に努めます。

4

書式の改善を行いました。 何度もすいません。

2019/12/14 09:56

投稿

rutus
rutus

スコア7

title CHANGED
File without changes
body CHANGED
@@ -149,7 +149,8 @@
149
149
  }
150
150
  }
151
151
  }
152
+ }
152
- }``
153
+ ```
153
154
 
154
155
 
155
156
 

3

書式の改善を行いました。どうでしょうか?

2019/12/10 04:52

投稿

rutus
rutus

スコア7

title CHANGED
File without changes
body CHANGED
@@ -7,12 +7,8 @@
7
7
 
8
8
 
9
9
  ### 該当のソースコード
10
-
11
- ```
10
+ ```ここに言語を入力
12
-
13
-
14
- ```
15
- コードpackage aaa;
11
+ package aaa;
16
12
  import java.awt.BorderLayout;
17
13
  import java.awt.event.ActionEvent;
18
14
  import java.awt.event.ActionListener;
@@ -154,7 +150,6 @@
154
150
  }
155
151
  }
156
152
  }``
157
- ``
158
153
 
159
154
 
160
155
 

2

当方初心者ですので初心者アイコンをつけさせてもらいました

2019/12/10 04:49

投稿

rutus
rutus

スコア7

title CHANGED
File without changes
body CHANGED
@@ -159,6 +159,4 @@
159
159
 
160
160
 
161
161
  ### 補足情報(FW/ツールのバージョンなど)
162
- Eclipse2019-09を使用しています。
162
+ Eclipse2019-09を使用しています。
163
-
164
- ソースをそのままコピペしたら見にくい内容になってすいません。

1

書式の改善を行いました。

2019/12/10 04:40

投稿

rutus
rutus

スコア7

title CHANGED
File without changes
body CHANGED
@@ -11,7 +11,8 @@
11
11
  ```
12
12
 
13
13
 
14
+ ```
14
- package aaa;
15
+ コードpackage aaa;
15
16
  import java.awt.BorderLayout;
16
17
  import java.awt.event.ActionEvent;
17
18
  import java.awt.event.ActionListener;
@@ -152,7 +153,7 @@
152
153
  }
153
154
  }
154
155
  }
155
- }
156
+ }``
156
157
  ``
157
158
 
158
159