質問編集履歴

1

コードの修正

2015/10/18 07:13

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -32,21 +32,133 @@
32
32
 
33
33
 
34
34
 
35
+ package kadai;
36
+
37
+
38
+
39
+ import java.awt.BorderLayout;
40
+
41
+ import java.awt.event.ActionEvent;
42
+
43
+ import java.awt.event.ActionListener;
44
+
45
+ import java.io.File;
46
+
47
+ import java.io.FileNotFoundException;
48
+
49
+ import java.io.FileReader;
50
+
51
+ import java.io.FileWriter;
52
+
53
+ import java.io.IOException;
54
+
55
+
56
+
57
+ import javax.swing.ButtonGroup;
58
+
59
+ import javax.swing.JButton;
60
+
61
+ import javax.swing.JComboBox;
62
+
63
+ import javax.swing.JFileChooser;
64
+
65
+ import javax.swing.JFrame;
66
+
67
+ import javax.swing.JLabel;
68
+
69
+ import javax.swing.JOptionPane;
70
+
71
+ import javax.swing.JPanel;
72
+
73
+ import javax.swing.JRadioButton;
74
+
75
+ import javax.swing.JScrollPane;
76
+
77
+ import javax.swing.JTextArea;
78
+
79
+ import javax.swing.JTextField;
80
+
81
+
82
+
83
+
84
+
85
+ public class TextTool extends JFrame implements ActionListener {
86
+
87
+
88
+
89
+
90
+
35
- JRadioButton rb = new JRadioButton("新規作成", true);
91
+ JRadioButton rb = new JRadioButton("新規作成", true);
36
-
92
+
37
- JRadioButton rb1 = new JRadioButton("上書き保存", false);
93
+ JRadioButton rb1 = new JRadioButton("上書き保存", false);
94
+
38
-
95
+ JRadioButton rb2 = new JRadioButton("削除", false);
96
+
97
+ ButtonGroup gr =new ButtonGroup();
98
+
99
+
100
+
101
+ JButton jb = new JButton("終了");
102
+
103
+ JButton jb1 = new JButton("開く");
104
+
105
+ JButton jb2 = new JButton("クリア");
106
+
107
+ JButton jb3 = new JButton("削除");
108
+
39
- JButton jb = new JButton("保存");
109
+ JButton jb4 = new JButton("保存");
110
+
111
+ JButton jb5 = new JButton("・・・");
112
+
113
+ JButton jb6 = new JButton("・・・");
114
+
115
+
116
+
117
+ JLabel l1 = new JLabel("動作:");
118
+
119
+ JLabel l2 = new JLabel("入力ファイル:");
120
+
121
+ JLabel l3 = new JLabel("出力方法:");
122
+
123
+ JLabel l4 = new JLabel("出力ファイル:");
124
+
125
+
126
+
127
+ JTextField t = new JTextField();
128
+
129
+ JTextField t1 = new JTextField();
130
+
131
+
132
+
133
+ JFileChooser jc = new JFileChooser();
134
+
135
+
40
136
 
41
137
  JTextArea ta = new JTextArea();
42
138
 
139
+
140
+
43
-
141
+ JScrollPane sp = new JScrollPane(ta);
142
+
143
+
144
+
44
-
145
+ String[] filesave = {"", ""};
146
+
147
+ JComboBox c =new JComboBox(filesave);
148
+
149
+
150
+
45
- public TextTool() {
151
+ public TextTool() {
46
-
47
-
48
-
152
+
153
+
154
+
49
- super("メモ帳");
155
+ super("メモ帳");
156
+
157
+
158
+
159
+
160
+
161
+
50
162
 
51
163
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
52
164
 
@@ -54,7 +166,13 @@
54
166
 
55
167
  ta.setLineWrap(true);
56
168
 
169
+
170
+
171
+
172
+
173
+ //パネル スクロール
174
+
57
- JPanel p = new JPanel();
175
+ JPanel p = new JPanel();
58
176
 
59
177
  p.setLayout(null);
60
178
 
@@ -62,55 +180,331 @@
62
180
 
63
181
  p.add(sp);
64
182
 
183
+
184
+
185
+ //ラジオ
186
+
65
- rb.setBounds(240, 15, 85, 20);
187
+ rb.setBounds(240, 15, 85, 20);
66
188
 
67
189
  rb1.setBounds(335, 15, 85, 20);
68
190
 
191
+ rb2.setBounds(420, 15, 85, 20);
192
+
69
193
  gr.add(rb);
70
194
 
71
195
  gr.add(rb1);
72
196
 
197
+ gr.add(rb2);
198
+
73
- p.add(rb);
199
+ p.add(rb);
74
200
 
75
201
  p.add(rb1);
76
202
 
203
+ p.add(rb2);
204
+
205
+
206
+
207
+
208
+
209
+ //ボタン
210
+
77
211
  jb.setBounds(575, 50, 90, 20);
78
212
 
213
+ jb1.setBounds(575, 90, 90, 20);
214
+
215
+ jb2.setBounds(575, 165, 90, 30);
216
+
217
+ jb3.setBounds(575, 200, 90, 30);
218
+
219
+ jb4.setBounds(575, 340, 90, 50);
220
+
221
+ jb5.setBounds(500, 400, 20, 23);
222
+
223
+ jb6.setBounds(500, 55, 20, 23);
224
+
79
225
  p.add(jb);
80
226
 
81
-
227
+ p.add(jb1);
228
+
229
+ p.add(jb2);
230
+
231
+ p.add(jb3);
232
+
233
+ p.add(jb4);
234
+
235
+ p.add(jb5);
236
+
237
+ p.add(jb6);
238
+
239
+ jb1.setEnabled(false);
240
+
241
+ jb6.setEnabled(false);
242
+
243
+
244
+
245
+ //ラベル
246
+
247
+ l1.setBounds(180, 17, 50, 15);
248
+
249
+ l2.setBounds(75, 55, 100, 15);
250
+
251
+ l3.setBounds(100, 350, 80, 30);
252
+
253
+ l4.setBounds(80, 400, 100, 15);
254
+
255
+ p.add(l1);
256
+
257
+ p.add(l2);
258
+
259
+ p.add(l3);
260
+
261
+ p.add(l4);
262
+
263
+
264
+
265
+ //テキストエリア
266
+
267
+ t.setBounds(174, 55, 320, 23);
268
+
269
+ t1.setBounds(174, 400, 323, 23);
270
+
271
+ p.add(t);
272
+
273
+ p.add(t1);
274
+
275
+ t.setEnabled(false);
276
+
277
+
278
+
279
+ c .setBounds(174, 352, 323, 19);
280
+
281
+ c.setEnabled(false);
282
+
283
+
284
+
285
+ p.add(c);
286
+
287
+
288
+
289
+
290
+
291
+ rb.addActionListener(this);
292
+
293
+ rb1.addActionListener(this);
294
+
295
+ rb2.addActionListener(this);
296
+
297
+
298
+
299
+
300
+
301
+ jb.addActionListener(this);
302
+
303
+ jb1.addActionListener(this);
304
+
305
+ jb2.addActionListener(this);
306
+
307
+ jb4.addActionListener(this);
308
+
309
+
310
+
311
+ getContentPane().add(p, BorderLayout.CENTER);
312
+
313
+ }
314
+
315
+
316
+
317
+ public static void main(String[] args) {
318
+
319
+ new TextTool().setVisible(true);
320
+
321
+ }
322
+
323
+
324
+
325
+
326
+
327
+
328
+
329
+
82
330
 
83
331
  public void actionPerformed(ActionEvent e) {
84
332
 
85
333
 
86
334
 
87
- if (e.getSource().equals(jb4)) {
88
-
89
- String selectvalues[] = {"実行", "終了"};
90
-
91
- JOptionPane.showOptionDialog(this,
92
-
93
- "ファイルを作成します。よろしですか?", "",
94
-
95
- JOptionPane.YES_NO_OPTION,
96
-
97
- JOptionPane.ERROR_MESSAGE,
98
-
99
- null,
100
-
101
- selectvalues,
102
-
103
- selectvalues[0]);
104
-
105
- System.out.println("actionPerformed()");
106
-
107
- saveFile();
108
-
109
-
110
-
111
- if (e.getSource().equals(jb4)) {
112
-
113
- String selectvalues[] = {"実行", "終了"};
335
+ if (e.getSource() == (rb)) {
336
+
337
+ System.out.println("actionPerformed()");
338
+
339
+ ta.setText("");
340
+
341
+ c.setEnabled(false);
342
+
343
+ jb1.setEnabled(false);
344
+
345
+ jb4.setEnabled(true);
346
+
347
+ jb6.setEnabled(false);
348
+
349
+ ta.setEnabled(true);
350
+
351
+ t.setEnabled(false);
352
+
353
+
354
+
355
+
356
+
357
+ }
358
+
359
+ if (e.getSource() == (rb1)) {
360
+
361
+ System.out.println("actionPerformed()");
362
+
363
+ c.setEnabled(true);
364
+
365
+ jb1.setEnabled(true);
366
+
367
+ jb4.setEnabled(true);
368
+
369
+ jb6.setEnabled(true);
370
+
371
+ ta.setEnabled(true);
372
+
373
+ t.setEnabled(true);
374
+
375
+
376
+
377
+ }
378
+
379
+ if (e.getSource() ==(rb2)) {
380
+
381
+ System.out.println("actionPerformed()");
382
+
383
+ jb1.setEnabled(true);
384
+
385
+ jb4.setEnabled(false);
386
+
387
+ jb6.setEnabled(true);
388
+
389
+ ta.setEnabled(false);
390
+
391
+ t.setEnabled(true);
392
+
393
+ t.setText("");
394
+
395
+ }
396
+
397
+
398
+
399
+ if (e.getSource() == (jb)) {
400
+
401
+ System.out.println("actionPerformed()");
402
+
403
+ System.exit(0);
404
+
405
+ }
406
+
407
+
408
+
409
+ if (e.getSource() == (jb1)) {
410
+
411
+ openFile();
412
+
413
+ }
414
+
415
+
416
+
417
+
418
+
419
+ if (e.getSource() == jb2) {
420
+
421
+
422
+
423
+ String selectvalues[] = {"実行", "終了"};
424
+
425
+ JOptionPane.showOptionDialog(this,
426
+
427
+ "内容をクリアにします。よろしですか?", "",
428
+
429
+ JOptionPane.YES_NO_OPTION,
430
+
431
+ JOptionPane.ERROR_MESSAGE,
432
+
433
+ null,
434
+
435
+ selectvalues,
436
+
437
+ selectvalues[0]);
438
+
439
+ System.out.println("actionPerformed()");
440
+
441
+ ta.setText("");
442
+
443
+ }
444
+
445
+ if (e.getSource() == jb3) {
446
+
447
+ String selectvalues[] = {"実行", "終了"};
448
+
449
+
450
+
451
+ JOptionPane.showOptionDialog(this,
452
+
453
+ "ファイルを削除します。よろしですか?", ""
454
+
455
+ , JOptionPane.YES_NO_OPTION,
456
+
457
+ JOptionPane.ERROR_MESSAGE,
458
+
459
+ null,
460
+
461
+ selectvalues,
462
+
463
+ selectvalues[0]);
464
+
465
+ System.out.println("actionPerformed()");
466
+
467
+ }
468
+
469
+
470
+
471
+
472
+
473
+ boolean c = true;
474
+
475
+ if (e.getSource().equals(jb4)) {
476
+
477
+ String selectvalues[] = {"実行", "終了"};
478
+
479
+
480
+
481
+ JOptionPane.showOptionDialog(this,
482
+
483
+ "ファイルを作成します。よろしですか?", ""
484
+
485
+ , JOptionPane.YES_NO_OPTION,
486
+
487
+ JOptionPane.ERROR_MESSAGE,
488
+
489
+ null,
490
+
491
+ selectvalues,
492
+
493
+ selectvalues[0]);
494
+
495
+ System.out.println("actionPerformed()");
496
+
497
+ saveFile();
498
+
499
+
500
+
501
+ }
502
+
503
+ if (e.getSource().equals(jb4)) {
504
+
505
+ String selectvalues[] = {"実行", "終了"};
506
+
507
+
114
508
 
115
509
  JOptionPane.showOptionDialog(this,
116
510
 
@@ -134,7 +528,43 @@
134
528
 
135
529
  }
136
530
 
137
- }
531
+ }
532
+
533
+ void openFile() {
534
+
535
+ int returnVal = jc.showOpenDialog(this);
536
+
537
+ try {
538
+
539
+ if (returnVal == JFileChooser.APPROVE_OPTION) {
540
+
541
+ File file = jc.getSelectedFile();
542
+
543
+ FileReader reader = new FileReader(file);
544
+
545
+ ta.read(reader, null);
546
+
547
+ setTitle(file.getAbsolutePath());
548
+
549
+ reader.close();
550
+
551
+ }
552
+
553
+ } catch (FileNotFoundException e) {
554
+
555
+ e.printStackTrace();
556
+
557
+ } catch (IOException e) {
558
+
559
+ e.printStackTrace();
560
+
561
+ }
562
+
563
+ }
564
+
565
+
566
+
567
+
138
568
 
139
569
 
140
570