Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在SWING中倒数结束时如何调用另一帧_Java_Swing_User Interface_Timer_Countdown - Fatal编程技术网

Java 在SWING中倒数结束时如何调用另一帧

Java 在SWING中倒数结束时如何调用另一帧,java,swing,user-interface,timer,countdown,Java,Swing,User Interface,Timer,Countdown,我正在用JavaSwing制作一个考试系统桌面应用程序。 我有一个名为“show\u class\u question”的框架。 此框架用于显示数据库中的问题和四个选项。 每当用户单击“下一步”按钮时,我都会一次又一次地调用同一个框架(show_class_question)(自引用)直到问题总数(比如20个) show\u class\u question(用户单击其上的下一步按钮)-->show\u class\u question 现在我想添加一个倒计时计时器,它的值将从数据库中检索。我在

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

show\u class\u question(用户单击其上的下一步按钮)-->show\u class\u question

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

除非用户:

  • 回答所有问题
  • 或点击“结束考试”按钮
  • 或者当倒计时变为零时
  • 当上述任何情况发生时,我想调用“user\u Result”框架

    我的问题是,每当用户单击“下一步”按钮时,我无法以正确的方式度过剩余时间。 虽然我设法操纵了从互联网上获得的倒计时代码,并在下一帧上度过了剩余的时间,但“user_Result”帧被调用了好几次。假设我在“show_class_question”上点击了7次“Next”按钮,“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...
    
    public class Show\u class\u问题扩展了javax.swing.JFrame{
    私人定时器;
    字符串头名称;
    字符串类名称;
    字符串流名称;
    字符串u_id;
    字符串混合\课程\标题;
    int d=0;
    公开考试
    {
    截断表();
    用户结果ur=新用户结果(分数、未回答的任务、标题名称、混合课程标题、id、负面评分、得分、扣分、难度等级、未回答正确答案、问题总数);
    ur.setVisible(真);
    这个。dispose();
    }
    公共void truncate_表()
    {
    试一试{
    Class.forName(“com.mysql.jdbc.Driver”);
    Connection con=DriverManager.getConnection(连接常数DB\u URL、连接常数用户名、连接常数密码);
    语句st=con.createStatement();
    String sqla=“TRUNCATE TABLE ask\u ques\u TABLE”;
    st.executeUpdate(sqla);
    }捕获(例外e){
    }
    }
    /*
    *为op1 2 3 4声明publics字符串并回答
    */
    字符串ques=null;
    字符串q_id=null;
    字符串op1=null;
    字符串op2=null;
    字符串op3=null;
    字符串op4=null;
    字符串应答=null;
    //声明逻辑变量
    int标志;
    浮点数;
    回答的任务的整数否;
    int无正确答案;
    //考试模式变量
    串级难度;
    整数总数(数量);
    整数时间限制;
    积分;;
    int负标记;//0-->否,1-->是
    扣除浮点数;
    //倒计时变量
    长期存在;
    早已过去;
    长限;
    /**
    *创建新表单Show\u class\u问题
    */
    公开表演课问题(){
    初始化组件();
    }
    //我们的新构造
    公共显示类问题(字符串h_名称、字符串pclass_名称、字符串pstream_名称、int pflag、字符串pu_id){
    标题名称=h\U名称;
    类别名称=类别名称;
    流名称=流名称;
    u_id=pu_id;
    flag=pflag;
    初始化组件();
    }
    //自引用构造函数
    公共显示类问题(整数pflag、浮点pscore、整数pu no_of_ques_ans、字符串pq_id、字符串pclass_name、字符串pstream_name、字符串namelb、字符串pu id、整数pno_of_correct_ans、长plimit、长ppassed)
    {
    flag=pflag;
    分数=pscore;
    没有任务的回答=没有问题的回答;
    q_id=pq_id;
    类别名称=类别名称;
    流名称=流名称;
    标题_name=namelb;
    u_id=pu_id;
    无正确答案=无正确答案;
    //剩余=预培训;
    通过=通过;
    极限=plimit;
    System.out.println(“rem-inside-cons=“+剩余”);
    //x2=px;
    System.out.println(“d cls name=“+class_name”);
    System.out.println(“d strm name=“+stream\u name”);
    System.out.println(“顶部的标志值=”+标志);
    System.out.println(“顶部的分数值=”+分数);
    System.out.println(“顶部数值的否=”+回答的任务的否);
    System.out.println(“顶部的q_id值=”+q_id);
    初始化组件();
    }
    私有void jButton1ActionPerformed(java.awt.event.ActionEvent evt){
    //TODO在此处添加您的处理代码:
    限制=剩余;
    字符串用户应答;
    如果(标志==0){
    得分=0;
    没有回答的任务=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();
    }否则{
    user_answer=null;
    }
    //检查答案与否
    if(用户回答!=null){
    没有任务回答++;
    如果(负_标记==0)
    {
    if(用户回答等于(回答))
    {
    分数=分数+(1*分);
    没有正确答案++;
    }
    }
    else if(负_标记==1)
    {
    if(用户回答等于(回答))
    {
    分数=分数+(1*分);
    没有正确答案++;
    }
    其他的
    {
    分数=(扣减的分数);
    }
    } 
    }
    flag++;
    System.out.println(“底部的标志值=”+标志);
    如果(标志<总数量)//开始frm 0