我正在使用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 :(得分:4)
我希望每张卡的计时器仅在显示该卡时启动。
您可以使用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
的每个面板。
我希望该卡自动更改为下一张卡
CardLayout
支持&#34;下一张卡&#34;功能,所以你只需在Timer达到0时调用适当的方法。