CardLayout:如何让事件仅在显示特定卡片时开始?

CardLayout : How to get an event to start only when a particular card is displayed?

我正在使用 CardLayout 来显示一系列问题。对于每个问题,标签上都会显示一个计时器。我从主class.

传递题目和每个题目的时间限制
//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);
        }
    }

}

这是构成卡片的class。它有 3 个面板 - 显示问题的顶部面板,在标签上显示分钟和秒以及暂停和恢复按钮的中心面板,以及存在 OK 按钮的底部面板。单击 'OK' 按钮后,将显示下一张卡片。

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 开始向下滚动。假设我在按 'OK'.

之前在此标签上停留了 10 秒

然后显示第二个标签,计时器从 1.50(2.00-0.10,而不是 2.00)开始计时。假设我在按 'OK'.

之前在这个问题上停留了 25 秒

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

我想做两件事:

  1. 我希望每张卡片的计时器仅在显示该卡片时启动。
  2. 我希望卡片在时间限制到达后自动更改为下一张卡片00:00。

任何建议都会有所帮助。提前致谢!

I want the timer for each card to start only when that card is shown.

您可以使用 HierarchyListener 来监听每张卡片可见性的变化:

@Override
public void hierarchyChanged(HierarchyEvent e)
{
    JComponent component = (JComponent)e.getSource();

    if ((HierarchyEvent.SHOWING_CHANGED & e.getChangeFlags()) != 0
    &&  component.isShowing())
    {
        // start the Timer
    }
}

因此您需要将 HierarchyListener 添加到您添加到 CardLayout 的每个面板。

I want the card to automatically change to the next card

CardLayout 支持 "next card" 功能,因此您只需在计时器达到 0 时调用适当的方法即可。