質問編集履歴
1
コードを書き直した、作りかけのjavaファイルをURLでダウンロードできるようにした、質問文を少し詳しくした。
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,101 +1,483 @@
|
|
1
|
+
【書き直し】
|
2
|
+
|
1
3
|
今作っているプログラムは、メインメソッドを含むクラスが実行されると新しいファイルを作り、フレームを表示します。
|
2
4
|
|
3
5
|
そのクラスから、別のクラスを呼び出して新しくフレームを作り、新しいフレーム内での操作で最初のクラスにある変数の値を変更したいんです。
|
4
6
|
|
7
|
+
下のように書くと、新しく作ったフレームのボタンを押したとき、毎回makeHTML()が実行されてしまっていらないファイルを作ってしまいます。
|
8
|
+
|
9
|
+
/* */で囲んだコメントは、文を省略しているところです。
|
10
|
+
|
11
|
+
makeHTML()を実行せずに変数だけを使う方法を教えてください。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
全部のjavaファイルやclassファイルなどは、[ここ](http://fromn.webcrow.jp/makeHTML.zip)からダウンロードできます。
|
16
|
+
|
17
|
+
mhE.batで実行されます。
|
18
|
+
|
5
19
|
|
6
20
|
|
7
21
|
```java
|
8
22
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
A
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
23
|
+
import java.awt.*;
|
24
|
+
|
25
|
+
import java.awt.event.*;
|
26
|
+
|
27
|
+
import java.net.URL;
|
28
|
+
|
29
|
+
import javax.swing.*;
|
30
|
+
|
31
|
+
import java.io.*;
|
32
|
+
|
33
|
+
import java.util.*;
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
class makeHTML extends JFrame implements ActionListener{
|
38
|
+
|
39
|
+
|
40
|
+
|
41
|
+
public static void main(String[] args){
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
makeHTML f = new makeHTML();
|
46
|
+
|
47
|
+
f.setTitle("問題作成ツール");
|
48
|
+
|
49
|
+
f.setBounds(200,200,600,400);
|
50
|
+
|
51
|
+
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
52
|
+
|
53
|
+
f.setVisible(true);
|
54
|
+
|
55
|
+
}
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
JPanel base;
|
60
|
+
|
61
|
+
JTextArea mondai;
|
62
|
+
|
63
|
+
JButton tButton;
|
64
|
+
|
65
|
+
JButton fButton;
|
66
|
+
|
67
|
+
JButton next;
|
68
|
+
|
69
|
+
JButton logButton;
|
70
|
+
|
71
|
+
JButton finish;
|
72
|
+
|
73
|
+
JLabel[] log;
|
74
|
+
|
75
|
+
File html;
|
76
|
+
|
77
|
+
ArrayList<String> mon = new ArrayList<String>();//この変数をclass aflから呼び出して使いたい
|
78
|
+
|
79
|
+
String ans;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
makeHTML(){
|
84
|
+
|
85
|
+
//略
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
html = new File("過去問.html");
|
90
|
+
|
91
|
+
int i = 0;
|
92
|
+
|
93
|
+
while(html.exists()){
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
i ++;
|
98
|
+
|
99
|
+
html = new File("過去問 (" + i + ").html");
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
try{
|
104
|
+
|
105
|
+
if(html.createNewFile()) System.out.println("createNewFile(" + html.getName() + ")");
|
106
|
+
|
107
|
+
} catch(IOException e){}
|
108
|
+
|
109
|
+
try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html, true), "UTF-8")))){
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
/*out.println()でHTMLの前半を書く*/
|
114
|
+
|
115
|
+
} catch(IOException e){}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
base = new JPanel();
|
120
|
+
|
121
|
+
//略
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
JPanel kotae = new JPanel();
|
126
|
+
|
127
|
+
//略
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
mondai = new JTextArea();
|
132
|
+
|
133
|
+
//略
|
134
|
+
|
135
|
+
mondai.setText("問題文");
|
136
|
+
|
137
|
+
base.add(mondai);
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
tButton = new JButton("〇");
|
142
|
+
|
143
|
+
//略
|
144
|
+
|
145
|
+
tButton.addActionListener(this);
|
146
|
+
|
147
|
+
tButton.setActionCommand("t");
|
148
|
+
|
149
|
+
kotae.add(tButton);
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
fButton = new JButton("×");
|
154
|
+
|
155
|
+
//略
|
156
|
+
|
157
|
+
fButton.addActionListener(this);
|
158
|
+
|
159
|
+
fButton.setActionCommand("f");
|
160
|
+
|
161
|
+
kotae.add(fButton);
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
base.add(kotae);
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
next = new JButton("次へ");
|
170
|
+
|
171
|
+
//略
|
172
|
+
|
173
|
+
next.addActionListener(this);
|
174
|
+
|
175
|
+
next.setActionCommand("next");
|
176
|
+
|
177
|
+
base.add(next);
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
finish = new JButton("終了");
|
182
|
+
|
183
|
+
//略
|
184
|
+
|
185
|
+
finish.addActionListener(this);
|
186
|
+
|
187
|
+
finish.setActionCommand("fin");
|
188
|
+
|
189
|
+
base.add(finish);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
logButton = new JButton("ログから");
|
194
|
+
|
195
|
+
//略
|
196
|
+
|
197
|
+
logButton.addActionListener(this);
|
198
|
+
|
199
|
+
logButton.setActionCommand("log");
|
200
|
+
|
201
|
+
base.add(logButton);
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
add(base);
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
public void actionPerformed(ActionEvent e){
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
String com = e.getActionCommand();
|
218
|
+
|
219
|
+
if(com.equals("t")){
|
220
|
+
|
221
|
+
ans = "true";
|
222
|
+
|
223
|
+
tButton.setForeground(Color.BLUE);
|
224
|
+
|
225
|
+
} else if(com.equals("f")){
|
226
|
+
|
227
|
+
ans = "false";
|
228
|
+
|
229
|
+
fButton.setForeground(Color.BLUE);
|
230
|
+
|
231
|
+
} else if(com.equals("next")) setAns(false);
|
232
|
+
|
233
|
+
else if(com.equals("fin")) setAns(true);
|
234
|
+
|
235
|
+
else if(com.equals("log")) addFromLog();
|
236
|
+
|
237
|
+
}
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
private void setAns(boolean ending){
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
fButton.setForeground(Color.BLACK);
|
246
|
+
|
247
|
+
tButton.setForeground(Color.BLACK);
|
248
|
+
|
249
|
+
String c = ",";
|
250
|
+
|
251
|
+
if(ending) c = "";
|
252
|
+
|
253
|
+
mon.add(mondai.getText());
|
254
|
+
|
255
|
+
try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html, true), "UTF-8")))){
|
256
|
+
|
257
|
+
|
258
|
+
|
259
|
+
out.println("\t\t" + ans + c);
|
260
|
+
|
261
|
+
if(ending){
|
262
|
+
|
263
|
+
|
264
|
+
|
265
|
+
/*out.println()でHTMLの続きを書く*/
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
int i = 0;
|
270
|
+
|
271
|
+
while(i < mon.size()){
|
272
|
+
|
273
|
+
|
274
|
+
|
275
|
+
out.println("<div>");
|
276
|
+
|
277
|
+
out.println(brTag(mon.get(i)));
|
278
|
+
|
279
|
+
out.println("</div>");
|
280
|
+
|
281
|
+
i ++;
|
282
|
+
|
283
|
+
}
|
284
|
+
|
285
|
+
|
286
|
+
|
287
|
+
/*out.println()でHTMLの後ろを書く*/
|
288
|
+
|
289
|
+
}
|
290
|
+
|
291
|
+
} catch(IOException e){}
|
292
|
+
|
293
|
+
try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("過去データ.otft", true), "UTF-8")))){
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
out.println(mondai.getText());
|
298
|
+
|
299
|
+
out.println("&ANS=" + ans);
|
300
|
+
|
301
|
+
} catch(IOException e){}
|
302
|
+
|
303
|
+
mondai.setText("");
|
304
|
+
|
305
|
+
if(ending) System.exit(0);
|
306
|
+
|
307
|
+
}
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
private String brTag(String str){
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
between bet = new between();
|
316
|
+
|
317
|
+
String st = bet.Change(str, "\n", "<br>");//strの中の"\n"を全て"<br>"に変更する
|
318
|
+
|
319
|
+
return st;
|
320
|
+
|
321
|
+
}
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
private void addFromLog(){
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
afl a = new afl(html);
|
330
|
+
|
331
|
+
a.setBounds(800,100,400,600);
|
332
|
+
|
333
|
+
a.setTitle("選ぶ");
|
334
|
+
|
335
|
+
a.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
|
336
|
+
|
337
|
+
a.setVisible(true);
|
338
|
+
|
339
|
+
}
|
68
340
|
|
69
341
|
}
|
70
342
|
|
71
343
|
|
72
344
|
|
73
|
-
class
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
345
|
+
class afl extends JFrame implements ActionListener{
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
JPanel[] ap;
|
350
|
+
|
351
|
+
JLabel[] disp;
|
352
|
+
|
353
|
+
JButton[] addLog;
|
354
|
+
|
355
|
+
String[][] mon;
|
356
|
+
|
357
|
+
String[] ans;
|
358
|
+
|
359
|
+
File fl;
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
afl(File html){
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
fl = html;
|
368
|
+
|
369
|
+
int i = 0;
|
370
|
+
|
371
|
+
int a = 0;
|
372
|
+
|
373
|
+
JPanel base = new JPanel();
|
374
|
+
|
375
|
+
base.setLayout(new BoxLayout(base, BoxLayout.Y_AXIS));
|
376
|
+
|
377
|
+
try{
|
378
|
+
|
379
|
+
|
380
|
+
|
381
|
+
String fline = "";
|
382
|
+
|
383
|
+
BufferedReader fr = new BufferedReader(new FileReader("過去データ.otft"));
|
384
|
+
|
385
|
+
while(true){
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
fline = fr.readLine();
|
390
|
+
|
391
|
+
if(fline == null) break;
|
392
|
+
|
393
|
+
else if(fline.startsWith("&ANS=")) a ++;
|
394
|
+
|
395
|
+
}
|
396
|
+
|
397
|
+
fr.close();
|
398
|
+
|
399
|
+
ap = new JPanel[a];
|
400
|
+
|
401
|
+
disp = new JLabel[a];
|
402
|
+
|
403
|
+
addLog = new JButton[a]; //追加ボタン
|
404
|
+
|
405
|
+
mon = new String[a][10];
|
406
|
+
|
407
|
+
ans = new String[a];
|
408
|
+
|
409
|
+
|
410
|
+
|
411
|
+
a = 0;
|
412
|
+
|
413
|
+
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("過去データ.otft"), "UTF-8"));
|
414
|
+
|
415
|
+
String line = br.readLine();
|
416
|
+
|
417
|
+
while(line != null){
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
if(!line.startsWith("&ANS=")){
|
422
|
+
|
423
|
+
|
424
|
+
|
425
|
+
mon[i][a] = line;
|
426
|
+
|
427
|
+
a ++;
|
428
|
+
|
429
|
+
} else{
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
/*mon[][]をJPanelに入れ、「追加」ボタンを入れる。アクションコマンドに"" + i*/
|
434
|
+
|
435
|
+
a = 0;
|
436
|
+
|
437
|
+
i ++;
|
438
|
+
|
439
|
+
}
|
440
|
+
|
441
|
+
line = br.readLine();
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
} catch(IOException e){}
|
446
|
+
|
447
|
+
add(base);
|
448
|
+
|
449
|
+
}
|
450
|
+
|
451
|
+
|
452
|
+
|
453
|
+
public void actionPerformed(ActionEvent e){
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
int com = new Integer(e.getActionCommand());
|
458
|
+
|
459
|
+
try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fl, true), "UTF-8")))){
|
460
|
+
|
461
|
+
|
462
|
+
|
463
|
+
out.println("\t\t" + ans[com] + ",");
|
464
|
+
|
465
|
+
} catch(IOException ex){}
|
466
|
+
|
467
|
+
new makeHTML().mon.add(Mondai(com, "<br>"));
|
468
|
+
|
469
|
+
}
|
470
|
+
|
471
|
+
|
472
|
+
|
473
|
+
private String Mondai(int i, String n){
|
474
|
+
|
475
|
+
|
476
|
+
|
477
|
+
/*mon[][]を全て繋げる*/
|
478
|
+
|
479
|
+
}
|
94
480
|
|
95
481
|
}
|
96
482
|
|
97
483
|
```
|
98
|
-
|
99
|
-
こう書くと、class B の new A().al.add()でA()が再度実行されてしまって、ボタンを押すたびに必要もないのにファイルが作成されてしまいます。
|
100
|
-
|
101
|
-
実行せずに変数だけ操作する方法はありますか?
|