0

Java Swingで試験系デスクトップアプリを作っています。「show_class_question」フレームという名前のフレームがあります。このフレームは、質問とデータベースからの 4 つのオプションを表示するために使用されます。ユーザーが「次へ」ボタンをクリックするたびに、この同じフレーム(show_class_question)をそれ自体(自己参照)から質問の総数(たとえば20)まで何度も呼び出しています。

show_class_question --(ユーザーが次のボタンをクリック)--> show_class_question

ここで、値がデータベースから取得されるカウントダウン タイマーを追加したいと思います。データベースの int 列に分を保存しました。ユーザーが次のボタンをクリックするたびに、残り時間が他の値とともに同じフレーム (show_class の質問) に渡されます。

ユーザーが次のことを行わない限り、このプロセスは続行されます。

  1. すべての質問に答える
  2. または「試験終了」ボタンをクリック
  3. またはカウントダウンがゼロになったとき

上記のいずれかが発生した場合、「user_Result」フレームを呼び出したい

私の問題は、ユーザーが「次へ」ボタンをクリックするたびに、残り時間を正しく渡すことができないことです。ネットで取得したカウントダウンコードを何とか操作して次のフレームで残り時間を経過させたのですが、何回も「user_Result」フレームが呼び出されています。「show_class_question」の「次へ」ボタンを 7 回クリックしたとします。「user_Result」フレームは 7 回呼び出され、カウントダウンがゼロになる前でもあります。

以下に私のコードを投稿します。コードを修正するか、寛大になって、より適切で正しい方法を提供してください。

-ありがとうございました

public class Show_class_question extends javax.swing.JFrame {

private Timer timer;
String header_name;
String class_name;
String stream_name;
String u_id;
String mix_course_header;
int d=0;

public void end_exam()
{
 truncate_table();
        User_result ur = new User_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);
        ur.setVisible(true);
        this.dispose();
}
public void truncate_table()
 {
     try {
        Class.forName("com.mysql.jdbc.Driver");
         Connection con = DriverManager.getConnection(Connection_const.DB_URL,Connection_const.USER_NAME,Connection_const.PASSWORD);
         Statement st = con.createStatement();

         String sqla = "TRUNCATE TABLE asked_ques_table";
         st.executeUpdate(sqla);

    } catch (Exception e) {
    }

}


/*
 * Declaring publics strings for op1 2 3 4 and answer
 */
String ques = null;
String q_id = null;
String op1 = null;
String op2 = null;
String op3 = null;
String op4 = null;
String answer = null;

// declaring logical variables
int flag;
float score;
int no_of_quest_answered;
int no_of_correct_answers;

//Exam pattern variables
String difficulty_level;
 int total_no_of_ques;
 int time_limit;
 int points_awarded;
 int negative_marking; // 0 --> no, 1 --> yes
 float points_deducted;


 // countdown variables

long remaining;
long passed;
long limit;

/**
 * Creates new form Show_class_question
 */
public Show_class_question() {
    initComponents();
}

// our new constructor

public Show_class_question(String h_name, String pclass_name, String pstream_name, int pflag, String pu_id) {
    header_name = h_name;
    class_name = pclass_name;
    stream_name = pstream_name;
    u_id = pu_id;
    flag = pflag;
    initComponents();
}


