CardLayout中每张新卡的倒数计时器

时间:2015-03-30 17:55:01

标签: java swing timer countdowntimer cardlayout

我正在使用CardLayout来显示一系列问题。对于每个问题,标签上都会显示一个计时器。 我从主要课程中传递了每个问题的问题和时间限制。

//Reference - SynforgeTutorials
public class Quiz extends JFrame{
    JPanel p=new JPanel();
    CardLayout cards=new CardLayout();
    int numQs;


    int cardnumber;

    CL1 questions[];



    public static void main(String[] args) {

        String[] messages = {"What is area of a circle?","When does spring start?","When is the next leap year?","How many days in a week?","What causes seasons?"};
        int[] mins = {1,2,0,1,1};
        int[] secs = {30,0,30,0,0};
        new Quiz(messages,mins,secs);
    } 

    public Quiz(String messages[],int mins[],int secs[]){


        questions = new CL1[messages.length];

        for(int i=0;i<questions.length;i++)
        {
        questions[i] = new CL1(messages[i],mins[i],secs[i],this);

        }

        p.setLayout(cards);

        numQs=questions.length;
        for(int i=0;i<numQs;i++){
            p.add(questions[i],"q"+i);
        }

        cardnumber = 0;

        cards.show(p,"q"+ cardnumber);


        add(p);
        setVisible(true);


    }

    public void OK(){
        if(cardnumber==(numQs-1)){

            this.dispose();

        }
        else{

            cardnumber = cardnumber + 1;
            cards.show(p,"q"+ cardnumber);
        }
    }

}

这是构建卡片的类。它有3个面板 - 显示问题的顶部面板,标签上显示分钟和秒的中心面板以及暂停和恢复按钮,以及存在OK按钮的底部面板。点击&#39;确定&#39;按钮,显示下一张卡片。

public class CL1 extends JPanel implements ActionListener {

    int correctAns;

    Quiz quiz;
    int selected;
    boolean used;

    private Timer myTimer1;
    public long initialTimeInSecs;
    public long elapsedTime;
    public long convertToSecs;
    public String minutes;
    public String seconds;
    public String clockTimeString; 
    public static final int ONE_SEC = 1000;
    Font myClockFont = new Font("Serif", Font.PLAIN, 20);




    //Message
    JPanel qPanel=new JPanel();

    //Timer
    JPanel tPanel=new JPanel();
    JLabel timeLbl = new JLabel("New label");
    JButton btnPause=new JButton("Pause");
    JButton btnResume=new JButton("Resume");


    //bottom
    JPanel botPanel=new JPanel();
    JButton OK=new JButton("OK");



    public CL1(String q, int userMinutes, int userSeconds, Quiz quiz){

        this.quiz=quiz;

        setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));


        //Message
        qPanel.add(new JLabel(q));
        add(qPanel);


        //Timer
        convertToSecs = (userMinutes * 60) + userSeconds;

        initialTimeInSecs = convertToSecs;
        elapsedTime = initialTimeInSecs;

        seconds = Integer.toString((int)(elapsedTime % 60));
        minutes = Integer.toString((int)((elapsedTime % 3600) / 60));
        if (seconds.length() < 2)
            seconds = "0" + seconds;
        if (minutes.length() < 2)
            minutes = "0" + minutes;
        clockTimeString = (minutes+":"+seconds).toString();

        timeLbl.setText(clockTimeString);
        timeLbl.setFont(myClockFont);
        timeLbl.setBorder(raisedbevel);

        myTimer1 = new Timer(ONE_SEC,new ActionListener() {
            public void actionPerformed(ActionEvent evt) {

                initialTimeInSecs = initialTimeInSecs - 1;

                elapsedTime = initialTimeInSecs;

                String seconds = Integer.toString((int)(elapsedTime % 60));
                String minutes = Integer.toString((int)((elapsedTime % 3600) / 60));


                if (seconds.length() < 2)
                    seconds = "0" + seconds;

                if (minutes.length() < 2)
                    minutes = "0" + minutes;

                clockTimeString = (minutes+":"+seconds).toString();

                timeLbl.setText(clockTimeString);

                if(clockTimeString.equals("00:00"))
                {
                    myTimer1.stop();

                }
                } 

          });

        myTimer1.start(); 



        btnPause.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {   
                PauseBtnActionPerformed(evt);
            }
        });



        btnResume.addActionListener(new java.awt.event.ActionListener() 
        {
            public void actionPerformed(java.awt.event.ActionEvent evt) 
            {   
                ResumeBtnActionPerformed(evt);
            }
        });

        tPanel.add(timeLbl);
        tPanel.add(btnPause);
        tPanel.add(btnResume);

        add(tPanel);


        //bottom

        OK.addActionListener(this);

        botPanel.add(OK);

        add(botPanel);
    }

    public void actionPerformed(ActionEvent e){
        Object src=e.getSource();
        //OK button
        if(src.equals(OK)){

            quiz.OK();}

    }

    public void PauseBtnActionPerformed(java.awt.event.ActionEvent evt)
    {
        myTimer1.stop();
    }

    public void ResumeBtnActionPerformed(java.awt.event.ActionEvent evt)
    {
        myTimer1.start();
    }
}

计时器似乎不会刷新每个新标签。它从第一个标签的开头开始滴答作响。但是,对于每个其他标签,它会从当前卡的时间限制中减去从开始经过的时间而不是该特定问题,并显示该时间限制。

例如,对于问题1,时间限制为1.30         对于问题2,时间限制为2.00         对于问题3,时间限制为3.00

对话框首先显示第一个标签,然后从1.30开始滴答。假设我在这个标签上停留了10秒钟,然后再按“确定”。

然后显示第二个标签,计时器从1.50(2.00-0.10,而不是2.00)开始滴答。 假设我在这个问题上保持25秒,然后再按“确定”。

然后显示第三个标签,计时器开始从2.25(3.00-(0.10 + 0.25)开始,而不是3.00)。

即使我按下暂停按钮,计时器似乎也不会停止计算。当我按下继续按钮时,计时器会考虑暂停时间,并显示时间。

关于这个计时器出了什么问题的任何想法?

0 个答案:

没有答案