倒计时在SWING结束时如何调用另一帧

时间:2013-05-03 04:20:05

标签: java swing user-interface timer countdown

我正在Java Swing中制作一个考试系统桌面应用程序。 我有一个名为'show_class_question'框架的框架。 此框架用于显示问题和数据库中的四个选项。 每当用户点击“下一步”按钮时,我会一次又一次地调用同一帧(show_class_question)(自我引用)直到问题总数(比如20)。

show_class_question - (用户点击其上的下一个按钮) - >的 show_class_question

现在我想添加倒数计时器,其值将从数据库中检索。我在数据库中的int列中存储了分钟数。 每当用户点击下一个按钮时,剩余时间将与其他值一起传递到同一帧(show_class问题)。

除非用户

,否则此过程将继续
  1. 回答所有问题
  2. 或点击“结束考试”按钮
  3. 或当倒计时变为零时
  4. 当上述任何一种情况发生时,我想调用'user_Result'框架

    我的问题是,当用户点击“下一步”按钮时,我无法以正确的方式传递剩余时间。 虽然我以某种方式设法操纵我从互联网上获得的倒计时代码并在下一帧传递剩余时间,但'user_Result'帧被多次调用。假设我点击'show_class_question'上的'Next'按钮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...
    

0 个答案:

没有答案