//self refrencing constructor
public Show_class_question(int pflag, float pscore, int p_no_of_ques_ans, String pq_id, String pclass_name, String pstream_name, String namelb,  String pu_id, int pno_of_correct_ans,  long plimit,long ppassed)
{
    flag = pflag;
    score = pscore;
    no_of_quest_answered = p_no_of_ques_ans;
    q_id = pq_id;
    class_name = pclass_name;
    stream_name = pstream_name;
    header_name = namelb;
    u_id = pu_id;
    no_of_correct_answers = pno_of_correct_ans;
    //remaining = premaining;
   passed = ppassed;
    limit = plimit;
    System.out.println("rem inside cons= "+remaining);


    //x2 = px;

    System.out.println("d cls name= "+class_name);
    System.out.println("d strm name= "+stream_name);

    System.out.println("flag value at top  = "+flag);
    System.out.println("score value at top  = "+score);
    System.out.println("no_of_q_a value at top  = "+no_of_quest_answered);
    System.out.println("q_id value at top  = "+q_id);

    initComponents(); 
}


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
  limit=remaining;

    String user_answer;
    if (flag == 0) {

        score = 0;
        no_of_quest_answered = 0;
    }

    if (jRadioButton2.isSelected()) {
        user_answer = jRadioButton2.getText();
    } else if (jRadioButton3.isSelected()) {
        user_answer = jRadioButton3.getText();
    } else if (jRadioButton4.isSelected()) {
        user_answer = jRadioButton4.getText();
    } else if (jRadioButton5.isSelected()) {
        user_answer = jRadioButton5.getText();
    } else {
        user_answer = null;
    }


    //checking answer and answered or not

    if (user_answer != null) {

        no_of_quest_answered++;
       if(negative_marking==0)
        {
            if(user_answer.equals(answer))
            {
                score=score+(1*points_awarded);
                no_of_correct_answers++;
             }
        }
        else if(negative_marking==1)
        {
             if(user_answer.equals(answer))
            {
                 score=score+(1*points_awarded);
                 no_of_correct_answers++;

            }
             else
             {
                 score= (score-points_deducted);

             }
        } 
    }

    flag++;
    System.out.println("flag value at bottom  = " + flag);

    if (flag < total_no_of_ques) //starting frm 0
    {

        Show_class_question s_c_q = new Show_class_question(flag, score, no_of_quest_answered, q_id, class_name, stream_name,  header_name, u_id,no_of_correct_answers,  limit, passed);
        s_c_q.setVisible(true);
        this.dispose();
    } else {
        truncate_table();
        User_result ur = new User_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);
        ur.setVisible(true);
        this.dispose();
    }
}                                        

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:

    // user ended exam in between

    int response = JOptionPane.showConfirmDialog(rootPane, "You will not be able to answer any more questions. Continue?");
    if(response==0)
    {
     truncate_table();
    User_result ur = new User_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking,points_awarded,points_deducted,difficulty_level,no_of_correct_answers,total_no_of_ques);
    ur.setVisible(true);
    this.dispose();
    }
}                                        

