回答編集履歴

1

返信が想像どおりだったので模範回答を追記

2015/12/19 18:08

投稿

ipadcaron
ipadcaron

スコア1693

test CHANGED
@@ -1,3 +1,627 @@
1
1
  レイアウトマネージャーの問題かな。
2
2
 
3
3
  gridbaglayout とか使えばいいんじゃないでしょか。
4
+
5
+
6
+
7
+ ![イメージ説明](05770cbfd8bc20dc5134a45d439f4abf.png)
8
+
9
+
10
+
11
+ ![イメージ説明](7de95ce16359ffdbb210137877064bfb.png)
12
+
13
+
14
+
15
+ 模範回答?作ったのでコピペで実行確認してみてください。
16
+
17
+
18
+
19
+ 変な書き方でかいてありますが、ちゃんとあなたの希望通りです。
20
+
21
+
22
+
23
+ 動作環境:Mac, Eclipse Luna Java1.8 swing
24
+
25
+
26
+
27
+ Debtaku.java で保存
28
+
29
+ ```java
30
+
31
+ package jp.kanagawa.caron.swing.tools;
32
+
33
+
34
+
35
+ import java.awt.BorderLayout;
36
+
37
+ import java.awt.event.ActionEvent;
38
+
39
+ import java.awt.event.ActionListener;
40
+
41
+ import java.util.HashMap;
42
+
43
+ import java.util.Map;
44
+
45
+
46
+
47
+ import javax.script.ScriptEngine;
48
+
49
+ import javax.script.ScriptEngineManager;
50
+
51
+ import javax.swing.JButton;
52
+
53
+ import javax.swing.JFrame;
54
+
55
+ import javax.swing.JPanel;
56
+
57
+
58
+
59
+ /**
60
+
61
+ * swing で電卓、普通の四則演算ができる電卓を作ります。
62
+
63
+ * MC,C, ,
64
+
65
+ * (,),+,-
66
+
67
+ * 1,2,3,/
68
+
69
+ * 4,5,6,*
70
+
71
+ * 7,8,9,%
72
+
73
+ * =,0,$,_
74
+
75
+ * ( 左かっこ
76
+
77
+ * ) 右かっこ
78
+
79
+ * MC = Memory Clear
80
+
81
+ * C = Clear
82
+
83
+ * 0-9 Number
84
+
85
+ * + prefix / add
86
+
87
+ * - prefix / sub
88
+
89
+ * "/" div
90
+
91
+ * "%" mod
92
+
93
+ * "*" mul
94
+
95
+ * "$" register $0-$7 まで。
96
+
97
+ *
98
+
99
+ * 計算部分は、ScriptEngine による大幅な手抜き動作です。
100
+
101
+ * 最近はやりの 3 * 1/3 + 1 が 10になるってのも Javascript なら正しい結果になります。
102
+
103
+ *
104
+
105
+ * @author masa01
106
+
107
+ *
108
+
109
+ */
110
+
111
+ public class Dentaku extends JFrame {
112
+
113
+
114
+
115
+ /**
116
+
117
+ *
118
+
119
+ */
120
+
121
+ private static final long serialVersionUID = 1L;
122
+
123
+
124
+
125
+ public static void main(String...args) {
126
+
127
+
128
+
129
+ new Dentaku();
130
+
131
+
132
+
133
+ }
134
+
135
+
136
+
137
+ private ScriptEngine engine;
138
+
139
+
140
+
141
+ private Map<String, Object> paramMap;
142
+
143
+
144
+
145
+ private StringBuilder buffer;
146
+
147
+
148
+
149
+ public Dentaku() {
150
+
151
+ // JavaScript Engine nathhorn を取得
152
+
153
+ engine = new ScriptEngineManager().getEngineByName("JavaScript");
154
+
155
+ paramMap = new HashMap<String, Object>();
156
+
157
+ buffer = new StringBuilder(500);
158
+
159
+
160
+
161
+ init();
162
+
163
+
164
+
165
+ initLayout();
166
+
167
+
168
+
169
+ super.setDefaultCloseOperation(EXIT_ON_CLOSE);
170
+
171
+
172
+
173
+ super.setSize(400, 400);
174
+
175
+
176
+
177
+ super.setVisible(true);
178
+
179
+
180
+
181
+ }
182
+
183
+ private void init() {
184
+
185
+ // 電卓計算結果の途中保存用レジスタの登録と初期化
186
+
187
+ paramMap.put("$0", null);
188
+
189
+ paramMap.put("$1", null);
190
+
191
+ paramMap.put("$2", null);
192
+
193
+ paramMap.put("$3", null);
194
+
195
+ paramMap.put("$4", null);
196
+
197
+ paramMap.put("$5", null);
198
+
199
+ paramMap.put("$6", null);
200
+
201
+ paramMap.put("$7", null);
202
+
203
+
204
+
205
+ //$err エラーメッセージ格納用変数を定義する
206
+
207
+ paramMap.put("$err", null);
208
+
209
+
210
+
211
+ // レジスタを使わない計算の結果はすべてここに入る
212
+
213
+ paramMap.put("$ans", null);
214
+
215
+
216
+
217
+ // バッファクリア
218
+
219
+ buffer.setLength(0);
220
+
221
+ }
222
+
223
+
224
+
225
+ /**
226
+
227
+ * try { sys.$ans = ("画面で入力した羅列"); } catch (e) { sys.$err = e; }
228
+
229
+ * を作ります。ScriptEngine の eval を実行すると結果が java から取得できるっていう感じですね。
230
+
231
+ * @return
232
+
233
+ */
234
+
235
+ private String createJs() {
236
+
237
+ return null;
238
+
239
+ }
240
+
241
+
242
+
243
+ /**
244
+
245
+ * 画面レイアウトを変更するならオーバーライドしてください。
246
+
247
+ * デフォルトでは、
248
+
249
+ * BorderLayout で、
250
+
251
+ * North : 計算入力用テキストフィールド(直接入力不可)、readonly
252
+
253
+ * Center: 数字と記号とその他パーツを 4 * 4 のマトリクスにしたもの
254
+
255
+ * South : 実行結果の履歴
256
+
257
+ *
258
+
259
+ */
260
+
261
+ protected void initLayout() {
262
+
263
+
264
+
265
+ ActionListener be = new ActionListener() {
266
+
267
+
268
+
269
+ @Override
270
+
271
+ public void actionPerformed(ActionEvent e) {
272
+
273
+
274
+
275
+ System.out.println(e.getActionCommand());
276
+
277
+ }
278
+
279
+ };
280
+
281
+
282
+
283
+ JPanel gp = new GridLayoutPanel(6, 4)
284
+
285
+ .add(0, 0, createButton("MC", "MC", "MC", be))
286
+
287
+ .add(0, 1, createButton("C", "C", "C", be))
288
+
289
+ .add(0, 2, createButton(" ", " ", "NOP", be))
290
+
291
+ .add(0, 3, createButton(" ", " ", "NOP", be))
292
+
293
+
294
+
295
+ .add(1, 0, createButton("LPAR", "(", "LPAR", be))
296
+
297
+ .add(1, 1, createButton("RPAR", ")", "RPAR", be))
298
+
299
+ .add(1, 2, createButton("ADD", "+", "ADD", be))
300
+
301
+ .add(1, 3, createButton("SUB", "-", "SUB", be))
302
+
303
+
304
+
305
+ .add(2, 0, createButton("ONE", "1", "ONE", be))
306
+
307
+ .add(2, 1, createButton("TWO", "2", "TWO", be))
308
+
309
+ .add(2, 2, createButton("TRE", "3", "TRE", be))
310
+
311
+ .add(2, 3, createButton("DIV", "/", "DIV", be))
312
+
313
+
314
+
315
+ .add(3, 0, createButton("FOR", "4", "FOR", be))
316
+
317
+ .add(3, 1, createButton("FIV", "5", "FIV", be))
318
+
319
+ .add(3, 2, createButton("SIX", "6", "SIX", be))
320
+
321
+ .add(3, 3, createButton("MUL", "*", "MUL", be))
322
+
323
+
324
+
325
+ .add(4, 0, createButton("SVN", "4", "SVN", be))
326
+
327
+ .add(4, 1, createButton("EGT", "5", "EGT", be))
328
+
329
+ .add(4, 2, createButton("NIN", "9", "NIN", be))
330
+
331
+ .add(4, 3, createButton("MOD", "%", "MOD", be))
332
+
333
+
334
+
335
+ .add(5, 0, createButton("EQU", "=", "EQU", be))
336
+
337
+ .add(5, 1, createButton("ZRO", "0", "ZRO", be))
338
+
339
+ .add(5, 2, createButton("DOL", "$", "DOL", be))
340
+
341
+ .add(5, 3, createButton("USC", "_", "USC", be))
342
+
343
+
344
+
345
+ .done();
346
+
347
+ /*
348
+
349
+ * MC,C, ,
350
+
351
+ * (,),+,-
352
+
353
+ * 1,2,3,/
354
+
355
+ * 4,5,6,*
356
+
357
+ * 7,8,9,%
358
+
359
+ * =,0,$,_
360
+
361
+ */
362
+
363
+
364
+
365
+ super.getContentPane().add(gp, BorderLayout.CENTER);
366
+
367
+
368
+
369
+ }
370
+
371
+ /**
372
+
373
+ * ボタンを作る、アイコンボタンにしたいならオーバーライドする
374
+
375
+ * @param name
376
+
377
+ * @param text
378
+
379
+ * @param command
380
+
381
+ * @return
382
+
383
+ */
384
+
385
+ protected JButton createButton(String name, String text, String command, ActionListener actionListener) {
386
+
387
+ JButton jb = new JButton(text);
388
+
389
+ // アクションコマンド設定、集約イベントハンドラ内でこのコマンドを取得して対応アクションで処理する
390
+
391
+ jb.setActionCommand(command);
392
+
393
+ // 名前を設定
394
+
395
+ jb.setName(name);
396
+
397
+ // 集約?イベントを設定する
398
+
399
+ jb.addActionListener(actionListener);
400
+
401
+ return jb;
402
+
403
+ }
404
+
405
+
406
+
407
+ /**
408
+
409
+ * @author masa01
410
+
411
+ *
412
+
413
+ */
414
+
415
+
416
+
417
+ }
418
+
419
+
420
+
421
+ ```
422
+
423
+
424
+
425
+ GridLayoutPanel.java で保存
426
+
427
+ ```java
428
+
429
+ package jp.kanagawa.caron.swing.tools;
430
+
431
+
432
+
433
+ import java.awt.Component;
434
+
435
+ import java.awt.GridLayout;
436
+
437
+ import java.util.ArrayList;
438
+
439
+ import java.util.Comparator;
440
+
441
+ import java.util.List;
442
+
443
+
444
+
445
+ import javax.swing.JPanel;
446
+
447
+
448
+
449
+ public class GridLayoutPanel extends GridLayout {
450
+
451
+
452
+
453
+ /**
454
+
455
+ *
456
+
457
+ */
458
+
459
+ private static final long serialVersionUID = 1L;
460
+
461
+
462
+
463
+ private List<Compo> comps;
464
+
465
+ private int rows, cols;
466
+
467
+
468
+
469
+ class Compo {
470
+
471
+ int x, y;
472
+
473
+ int val;
474
+
475
+ Component compo;
476
+
477
+ public Compo(int x, int y, Component compo) {
478
+
479
+ this.x = x;
480
+
481
+ this.y = y;
482
+
483
+ this.compo = compo;
484
+
485
+ this.val = (x + 1) * (y + 1);
486
+
487
+ }
488
+
489
+ }
490
+
491
+
492
+
493
+ public GridLayoutPanel(int row, int col) {
494
+
495
+ super(row, col);
496
+
497
+
498
+
499
+ rows = row;
500
+
501
+ cols = col;
502
+
503
+ comps = new ArrayList<Compo>();
504
+
505
+ }
506
+
507
+
508
+
509
+ public GridLayoutPanel add(int row, int col, Component comp) {
510
+
511
+
512
+
513
+ Utils.assertRange(row, 0, rows);
514
+
515
+ Utils.assertRange(col, 0, cols);
516
+
517
+ Utils.assertNull(comp);
518
+
519
+
520
+
521
+ comps.add(new Compo(col, row, comp));
522
+
523
+ return this;
524
+
525
+ }
526
+
527
+ public JPanel done() {
528
+
529
+ comps.sort(new Comparator<Compo>() {
530
+
531
+ @Override
532
+
533
+ public int compare(Compo o1, Compo o2) {
534
+
535
+ // o1 - o2 は昇順ソート
536
+
537
+ // o2 - o1 は降順ソート
538
+
539
+ int v1 = o1.y * rows + o1.x;
540
+
541
+ int v2 = o2.y * rows + o2.x;
542
+
543
+
544
+
545
+ return v1 - v2;
546
+
547
+
548
+
549
+
550
+
551
+ }
552
+
553
+ });
554
+
555
+ JPanel jp = new JPanel(this);
556
+
557
+ for (Compo c : comps) {
558
+
559
+ jp.add(c.compo);
560
+
561
+ }
562
+
563
+
564
+
565
+ return jp;
566
+
567
+ }
568
+
569
+ }
570
+
571
+
572
+
573
+
574
+
575
+ ```
576
+
577
+
578
+
579
+ Utils.java で保存
580
+
581
+ ```java
582
+
583
+ package jp.kanagawa.caron.swing.tools;
584
+
585
+
586
+
587
+ public final class Utils {
588
+
589
+
590
+
591
+ public static void assertNull(Object o) {
592
+
593
+ if (o != null) return;
594
+
595
+ throw new RuntimeException("null はいかん。");
596
+
597
+ }
598
+
599
+
600
+
601
+ public static void assertRange(int a, int rs, int re) {
602
+
603
+ if (a >= rs && a < re) return;
604
+
605
+ throw new RuntimeException("範囲が狂っとる");
606
+
607
+
608
+
609
+ }
610
+
611
+
612
+
613
+ }
614
+
615
+
616
+
617
+ ```
618
+
619
+
620
+
621
+ 全部対応するファイル名で保存、パッケージパスを自分とこに合わせて、Dentaku.java を実行してみてください。
622
+
623
+ 画面サイズを変更しても元に戻すとちゃんとレイアウトが戻ります。
624
+
625
+
626
+
627
+