【書き直し】
今作っているプログラムは、メインメソッドを含むクラスが実行されると新しいファイルを作り、フレームを表示します。
そのクラスから、別のクラスを呼び出して新しくフレームを作り、新しいフレーム内での操作で最初のクラスにある変数の値を変更したいんです。
下のように書くと、新しく作ったフレームのボタンを押したとき、毎回makeHTML()が実行されてしまっていらないファイルを作ってしまいます。
/* */で囲んだコメントは、文を省略しているところです。
makeHTML()を実行せずに変数だけを使う方法を教えてください。
全部のjavaファイルやclassファイルなどは、ここからダウンロードできます。
mhE.batで実行されます。
java
1import java.awt.*; 2import java.awt.event.*; 3import java.net.URL; 4import javax.swing.*; 5import java.io.*; 6import java.util.*; 7 8class makeHTML extends JFrame implements ActionListener{ 9 10 public static void main(String[] args){ 11 12 makeHTML f = new makeHTML(); 13 f.setTitle("問題作成ツール"); 14 f.setBounds(200,200,600,400); 15 f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 16 f.setVisible(true); 17 } 18 19 JPanel base; 20 JTextArea mondai; 21 JButton tButton; 22 JButton fButton; 23 JButton next; 24 JButton logButton; 25 JButton finish; 26 JLabel[] log; 27 File html; 28 ArrayList<String> mon = new ArrayList<String>();//この変数をclass aflから呼び出して使いたい 29 String ans; 30 31 makeHTML(){ 32 //略 33 34 html = new File("過去問.html"); 35 int i = 0; 36 while(html.exists()){ 37 38 i ++; 39 html = new File("過去問 (" + i + ").html"); 40 } 41 try{ 42 if(html.createNewFile()) System.out.println("createNewFile(" + html.getName() + ")"); 43 } catch(IOException e){} 44 try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html, true), "UTF-8")))){ 45 46 /*out.println()でHTMLの前半を書く*/ 47 } catch(IOException e){} 48 49 base = new JPanel(); 50 //略 51 52 JPanel kotae = new JPanel(); 53 //略 54 55 mondai = new JTextArea(); 56 //略 57 mondai.setText("問題文"); 58 base.add(mondai); 59 60 tButton = new JButton("〇"); 61 //略 62 tButton.addActionListener(this); 63 tButton.setActionCommand("t"); 64 kotae.add(tButton); 65 66 fButton = new JButton("×"); 67 //略 68 fButton.addActionListener(this); 69 fButton.setActionCommand("f"); 70 kotae.add(fButton); 71 72 base.add(kotae); 73 74 next = new JButton("次へ"); 75 //略 76 next.addActionListener(this); 77 next.setActionCommand("next"); 78 base.add(next); 79 80 finish = new JButton("終了"); 81 //略 82 finish.addActionListener(this); 83 finish.setActionCommand("fin"); 84 base.add(finish); 85 86 logButton = new JButton("ログから"); 87 //略 88 logButton.addActionListener(this); 89 logButton.setActionCommand("log"); 90 base.add(logButton); 91 92 add(base); 93 94 } 95 96 public void actionPerformed(ActionEvent e){ 97 98 String com = e.getActionCommand(); 99 if(com.equals("t")){ 100 ans = "true"; 101 tButton.setForeground(Color.BLUE); 102 } else if(com.equals("f")){ 103 ans = "false"; 104 fButton.setForeground(Color.BLUE); 105 } else if(com.equals("next")) setAns(false); 106 else if(com.equals("fin")) setAns(true); 107 else if(com.equals("log")) addFromLog(); 108 } 109 110 private void setAns(boolean ending){ 111 112 fButton.setForeground(Color.BLACK); 113 tButton.setForeground(Color.BLACK); 114 String c = ","; 115 if(ending) c = ""; 116 mon.add(mondai.getText()); 117 try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(html, true), "UTF-8")))){ 118 119 out.println("\t\t" + ans + c); 120 if(ending){ 121 122 /*out.println()でHTMLの続きを書く*/ 123 124 int i = 0; 125 while(i < mon.size()){ 126 127 out.println("<div>"); 128 out.println(brTag(mon.get(i))); 129 out.println("</div>"); 130 i ++; 131 } 132 133 /*out.println()でHTMLの後ろを書く*/ 134 } 135 } catch(IOException e){} 136 try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("過去データ.otft", true), "UTF-8")))){ 137 138 out.println(mondai.getText()); 139 out.println("&ANS=" + ans); 140 } catch(IOException e){} 141 mondai.setText(""); 142 if(ending) System.exit(0); 143 } 144 145 private String brTag(String str){ 146 147 between bet = new between(); 148 String st = bet.Change(str, "\n", "<br>");//strの中の"\n"を全て"<br>"に変更する 149 return st; 150 } 151 152 private void addFromLog(){ 153 154 afl a = new afl(html); 155 a.setBounds(800,100,400,600); 156 a.setTitle("選ぶ"); 157 a.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); 158 a.setVisible(true); 159 } 160} 161 162class afl extends JFrame implements ActionListener{ 163 164 JPanel[] ap; 165 JLabel[] disp; 166 JButton[] addLog; 167 String[][] mon; 168 String[] ans; 169 File fl; 170 171 afl(File html){ 172 173 fl = html; 174 int i = 0; 175 int a = 0; 176 JPanel base = new JPanel(); 177 base.setLayout(new BoxLayout(base, BoxLayout.Y_AXIS)); 178 try{ 179 180 String fline = ""; 181 BufferedReader fr = new BufferedReader(new FileReader("過去データ.otft")); 182 while(true){ 183 184 fline = fr.readLine(); 185 if(fline == null) break; 186 else if(fline.startsWith("&ANS=")) a ++; 187 } 188 fr.close(); 189 ap = new JPanel[a]; 190 disp = new JLabel[a]; 191 addLog = new JButton[a]; //追加ボタン 192 mon = new String[a][10]; 193 ans = new String[a]; 194 195 a = 0; 196 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("過去データ.otft"), "UTF-8")); 197 String line = br.readLine(); 198 while(line != null){ 199 200 if(!line.startsWith("&ANS=")){ 201 202 mon[i][a] = line; 203 a ++; 204 } else{ 205 206 /*mon[][]をJPanelに入れ、「追加」ボタンを入れる。アクションコマンドに"" + i*/ 207 a = 0; 208 i ++; 209 } 210 line = br.readLine(); 211 } 212 } catch(IOException e){} 213 add(base); 214 } 215 216 public void actionPerformed(ActionEvent e){ 217 218 int com = new Integer(e.getActionCommand()); 219 try(PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fl, true), "UTF-8")))){ 220 221 out.println("\t\t" + ans[com] + ","); 222 } catch(IOException ex){} 223 new makeHTML().mon.add(Mondai(com, "<br>")); 224 } 225 226 private String Mondai(int i, String n){ 227 228 /*mon[][]を全て繋げる*/ 229 } 230}
回答1件
あなたの回答
tips
プレビュー