private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    // TODO add your handling code here:

    try{
         Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection(Connection_const.DB_URL,Connection_const.USER_NAME, Connection_const.PASSWORD);
        Statement st = con.createStatement();

        String sql6  = "SELECT pattern_id FROM exam_pattern_in_use WHERE pid = '1' ";
         ResultSet rs6 = st.executeQuery(sql6);
         String pattern_id = null;
         while(rs6.next())
         {
              pattern_id = rs6.getString("pattern_id");
         }
           String sq5 = "SELECT * FROM exam_pattern WHERE pattern_id='"+pattern_id+"' ";
         ResultSet rs5 = st.executeQuery(sq5);
         String db_diff_lev = null;
         String db_time_limit = null;
         String db_tot_ques = null;
         String db_points_award = null;
         String db_neg_marking = null;
         String db_points_ded = null;

         while(rs5.next())
         {
             db_diff_lev = rs5.getString("difficulty_level");
             db_tot_ques = rs5.getString("tot_no_of_ques");
             db_time_limit = rs5.getString("time_limit");
             db_points_award = rs5.getString("points_awarded_for_correct_ans");
             db_neg_marking = rs5.getString("negative_marking");
             db_points_ded = rs5.getString("points_deduced_for_wrong_ans");
         }
         int my_time_limit = Integer.parseInt(db_time_limit);
         System.out.println("my time lim = "+my_time_limit);


    ////------------------------->timer code start here
    final long current = System.currentTimeMillis();

    try {
        if(flag==0)
            limit = my_time_limit*1000; // X seconds

             timer = new Timer(1000, new ActionListener() {
             public void actionPerformed(ActionEvent event) {
             long time = System.currentTimeMillis();
             passed = time - current;
             remaining = limit - passed;
             System.out.println("rem time ="+remaining);

          if(remaining >= 1) {
              long seconds = remaining/1000;
            long minutes = seconds/60;
            long hours = minutes/60;
            jLabel13.setText(String.format("%02d:%02d:%02d", hours, minutes, seconds%60));


          } else {

           timer.stop();
           timer.setRepeats(false);
           truncate_table();
           setVisible(false);
           dispose();
        User_result ur = new User_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);
        ur.setVisible(true);
           d=1;
        }

       }

        });

      timer.start();

    } catch(NumberFormatException nfe) {
      // debug/report here

      nfe.printStackTrace();
    }

    ////------------------------->timer code end here


    setTitle("Question: "+(flag+1));
    headernmlb.setText(header_name);
    class_name_lb.setText(class_name);
    jLabel10.setText(stream_name);
    mix_course_header = class_name+"-"+stream_name;




    if(flag==0)
    {
        jButton3.setVisible(true);
    }
    else
    {
        jButton3.setVisible(false);
    }

          /*  if(d==1)
                  {
                        truncate_table();
                        User_result ur = new User_result(score, no_of_quest_answered, header_name, mix_course_header,  u_id, negative_marking, points_awarded, points_deducted, difficulty_level, no_of_correct_answers, total_no_of_ques);
                        ur.setVisible(true);
                        this.dispose();
                  }
    */

         difficulty_level = db_diff_lev;
         total_no_of_ques = Integer.parseInt(db_tot_ques);
         points_awarded = Integer.parseInt(db_points_award);
         negative_marking =  Integer.parseInt(db_neg_marking);
         points_deducted =  Float.parseFloat(db_points_ded);
         System.out.println("fractional points ded = "+points_deducted);


          String sql = "SELECT s_id FROM school_stream_level WHERE course_name = '"+class_name+"' and stream_name = '"+stream_name+"' ";
          // s_id in class_question_table = c_id in course_level
          // dono 1 hi cheez represent kar rahe he
          // dono me FK relationship he


           ResultSet rs = st.executeQuery(sql);

           String s_id=null;

         while(rs.next())
         {
              s_id = rs.getString("s_id");
         }

          String sql2;

         if(flag==0)
         {
             sql2 = "SELECT * FROM class_ques_table WHERE s_id = '"+s_id+"' and  difficulty_level = '"+difficulty_level+"' ORDER BY RAND() LIMIT 1";

         }
         else
         {
                  sql2 = "SELECT * FROM class_ques_table WHERE s_id = '"+s_id+"' and  difficulty_level = '"+difficulty_level+"' and q_id NOT IN(SELECT q_id FROM asked_ques_table) ORDER BY RAND() LIMIT 1";
         }


          ResultSet rs2 =st.executeQuery(sql2);
          System.out.println("under rs2");

              while(rs2.next())
              {
                   ques = rs2.getString("question");
                   q_id = rs2.getString("q_id");
                   op1 = rs2.getString("op1");
                   op2 = rs2.getString("op2");
                   op3 = rs2.getString("op3");
                   op4 = rs2.getString("op4");
                   answer = rs2.getString("answer");
                   System.out.println("q_id db = "+q_id);
              }

                  jLabel7.setText(""+(flag+1));
                  queslb.setText(ques);
                  jRadioButton2.setText(op1);
                  jRadioButton3.setText(op2);
                  jRadioButton4.setText(op3);
                  jRadioButton5.setText(op4);


                  String sql00 = "INSERT INTO asked_ques_table VALUES('0', '"+q_id+"')";
                  st.executeUpdate(sql00);



    }
    catch(Exception e){

    }
}                                 

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    if (flag == 0) {
        //   jButton3.setVisible(true);
        School_stream_selection s_s_s = new School_stream_selection(header_name, class_name, u_id);
        s_s_s.setVisible(true);
        dispose();

    }

}                                        

private void jLabel1FocusGained(java.awt.event.FocusEvent evt) {                                    
    // TODO add your handling code here:
}                                   

...and few more lines...
4

0 に答える 0