質問編集履歴

2

タイトル変更

2016/01/03 15:59

投稿

ryo-flat
ryo-flat

スコア21

test CHANGED
@@ -1 +1 @@
1
- javaのJPanelの使い方について
1
+ javaのJPanelの上での描画について
test CHANGED
@@ -281,3 +281,137 @@
281
281
 
282
282
 
283
283
  ```
284
+
285
+
286
+
287
+ 以下が少し前に作った別のJPanelを作るクラスです。このような感じでコンストラクタを呼び出せば表示されるようにしたいと考えています。
288
+
289
+ ```java
290
+
291
+ package gui;
292
+
293
+
294
+
295
+ import java.awt.Dimension;
296
+
297
+ import java.awt.event.ActionEvent;
298
+
299
+ import java.awt.event.ActionListener;
300
+
301
+
302
+
303
+ import javax.swing.JButton;
304
+
305
+ import javax.swing.JPanel;
306
+
307
+
308
+
309
+ public class JobSelect extends JPanel implements ActionListener {
310
+
311
+
312
+
313
+ public int job;
314
+
315
+
316
+
317
+ JButton fa;
318
+
319
+ JButton ma;
320
+
321
+ JButton se;
322
+
323
+
324
+
325
+
326
+
327
+ public JobSelect() {
328
+
329
+ fa = new JButton("ファイター");
330
+
331
+ fa.setPreferredSize(new Dimension(100, 100));
332
+
333
+ fa.addActionListener(this);
334
+
335
+ ma = new JButton("マジシャン");
336
+
337
+ ma.setPreferredSize(new Dimension(100, 100));
338
+
339
+ ma.addActionListener(this);
340
+
341
+ se = new JButton("シールドセージ");
342
+
343
+ se.setPreferredSize(new Dimension(100, 100));
344
+
345
+ se.addActionListener(this);
346
+
347
+
348
+
349
+ add(fa);
350
+
351
+ add(ma);
352
+
353
+ add(se);
354
+
355
+ }
356
+
357
+
358
+
359
+ public int getJob() {
360
+
361
+ return job;
362
+
363
+ }
364
+
365
+
366
+
367
+ public void setJob(int job) {
368
+
369
+ this.job = job;
370
+
371
+ }
372
+
373
+
374
+
375
+ public void actionPerformed(ActionEvent e) {
376
+
377
+ if(e.getSource() == fa){
378
+
379
+ job = 0;
380
+
381
+ System.out.println(job);
382
+
383
+ }
384
+
385
+ if(e.getSource() == ma){
386
+
387
+ job = 1;
388
+
389
+ System.out.println(job);
390
+
391
+ }
392
+
393
+ if(e.getSource() == se){
394
+
395
+ job = 2;
396
+
397
+ System.out.println(job);
398
+
399
+ }
400
+
401
+ }
402
+
403
+
404
+
405
+ public static void main(String[] args){
406
+
407
+ new JobSelect();
408
+
409
+ }
410
+
411
+
412
+
413
+ }
414
+
415
+
416
+
417
+ ```

1

MainScreenクラスのコード追加

2016/01/03 15:59

投稿

ryo-flat
ryo-flat

スコア21

test CHANGED
File without changes
test CHANGED
@@ -1 +1,283 @@
1
- javaでターン制のゲームを作っています。現在作っているBattleScreenクラスはバトル画面をJPanelに描画するクラスであり、MainScreenクラスのJFrameに追加できることを想定しています。しかし、単純に名前を表示したりすることはできるのですがそれを四角の枠で囲ったり、その枠の下に画像を表示を表示させる方法、及びきれいなレイアウトで描画する方法が分かりません。
1
+ javaでターン制のゲームを作っています。現在作っているBattleScreenクラスはバトル画面をJPanelに描画するクラスであり、MainScreenクラスのJFrameに追加できることを想定しています。しかし、単純に名前を表示したりすることはできるのですがそれを四角の枠で囲ったり、その枠の下に画像を表示を表示させる方法、及びきれいなレイアウトで描画する方法が分かりません。下記がMainScreenクラスで、このJFrameにBorderLayoutのCENTERでaddしたいと考えています。
2
+
3
+ ```java
4
+
5
+ package gui;
6
+
7
+
8
+
9
+ import java.awt.BorderLayout;
10
+
11
+ import java.awt.Container;
12
+
13
+ import java.awt.Dimension;
14
+
15
+ import java.awt.Font;
16
+
17
+ import java.awt.event.ActionEvent;
18
+
19
+ import java.awt.event.ActionListener;
20
+
21
+
22
+
23
+ import javax.swing.ButtonGroup;
24
+
25
+ import javax.swing.JButton;
26
+
27
+ import javax.swing.JFrame;
28
+
29
+ import javax.swing.JLabel;
30
+
31
+ import javax.swing.JPanel;
32
+
33
+ import javax.swing.JRadioButton;
34
+
35
+ import javax.swing.JTextField;
36
+
37
+
38
+
39
+ import state.BattleText;
40
+
41
+
42
+
43
+
44
+
45
+ public class MainScreen extends JFrame implements ActionListener {
46
+
47
+
48
+
49
+ JFrame f;
50
+
51
+ JPanel p1;
52
+
53
+ JPanel p2;
54
+
55
+ JPanel p3;
56
+
57
+ JPanel p4;
58
+
59
+ JPanel p5;
60
+
61
+ JPanel p6;
62
+
63
+ JPanel battleScreen;
64
+
65
+ JTextField ip;
66
+
67
+ JTextField na;
68
+
69
+ JButton connect;
70
+
71
+ JButton kettei1;
72
+
73
+ JButton kettei2;
74
+
75
+ JButton atk;
76
+
77
+ JButton def;
78
+
79
+ JButton sp;
80
+
81
+ JRadioButton fi;
82
+
83
+ JRadioButton ma;
84
+
85
+ JRadioButton se;
86
+
87
+ ButtonGroup group;
88
+
89
+ JLabel commandInfo;
90
+
91
+ JLabel count;
92
+
93
+ String address;
94
+
95
+ String eff="a";
96
+
97
+ String name;
98
+
99
+ int job = 0;
100
+
101
+
102
+
103
+ public MainScreen() {
104
+
105
+ f = new JFrame("DENDAI QUEST");
106
+
107
+ f.setVisible(true);
108
+
109
+ f.setSize(1100, 700);
110
+
111
+ setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
112
+
113
+
114
+
115
+ p1 = new JPanel();
116
+
117
+ p2 = new JPanel();
118
+
119
+ p3 = new JPanel();
120
+
121
+ p4 = new JPanel();
122
+
123
+ p5 = new JPanel();
124
+
125
+ p6 = new JPanel();
126
+
127
+ battleScreen = new JPanel();
128
+
129
+
130
+
131
+ commandInfo = new JLabel();
132
+
133
+ count = new JLabel();
134
+
135
+
136
+
137
+
138
+
139
+
140
+
141
+
142
+
143
+ ip = new JTextField("IP", 20);
144
+
145
+ na = new JTextField("名前", 20);
146
+
147
+
148
+
149
+ fi = new JRadioButton("ファイター", true);
150
+
151
+ ma = new JRadioButton("マジシャン");
152
+
153
+ se = new JRadioButton("シールドセージ");
154
+
155
+
156
+
157
+ group = new ButtonGroup();
158
+
159
+
160
+
161
+ group.add(fi);
162
+
163
+ group.add(ma);
164
+
165
+ group.add(se);
166
+
167
+
168
+
169
+ connect = new JButton("接続");
170
+
171
+ connect.addActionListener(this);
172
+
173
+ kettei1 = new JButton("決定");
174
+
175
+ kettei1.addActionListener(this);
176
+
177
+ kettei2 = new JButton("決定");
178
+
179
+ kettei2.addActionListener(this);
180
+
181
+
182
+
183
+ commandInfo.setText("<html>コマンド情報<br>攻撃:通常攻撃<br>防御:防御"
184
+
185
+ + "<br>特殊<br>ファイター:大攻撃<br>マジシャン:回復" + "<br>シールドセージ:跳ね返し</html>");
186
+
187
+ commandInfo.setBounds(0, 50, 0, 0);
188
+
189
+
190
+
191
+ atk = new JButton("攻撃");
192
+
193
+ atk.setPreferredSize(new Dimension(200, 100));
194
+
195
+ atk.addActionListener(this);
196
+
197
+ def = new JButton("防御");
198
+
199
+ def.setPreferredSize(new Dimension(200, 100));
200
+
201
+ def.addActionListener(this);
202
+
203
+ sp = new JButton("特殊");
204
+
205
+ sp.setPreferredSize(new Dimension(200, 100));
206
+
207
+ sp.addActionListener(this);
208
+
209
+
210
+
211
+ p1.add(commandInfo);
212
+
213
+
214
+
215
+ p2.add(atk);
216
+
217
+ p2.add(def);
218
+
219
+ p2.add(sp);
220
+
221
+
222
+
223
+
224
+
225
+ p3.add(ip);
226
+
227
+ p3.add(connect);
228
+
229
+ p3.add(na);
230
+
231
+ p3.add(kettei1);
232
+
233
+ p3.add(fi);
234
+
235
+ p3.add(ma);
236
+
237
+ p3.add(se);
238
+
239
+ p3.add(kettei2);
240
+
241
+
242
+
243
+ p5.add(battleScreen, BorderLayout.CENTER);
244
+
245
+
246
+
247
+
248
+
249
+ Container contentPane = f.getContentPane();
250
+
251
+ contentPane.add(p1, BorderLayout.EAST);
252
+
253
+
254
+
255
+ contentPane.add(p3, BorderLayout.NORTH);
256
+
257
+
258
+
259
+ //contentPane.add(p5, BorderLayout.CENTER);
260
+
261
+
262
+
263
+
264
+
265
+ //contentPane.add(jobSelectPanel, BorderLayout.CENTER);
266
+
267
+
268
+
269
+ contentPane.add(p2, BorderLayout.SOUTH);
270
+
271
+ }
272
+
273
+ public static void main(String[] args) {
274
+
275
+ new MainScreen();
276
+
277
+ }
278
+
279
+ }
280
+
281
+
282
+
283
+ ```