###前提・実現したいこと
javaにて簡単なクイズゲームのようなものを作っています。
完成形としては、ランダムに問題を抽選、
jLabelに問題数(何問目なのか)、問題文、選択肢を表示し、
jBttonで回答するようなものになる予定です
###発生している問題・エラーメッセージ
普通にjlabelにsetTextで文字列を設定しようとしたのですが、
何も変化がありません。
実行できないというわけではないので、エラーメッセージ等はありません。
###該当のソースコード
java
1/* 2 * To change this license header, choose License Headers in Project Properties. 3 * To change this template file, choose Tools | Templates 4 * and open the template in the editor. 5 */ 6package quizgame; 7 8import java.util.logging.Level; 9import java.util.logging.Logger; 10 11/** 12 * 13 * @author 14 */ 15public class QG_JFrame extends javax.swing.JFrame { 16 17 /** 18 * Creates new form QG_JFrame 19 */ 20 public QG_JFrame() { 21 initComponents(); 22 } 23 24 /** 25 * This method is called from within the constructor to initialize the form. 26 * WARNING: Do NOT modify this code. The content of this method is always 27 * regenerated by the Form Editor. 28 */ 29 @SuppressWarnings("unchecked") 30 31**IDEによって自動生成されたコード 32文字数が入りきらないので削除 33追記する方法があればよいのですが** 34 35 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { 36 System.out.println("mode="+mode); 37 if (mode == 1) { //回答可能なモードなら 38 System.out.println("button1"); 39 try { 40 Decision(0); //番号0で判定に回す 41 } catch (InterruptedException ex) { 42 Logger.getLogger(QG_JFrame.class.getName()).log(Level.SEVERE, null, ex); 43 } 44 } else if (mode == 2) { //結果表示モードなら 45 award(); //続行判定 46 } 47 } 48 49 private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { 50 System.out.println("mode="+mode); 51 if (mode == 1) { //回答可能なモードなら 52 System.out.println("button2"); 53 try { 54 Decision(1); //番号1で判定に回す 55 } catch (InterruptedException ex) { 56 Logger.getLogger(QG_JFrame.class.getName()).log(Level.SEVERE, null, ex); 57 } 58 } else if (mode == 2) { //結果表示モードなら 59 award(); //続行判定 60 } 61 } 62 63 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { 64 System.out.println("mode="+mode); 65 if (mode == 1) { //回答可能なモードなら 66 System.out.println("button3"); 67 try { 68 Decision(2); //番号2で判定に回す 69 } catch (InterruptedException ex) { 70 Logger.getLogger(QG_JFrame.class.getName()).log(Level.SEVERE, null, ex); 71 } 72 } else if (mode == 2) { //結果表示モードなら 73 award(); //続行判定 74 } 75 } 76 77 /** 78 * @param args the command line arguments 79 */ 80 public void main() { 81 /* Set the Nimbus look and feel */ 82 83**上と同じ理由で削除** 84 85 /* Create and display the form */ 86 java.awt.EventQueue.invokeLater(new Runnable() { 87 public void run() { 88 new QG_JFrame().setVisible(true); 89 } 90 }); 91 try { 92 proposing(); 93 } catch (InterruptedException ex) { 94 Logger.getLogger(QG_JFrame.class.getName()).log(Level.SEVERE, null, ex); 95 } 96 } 97 98 public void proposing() throws InterruptedException { 99 System.out.println("QG_JFrame/proposing");//デバッグ用メッセージ 100 mode = 0; //操作禁止モードに変更 101 102 if (QCount == 0) { 103 setQuestion(); //問題を設定 104 } 105 106 QCount++; 107 System.out.println("settext");//デバッグ用メッセージ 108 counter.setText(String.valueOf(QCount)); //出題中の問題を表示 109 110 while (true) { //出題する問題を決めるため無限ループ 111 System.out.println("roop");//デバッグ用メッセージ 112 rnd = (int) (Math.random() * 問題数);//出題する問題を抽選 113 Thread.sleep(10); 114 if (!question[rnd].getRead()) { // 問題が出題済みでないなら 115 break; //問題決定でループ脱出 116 } 117 } 118 119 String k = question[rnd].getQuestion(); //選ばれた問題の問題文を取得 120 System.out.println(k);//デバッグ用メッセージ 121 main.setText(k); //問題文を表示 122 String s[] = question[rnd].getChoices(); //選択肢(String[])を取得 123 System.out.println(s[0] + s[1] + s[2]);//デバッグ用メッセージ 124 S1.setText(s[0]); //********************// 125 S2.setText(s[1]); //選択肢を表示 126 S3.setText(s[2]); //********************// 127 System.out.println("settext");//デバッグ用メッセージ 128 mode = 1; //回答可能なモードに変更 129 System.out.println("回答開始");//デバッグ用メッセージ 130 } 131 132 private int rnd = 0; 133 private int mode; //モードを管理する変数 134 private int QCount = 0; //出した問題の数を管理する変数 135 private final int 問題数 = 3; 136 private Question question[]=new Question[問題数]; 137 138 void setQuestion() throws InterruptedException { 139 for(int i=0;i<question.length;i++){ 140 question[i]=new Question();//question(Question[])をすべてnew 141 Thread.sleep(10); 142 } 143 System.out.println("QG/setQuestion");//デバッグ用メッセージ 144 /*問題追加ひな形 145 choices[0]="選択肢正解"; 146 choices[1]="選択肢不正解"; 147 choices[2]="選択肢不正解"; 148 question[数字(順番に並ぶように)].setQuestion("問題文",A);*/ 149 150 String choices[]=new String[問題数]; 151 152 choices[0]="A"; 153 choices[1]="B"; 154 choices[2]="B"; 155 question[0].setQuestion("A", choices); 156 157 choices[0]="B"; 158 choices[1]="C"; 159 choices[2]="C"; 160 question[1].setQuestion("B", choices); 161 162 choices[0]="C"; 163 choices[1]="D"; 164 choices[2]="D"; 165 question[2].setQuestion("C", choices); 166 return; 167 } 168 169 void Decision(int n) throws InterruptedException { 170 System.out.println("QG_JFrame/Decision"); 171 mode = 0; //操作禁止モードに変更 172 173 if (question[rnd].getCorrect() == n) { //引数とgetCorrectで正解かを判別 174 main.setText("<html><br>正解<html/>"); // 正解時のメッセージを表示 175 } else { 176 main.setText("<html><br>はずれ<html/>");//はずれ時のメッセージを表示 177 } 178 179 mode = 2; //回答表示モードに移行 180 } 181 182 void award() { 183 System.out.println("QG_JFrame/award"); 184 if (問題数 == QCount) { 185 main.setText("<html><br>問題終了<html/>"); 186 } else { 187 try { 188 proposing(); 189 } catch (InterruptedException ex) { 190 Logger.getLogger(QG_JFrame.class.getName()).log(Level.SEVERE, null, ex); 191 } 192 } 193 } 194 195 // Variables declaration - do not modify 196 private javax.swing.JLabel S1; 197 private javax.swing.JLabel S2; 198 private javax.swing.JLabel S3; 199 private static javax.swing.JLabel counter; 200 private javax.swing.JButton jButton1; 201 private javax.swing.JButton jButton2; 202 private javax.swing.JButton jButton3; 203 private javax.swing.JLabel jLabel1; 204 private javax.swing.JLabel jLabel10; 205 private javax.swing.JLabel jLabel3; 206 private javax.swing.JLabel jLabel8; 207 private javax.swing.JLabel jLabel9; 208 private static javax.swing.JLabel main; 209 // End of variables declaration 210}
上のclassは別のclassから呼び出す仕組みですが、呼び出すだけのclassなため省略
java
1上のコードに出てきたQuestionクラスなのですが、 2文字数制限のため削除 3追記する方法があればよいのですが
長くなってしまい、申し訳ありません。
###試したこと
sleep関係の不具合は前に経験があったので、
Thread.sleep();を試してみました。
その結果、jLabelの
mainとcounterは正常に表示が更新されるようになりました。
ですが、S1,S2,S3(それぞれ選択肢の表示に使う)は
何も変化がありませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
java jdk 1.8.0_91
IDE netbeans 8.1
初歩的な質問かとは思いますが、回答いただけると幸いです。
(みなさん文字数制限どうしていらっしゃるのでしょうか…)
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。