Java Swing计时器未以正确的方式递减且未在正确的时间启动 [英] Java swing timer not decrementing the right way and not starting at the correct hour

查看:48
本文介绍了Java Swing计时器未以正确的方式递减且未在正确的时间启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个团队的成员,该团队正在为学生创建测试以使其适应某种格式,然后再参加认证测试.测试时间为四个小时,当我尝试实现计时器时,我看到了异常的模式.

我希望计时器从4小时开始(采用HH:MM:SS格式),然后倒计时到全零,不幸的是,它从11:00:00开始,倒计时到07:00:00.

下一个问题是必须在两个不同的页面之间显示计时器.考试和复习页面的实际参加情况.当我在页面之间来回切换时,计数器开始按页面之间单击次数的倍数递减.

下面是我创建的计时器类,进行测验并提交评论.该代码不是完美的,需要工作,但是我需要一个GUI模拟来呈现.

任何帮助将不胜感激.谢谢你.

参加测验

 包edu.kings.pexam.student;导入java.awt.BorderLayout;导入java.awt.Color;导入java.awt.Dimension;导入java.awt.FlowLayout;导入java.awt.Font;导入java.awt.GridLayout;导入java.awt.event.ActionEvent;导入java.awt.event.ActionListener;导入javax.swing.JButton;导入javax.swing.JFrame;导入javax.swing.JLabel;导入javax.swing.JPanel;导入javax.swing.JRadioButton;导入javax.swing.JTextField;公共类TakeQuiz扩展JFrame实现ActionListener {私有静态最终长serialVersionUID = 1L;私人JButton提交;私人JButton表演;静态QuizTimer计时器;/*** @参数args*/公共静态void main(String [] args){TakeQuiz窗口=新的TakeQuiz();window.setVisible(true);}公共TakeQuiz(){setExtendedState(JFrame.MAXIMIZED_BOTH);JPanel upperPanel =新的JPanel(新的GridLayout(2,8));upperPanel.setPreferredSize(new Dimension(WIDTH,100));upperPanel.setBackground(Color.lightGray);this.add(upperPanel,BorderLayout.NORTH);JPanel lowerPanel =新的JPanel(new FlowLayout(FlowLayout.LEFT));lowerPanel.setBackground(Color.white);this.add(lowerPanel,BorderLayout.CENTER);Font font = new Font("Dialog",Font.PLAIN,17);字体buttonFont =新的Font("Dialog",Font.PLAIN,13);字体textButtonFont = new Font("Dialog",Font.PLAIN + Font.BOLD,13);字体SubmitButtonFont =新的Font("Dialog",Font.PLAIN + Font.BOLD,15);//将问题按钮添加到上面板JPanel问题面板=新的JPanel(新的FlowLayout(FlowLayout.LEFT));questionPanel.setBackground(Color.lightGray);JButton firstQuestion =新的JButton(<<");firstQuestion.setFont(buttonFont);questionPanel.add(firstQuestion);//空格以帮助按钮与文本对齐JPanel spacer1 =新的JPanel();spacer1.setBackground(Color.lightGray);questionPanel.add(spacer1);JButton perviousQuestion =新的JButton(<");perviousQuestion.setFont(buttonFont);questionPanel.add(perviousQuestion);//空格以帮助按钮与文本对齐JPanel spacer2 =新的JPanel();spacer2.setBackground(Color.lightGray);questionPanel.add(spacer2);JButton nextQuestion =新的JButton(>");nextQuestion.setFont(buttonFont);questionPanel.add(nextQuestion);//空格以帮助按钮与文本对齐JPanel spacer3 =新的JPanel();spacer3.setBackground(Color.lightGray);questionPanel.add(spacer3);JButton lastQuestion =新的JButton(>>");lastQuestion.setFont(buttonFont);questionPanel.add(lastQuestion);upperPanel.add(questionPanel);//将goto按钮添加到上面板JPanel goToPanel =新的JPanel(新的FlowLayout(FlowLayout.CENTER));goToPanel.setBackground(Color.lightGray);JButton goTo = new JButton("Go To");goTo.setFont(textButtonFont);goToPanel.add(goTo);upperPanel.add(goToPanel);//将标志按钮添加到上面板JPanel flagPanel =新的JPanel(新的FlowLayout(FlowLayout.LEFT));flagPanel.setBackground(Color.lightGray);JButton flagP =新的JButton(< Flag");flagP.setFont(textButtonFont);flagPanel.add(flagP);JButton标志=新的JButton("Flag");flag.setFont(textButtonFont);flagPanel.add(flag);JButton标志N =新的JButton("Flag>");flagN.setFont(textButtonFont);flagPanel.add(flagN);upperPanel.add(flagPanel);//将帮助和隐藏/显示计时器按钮添加到上面板JPanel timerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));timerPanel.setBackground(Color.lightGray);JButton帮助=新的JButton("H​​elp");help.setFont(textButtonFont);timerPanel.add(help);show = new JButton("Show/Hide Timer");show.setFont(textButtonFont);show.addActionListener(this);timerPanel.add(show);upperPanel.add(timerPanel);//添加空间面板JPanel spacePanel1 =新的JPanel();JPanel spacePanel2 =新的JPanel();spacePanel1.setBackground(Color.lightGray);spacePanel2.setBackground(Color.lightGray);upperPanel.add(spacePanel1);upperPanel.add(spacePanel2);//将提交按钮添加到上面板JPanel SubmitPanel =新的JPanel(新的FlowLayout(FlowLayout.RIGHT));SubmitPanel.setBackground(Color.lightGray);提交=新的JButton(提交测验");Submit.setFont(submitButtonFont);Submit.addActionListener(this);SubmitPanel.add(submit);upperPanel.add(submitPanel);//将问题按钮文本添加到上面板JPanel questionText =新的JPanel(new FlowLayout(FlowLayout.LEFT));questionText.setBackground(Color.lightGray);JLabel label2 =新的JLabel("html< center> First< br>/br< Question</center>//html"");label2.setFont(textButtonFont);questionText.add(label2);JLabel label4 = new JLabel(< html>< center> Perivous</br> Question</center</html>");label4.setFont(textButtonFont);questionText.add(label4);JLabel label6 = new JLabel(< html>< center> Next< br></br>问题</center></html>");label6.setFont(textButtonFont);questionText.add(label6);JLabel label8 =新的JLabel("html< center> Last< br>/br> Question//center</html>");label8.setFont(textButtonFont);questionText.add(label8);upperPanel.add(questionText);//添加文本框以转到按钮JPanel textGoTo = new JPanel(new FlowLayout(FlowLayout.CENTER));textGoTo.setBackground(Color.lightGray);JPanel upper10 =新的JPanel();upper10.setBackground(Color.lightGray);JTextField goToText = new JTextField("1",2);JLabel标签10 =新的JLabel("/25");label10.setFont(font);upper10.add(goToText,BorderLayout.CENTER);upper10.add(label10,BorderLayout.CENTER);textGoTo.add(upper10);upperPanel.add(textGoTo);//在上面的面板上添加垫片JPanel spacePanel3 =新的JPanel();spacePanel3.setBackground(Color.lightGray);upperPanel.add(spacePanel3);//将计时器添加到上面板JPanel timePanel =新的JPanel();timePanel.setBackground(Color.lightGray);计时器=新的QuizTimer();timer.start();JPanel upper20 =新的JPanel();upper20.setBackground(Color.lightGray);upper20.add(timer.getTimeLabel(),BorderLayout.CENTER);timePanel.add(upper20);upperPanel.add(timePanel);//再添加两个空间面板JPanel spacePanel4 =新的JPanel();JPanel spacePanel5 =新的JPanel();spacePanel4.setBackground(Color.lightGray);spacePanel5.setBackground(Color.lightGray);upperPanel.add(spacePanel4);upperPanel.add(spacePanel5);//将问题添加到下面板JPanel lower1 =新的JPanel(new GridLayout(4,1));lower1.setBackground(Color.white);JLabel问题=新的JLabel("html"> 16岁游泳者的父母与运动教练联系,以寻求有关运动员赛前饮食的营养建议.运动教练应分享什么建议和父母一起为理想的餐前饭做饭?</html>);Question.setFont(new Font("Dialog",Font.PLAIN + Font.BOLD,18));JPanel的答案=新的JPanel(新的GridLayout(6,1));answer.setBackground(Color.white);JLabel类型= new JLabel(选择所有适用的项目.");type.setFont(new Font("Dialog",Font.PLAIN + Font.BOLD + Font.ITALIC,20));;JPanel答案A = new JPanel(new FlowLayout(FlowLayout.LEFT));answerA.setBackground(Color.white);JRadioButton a =新的JRadioButton();a.setBackground(Color.white);a.setSize(25,25);JLabel aFill =新的JLabel(包括高碳水化合物,高蛋白质和低脂肪的食物");aFill.setFont(font);answerA.add(a);answerA.add(aFill);JPanel answerB =新的JPanel(新的FlowLayout(FlowLayout.LEFT));answerB.setBackground(Color.white);JRadioButton b =新的JRadioButton();b.setBackground(Color.white);b.setSize(25,25);JLabel bFill =新的JLabel(准备不含利尿剂食物的饭菜");bFill.setFont(font);answerB.add(b);answerB.add(bFill);JPanel answerC = new JPanel(new FlowLayout(FlowLayout.LEFT));answerC.setBackground(Color.white);JRadioButton c =新的JRadioButton();c.setBackground(Color.white);c.setSize(25,25);JLabel cFill =新的JLabel(在比赛开始前四个小时准备饭菜");cFill.setFont(font);answerC.add(c);answerC.add(cFill);JPanel answerD = new JPanel(new FlowLayout(FlowLayout.LEFT));answerD.setBackground(Color.white);JRadioButton d =新的JRadioButton();d.setBackground(Color.white);d.setSize(25,25);JLabel dFill =新的JLabel(用延迟胃排空的食物准备饭菜");dFill.setFont(font);answerD.add(d);answerD.add(dFill);JPanel记录= new JPanel(new FlowLayout(FlowLayout.LEFT));record.setBackground(Color.lightGray);JLabel未回答=新的JLabel("Unanswered:");unanswered.setFont(font);record.add(未答复);JLabel unansweredNumber =新的JLabel("25");unansweredNumber.setFont(font);unansweredNumber.setForeground(Color.blue);record.add(unansweredNumber);JLabel space1 =新的JLabel();record.add(space1);JLabel回答=新的JLabel("Answered:");Answer.setFont(font);record.add(已回答);JLabel AnswerNumber =新的JLabel("0");AnswerNumber.setFont(font);AnswerNumber.setForeground(Color.blue);record.add(answeredNumber);JLabel space2 =新的JLabel();record.add(space2);已标记JLabel =新的JLabel("Flagged:");flagged.setFont(font);record.add(标记);JLabel flaggedNumber =新的JLabel("0");flaggedNumber.setFont(font);flaggedNumber.setForeground(Color.blue);record.add(flaggedNumber);answer.add(type);answer.add(answerA);answer.add(answerB);answer.add(answerC);answer.add(answerD);answer.add(record);lower1.add(question);lower1.add(answer);lowerPanel.add(lower1);getContentPane().setBackground(Color.white);upperPanel.setVisible(true);lowerPanel.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);}@Override公共无效actionPerformed(ActionEvent e){if(e.getSource()==提交){SubmitReview reviewScreen = new SubmitReview();this.setVisible(false);reviewScreen.setVisible(true);}其他if(e.getSource()==显示){if(timer.getTimeLabel().isVisible()== true){timer.getTimeLabel().setVisible(false);}别的{timer.getTimeLabel().setVisible(true);}}}} 

最终提交

 包edu.kings.pexam.student;导入java.awt.BorderLayout;导入java.awt.Color;导入java.awt.Dimension;导入java.awt.FlowLayout;导入java.awt.Font;导入java.awt.GridLayout;导入java.awt.event.ActionEvent;导入java.awt.event.ActionListener;导入java.awt.event.MouseAdapter;导入java.awt.event.MouseEvent;导入java.io.File;导入java.io.IOException;导入javax.imageio.ImageIO;导入javax.swing.ImageIcon;导入javax.swing.JButton;导入javax.swing.JComboBox;导入javax.swing.JFrame;导入javax.swing.JLabel;导入javax.swing.JOptionPane;导入javax.swing.JPanel;导入javax.swing.JTextField;公共类SubmitReview扩展JFrame实现ActionListener {私有静态最终长serialVersionUID = 1L;私有JButton hideTimerButton;私有JButton showTimerButton;私人JComboBox< String>languageBox;私人JButton returnToQuizButton;私有JButton endQuizButton;私有JTextField textEnter;静态QuizTimer计时器= TakeQuiz.timer;/***运行程序以生成用于提交审阅的屏幕.* @参数args*/公共静态void main(String [] args){SubmitReview窗口=新的SubmitReview();//将窗口设置为可见window.setVisible(true);}公共SubmitReview(){//将屏幕扩展到最大尺寸setExtendedState(JFrame.MAXIMIZED_BOTH);//创建一个面板,该面板将项目推向右侧.JPanel leftPanel =新的JPanel();leftPanel.setPreferredSize(new Dimension(200,HEIGHT));leftPanel.setBackground(Color.white);this.add(leftPanel,BorderLayout.WEST);//Panel页面上所有内容的去向.JPanel rightPanel =新JPanel(新GridLayout(10,1));rightPanel.setBackground(Color.white);this.add(rightPanel,BorderLayout.CENTER);//文字的字体字体textFont = new Font("Dialog",Font.PLAIN,15);//网格中的第一个面板.网格从上到下移动.JPanel panel0 =新的JPanel(新的FlowLayout(FlowLayout.RIGHT));panel0.setBackground(Color.white);//隐藏计时器按钮,在显示计时器时可见.hideTimerButton =新的JButton("H​​ide Timer");hideTimerButton.setBackground(Color.lightGray);维度hideTimerDimension =新维度(100,25);hideTimerButton.setSize(hideTimerDimension);hideTimerButton.setMinimumSize(hideTimerDimension);hideTimerButton.setMaximumSize(hideTimerDimension);hideTimerButton.setPreferredSize(hideTimerDimension);hideTimerButton.setVisible(true);//显示计时器按钮,当不显示计时器时可见.showTimerButton = new JButton("Show Timer");showTimerButton.setBackground(Color.lightGray);维度showTimerDimension =新维度(125,25);showTimerButton.setSize(showTimerDimension);showTimerButton.setMinimumSize(showTimerDimension);showTimerButton.setMaximumSize(showTimerDimension);showTimerButton.setPreferredSize(showTimerDimension);showTimerButton.setVisible(false);//为显示和隐藏计时器按钮创建功能hideTimerButton.addActionListener(this);showTimerButton.addActionListener(this);panel0.add(timer.getTimeLabel());panel0.add(hideTimerButton);panel0.add(showTimerButton);rightPanel.add(panel0);//网格中的第二个面板.JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));panel1.setBackground(Color.white);//将此面板拆分为网格JPanel网格= new JPanel(new GridLayout(2,1));//一个面板来容纳语言下拉菜单JPanel languagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));languagePanel.setBackground(Color.white);//创建一个语言组合框(下拉菜单)String []种语言= {英语",西班牙语",法语",葡萄牙语"};languageBox =新的JComboBox< String>(语言);languageBox.setBackground(Color.white);languageBox.addActionListener(this);languagePanel.add(languageBox);//组合框下的文本JPanel textPanel =新的JPanel();JLabel text = new JLabel(您现在要结束考试吗?");Font font = new Font("Dialog",Font.PLAIN,17);text.setFont(font);textPanel.setBackground(Color.white);textPanel.add(text);grid.add(languagePanel);grid.add(textPanel);//停车标志图片文件stopSign = new File("resources/stop_sign.png");ImageIcon stopSignIcon = null;尝试 {stopSignIcon =新的ImageIcon(ImageIO.read(stopSign));}捕获(IOException e){System.out.println(捕获的异常:" + e);}JLabel stopLabel =新的JLabel();stopLabel.setIcon(stopSignIcon);stopLabel.setBackground(Color.white);stopLabel.setBorder(null);panel1.add(grid);panel1.add(stopLabel);rightPanel.add(panel1);//网格中的第三个面板JPanel panel2 =新的JPanel(new FlowLayout(FlowLayout.LEFT));panel2.setBackground(Color.white);//将面板拆分为网格JPanel textArea2 =新JPanel(新GridLayout(2,1));textArea2.setBackground(Color.white);JPanel warningText1 =新的JPanel(new FlowLayout(FlowLayout.RIGHT));warningText1.setBackground(Color.white);JLabel warningText1Point = new JLabel(< html>< li>您未回答以下问题.如果现在结束考试,< b>您将失去回答这些问题的机会.</b></html>);warningText1Point.setFont(textFont);warningText1.add(warningText1Point);JPanel breakPanel =新的JPanel(new FlowLayout(FlowLayout.LEFT));breakPanel.setBackground(Color.white);JLabel空间=新的JLabel(< html>< t></t></html>");breakPanel.add(space);//将假问题按钮添加到面板JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));buttonPanel.setBackground(Color.white);JButton一个=新的JButton("1");JButton两个=新的JButton("5");JButton三=新的JButton("12");one.setBackground(Color.lightGray);two.setBackground(Color.lightGray);three.setBackground(Color.lightGray);buttonPanel.add(one);buttonPanel.add(two);buttonPanel.add(三);breakPanel.add(buttonPanel);textArea2.add(warningText1);textArea2.add(breakPanel);panel2.add(textArea2);rightPanel.add(panel2);//网格中的第四个面板JPanel panel3 =新的JPanel(new FlowLayout(FlowLayout.LEFT));panel3.setBackground(Color.white);JPanel textArea3 = new JPanel(new GridLayout(2,1));textArea3.setBackground(Color.white);JPanel warningText3 =新的JPanel(new FlowLayout(FlowLayout.RIGHT));warningText3.setBackground(Color.white);JLabel warningText3Point = new JLabel(< html>< li>您标记了以下问题供以后查看.如果您现在结束考试,< b>您将失去查看这些标记问题的机会.</html>);textArea3.setBackground(Color.white);warningText3Point.setFont(textFont);JPanel breakPanel3 =新的JPanel(新的FlowLayout(FlowLayout.LEFT));breakPanel3.setBackground(Color.white);JLabel space3 = new JLabel(< html>< t></t></html>");breakPanel3.add(space3);JPanel buttonPanel3 =新的JPanel(新的FlowLayout(FlowLayout.RIGHT));buttonPanel3.setBackground(Color.white);JButton四个=新的JButton("4");JButton五个=新的JButton("9");JButton六个=新的JButton("20");four.setBackground(Color.lightGray);五.setBackground(Color.lightGray);six.setBackground(Color.lightGray);buttonPanel3.add(四个);buttonPanel3.add(五);buttonPanel3.add(六个);breakPanel3.add(buttonPanel3);textArea3.add(warningText3Point);textArea3.add(warningText3);textArea3.add(breakPanel3);panel3.add(textArea3);rightPanel.add(panel3);//网格中的第五个面板JPanel panel4 =新的JPanel(新的FlowLayout(FlowLayout.LEFT));panel4.setBackground(Color.white);JPanel grid4 =新的JPanel(new GridLayout(2,1));grid4.setBackground(Color.white);JPanel border4 =新的JPanel(新的GridLayout(1,2));border4.setBackground(Color.white);JPanel spacer4 =新的JPanel();spacer4.setBackground(Color.white);JPanel button4 =新的JPanel();button4.setBackground(Color.white);returnToQuizButton = new JButton(否.返回测验");returnToQuizButton.setBackground(Color.lightGray);returnToQuizButton.addActionListener(this);button4.add(returnToQuizButton,BorderLayout.SOUTH);JPanel textPanel4 =新的JPanel(新的FlowLayout(FlowLayout.LEFT));textPanel4.setBackground(Color.white);JLabel label4 = new JLabel(< html>< li>您还有剩余时间.</b></html>"));label4.setFont(textFont);textPanel4.add(label4);border4.add(spacer4);border4.add(button4);grid4.add(textPanel4);grid4.add(border4);panel4.add(grid4);rightPanel.add(panel4);//网格中的第六个面板JPanel panel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));panel5.setBackground(Color.white);JPanel textPanel5 =新的JPanel(新的FlowLayout(FlowLayout.LEFT));textPanel5.setBackground(Color.white);JLabel text5 = new JLabel(< html>< li>如果现在结束考试,< b>您将无法返回考试.</b></html>");text5.setFont(textFont);textPanel5.add(text5);panel5.add(textPanel5);rightPanel.add(panel5);//网格中的第七个面板JPanel panel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));panel6.setBackground(Color.white);JPanel textPanel6 =新的JPanel(新的FlowLayout(FlowLayout.LEFT));textPanel6.setBackground(Color.white);JLabel text6 = new JLabel(如果现在准备结束多项选择考试,请在下面的框中输入单词我理解".");text6.setFont(textFont);textPanel6.add(text6);panel6.add(textPanel6);rightPanel.add(panel6);//网格中的八个面板JPanel panel7 =新的JPanel(新的FlowLayout(FlowLayout.CENTER));panel7.setBackground(Color.white);textEnter = new JTextField(在此输入我理解".");textEnter.setFont(textFont);textEnter.setColumns(13);//清除鼠标单击框textEnter.addMouseListener(new MouseAdapter(){公共无效mouseClicked(MouseEvent e){textEnter.setText(");}});textEnter.addActionListener(this);JPanel textHolder =新的JPanel();textHolder.setBackground(Color.white);textHolder.add(textEnter,BorderLayout.CENTER);panel7.add(textHolder);rightPanel.add(panel7);//网格中的第九个面板JPanel panel8 = new JPanel(new FlowLayout(FlowLayout.CENTER));panel8.setBackground(Color.white);endQuizButton = new JButton(是.立即结束测验");endQuizButton.setBackground(Color.lightGray);endQuizButton.addActionListener(this);endQuizButton.setEnabled(false);JPanel button8 =新的JPanel();button8.setBackground(Color.white);button8.add(endQuizButton,BorderLayout.CENTER);panel8.add(button8);rightPanel.add(panel8);getContentPane().setBackground(Color.white);leftPanel.setVisible(true);rightPanel.setVisible(true);this.setDefaultCloseOperation(EXIT_ON_CLOSE);}@Override公共无效actionPerformed(ActionEvent e){字符串文本= textEnter.getText();if(text.equals(我明白")){endQuizButton.setEnabled(true);}if(e.getSource()== hideTimerButton){hideTimerButton.setVisible(false);timer.getTimeLabel().setVisible(false);showTimerButton.setVisible(true);}其他if(e.getSource()== showTimerButton){showTimerButton.setVisible(false);hideTimerButton.setVisible(true);timer.getTimeLabel().setVisible(true);}其他if(e.getSource()== returnToQuizButton){TakeQuiz quizScreen =新的TakeQuiz();this.setVisible(false);quizScreen.setVisible(true);}其他if(e.getSource()== endQuizButton){int response = JOptionPane.showConfirmDialog(null,确定要提交测验以进行评分吗?",选择选项",JOptionPane.YES_NO_OPTION);if(response == JOptionPane.YES_OPTION){JOptionPane.showMessageDialog(null,您在本测验中的成绩是:85");System.exit(0);}}}} 

测验计时器

 包edu.kings.pexam.student;导入java.awt.Font;导入java.awt.event.ActionEvent;导入java.awt.event.ActionListener;导入java.text.SimpleDateFormat;导入javax.swing.JLabel;导入javax.swing.JOptionPane;导入javax.swing.Timer;公共类QuizTimer {私人静态两倍时间= 1.44 * Math.pow(10,7);私人SimpleDateFormat setTime = new SimpleDateFormat("hh:mm:ss");私人JLabel timeLabel;私人计时器倒计时;公共QuizTimer(){countDown = new Timer(1000,new ActionListener(){公共无效actionPerformed(ActionEvent e){如果(时间> = 0){timeLabel.setText(setTime.format(time));时间=时间-1000;}别的{JOptionPane.showMessageDialog(null,您的测验已自动提交以进行评分.","Out of Time",JOptionPane.OK_OPTION);System.exit(0);}}});timeLabel =新的JLabel();timeLabel.setFont(new Font("Dialog",Font.PLAIN + Font.BOLD,24));timeLabel.setVisible(true);}公共JLabel getTimeLabel(){返回时间标签;}public void start(){countDown.start();}} 

解决方案

第一个问题(11:00:00到7:00:00)可能与您的时区有关.

第二个可能(从我的头顶开始)与时间字段是静态的有关.

无论如何,我很好奇为什么它是静态的.如果您有两个计时器,似乎这种逻辑就会中断.

(请投票.)

I am part of team that is creating a test for students to get used to a certain format before they have to take a certification test. The test is four hours long and as I am trying to implement the timer I am seeing unusual patterns.

I expect the timer to start at 4 hours (in HH:MM:SS format) and count down to all zeros, unfortunately it is starting at 11:00:00 and counting down to 07:00:00.

The next problem is the timer has to be shown between two different pages. The actual taking of the exam and a review page. When I toggle back and fourth between the pages the counter starts decrementing by the multiple of times clicked between pages.

Below is my created timer class, take quiz and submit review. The code is not perfect and needs work but I needed a GUI mock up to present.

Any help would be appreciated. Thank you.

TAKE QUIZ

package edu.kings.pexam.student;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

public class TakeQuiz extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JButton submit;
    private JButton show;

    static QuizTimer timer;

    /**
     * @param args
     */
    public static void main(String[] args){
        TakeQuiz window = new TakeQuiz();
        window.setVisible(true);

    }

    public TakeQuiz(){
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        JPanel upperPanel = new JPanel(new GridLayout(2,8));
        upperPanel.setPreferredSize(new Dimension(WIDTH,100));
        upperPanel.setBackground(Color.lightGray);
        this.add(upperPanel,BorderLayout.NORTH);

        JPanel lowerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        lowerPanel.setBackground(Color.white);
        this.add(lowerPanel, BorderLayout.CENTER);

        Font font = new Font("Dialog",Font.PLAIN,17);
        Font buttonFont = new Font("Dialog",Font.PLAIN,13);
        Font textButtonFont = new Font("Dialog",Font.PLAIN+Font.BOLD,13);
        Font submitButtonFont = new Font("Dialog", Font.PLAIN + Font.BOLD,15);

        //adding the questions buttons to the upper panel
        JPanel questionPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        questionPanel.setBackground(Color.lightGray);

        JButton firstQuestion = new JButton("<<");
        firstQuestion.setFont(buttonFont);
        questionPanel.add(firstQuestion);

        //space to help button line up with text
        JPanel spacer1 = new JPanel();
        spacer1.setBackground(Color.lightGray);
        questionPanel.add(spacer1);

        JButton perviousQuestion = new JButton("<");
        perviousQuestion.setFont(buttonFont);
        questionPanel.add(perviousQuestion);

        //space to help button line up with text
        JPanel spacer2 = new JPanel();
        spacer2.setBackground(Color.lightGray);
        questionPanel.add(spacer2);

        JButton nextQuestion = new JButton(">");
        nextQuestion.setFont(buttonFont);
        questionPanel.add(nextQuestion);

        //space to help button line up with text
        JPanel spacer3 = new JPanel();
        spacer3.setBackground(Color.lightGray);
        questionPanel.add(spacer3);

        JButton lastQuestion = new JButton(">>");
        lastQuestion.setFont(buttonFont);
        questionPanel.add(lastQuestion);

        upperPanel.add(questionPanel);

        //adding the goto button to the upper panel
        JPanel goToPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        goToPanel.setBackground(Color.lightGray);

        JButton goTo = new JButton("Go To");
        goTo.setFont(textButtonFont);
        goToPanel.add(goTo);

        upperPanel.add(goToPanel);

        //adding the flag buttons to the upper panel
        JPanel flagPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        flagPanel.setBackground(Color.lightGray);

        JButton flagP = new JButton("< Flag");
        flagP.setFont(textButtonFont);
        flagPanel.add(flagP);

        JButton flag = new JButton("Flag");
        flag.setFont(textButtonFont);
        flagPanel.add(flag);

        JButton flagN = new JButton("Flag >");
        flagN.setFont(textButtonFont);
        flagPanel.add(flagN);

        upperPanel.add(flagPanel);

        //adding help and hide/show timer buttons to the upper panel 
        JPanel timerPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        timerPanel.setBackground(Color.lightGray);

        JButton help = new JButton("Help");
        help.setFont(textButtonFont);
        timerPanel.add(help);

        show = new JButton("Show/Hide Timer");
        show.setFont(textButtonFont);
        show.addActionListener(this);
        timerPanel.add(show);

        upperPanel.add(timerPanel);

        //adding space panels
        JPanel spacePanel1 = new JPanel();
        JPanel spacePanel2 = new JPanel();
        spacePanel1.setBackground(Color.lightGray);
        spacePanel2.setBackground(Color.lightGray);
        upperPanel.add(spacePanel1);
        upperPanel.add(spacePanel2);

        //adding the submit button to the upper panel
        JPanel submitPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        submitPanel.setBackground(Color.lightGray);

        submit = new JButton("Submit Quiz");
        submit.setFont(submitButtonFont);
        submit.addActionListener(this);
        submitPanel.add(submit);

        upperPanel.add(submitPanel);

        //adding the question button text to the upper panel
        JPanel questionText = new JPanel(new FlowLayout(FlowLayout.LEFT));
        questionText.setBackground(Color.lightGray);

        JLabel label2 = new JLabel("<html><center>First<br></br>Question</center></html>");
        label2.setFont(textButtonFont);
        questionText.add(label2);

        JLabel label4 = new JLabel("<html><center>Perivous<br></br>Question</center></html>");
        label4.setFont(textButtonFont);
        questionText.add(label4);

        JLabel label6 = new JLabel("<html><center>Next<br></br>Question</center></html>");
        label6.setFont(textButtonFont);
        questionText.add(label6);

        JLabel label8 = new JLabel("<html><center>Last<br></br>Question</center></html>");
        label8.setFont(textButtonFont);
        questionText.add(label8);

        upperPanel.add(questionText);

        //adding text box for go to button
        JPanel textGoTo = new JPanel(new FlowLayout(FlowLayout.CENTER));
        textGoTo.setBackground(Color.lightGray);

        JPanel upper10 = new JPanel();
        upper10.setBackground(Color.lightGray);
        JTextField goToText = new JTextField("1",2);
        JLabel label10 = new JLabel("/25");
        label10.setFont(font);
        upper10.add(goToText,BorderLayout.CENTER);
        upper10.add(label10,BorderLayout.CENTER);
        textGoTo.add(upper10);

        upperPanel.add(textGoTo);

        //adding spacer to the upper panel
        JPanel spacePanel3 = new JPanel();
        spacePanel3.setBackground(Color.lightGray);
        upperPanel.add(spacePanel3);

        //adding the timer to the upper panel
        JPanel timePanel = new JPanel();
        timePanel.setBackground(Color.lightGray);

        timer = new QuizTimer();
        timer.start();
        JPanel upper20 = new JPanel();
        upper20.setBackground(Color.lightGray);
        upper20.add(timer.getTimeLabel(),BorderLayout.CENTER);
        timePanel.add(upper20);

        upperPanel.add(timePanel);

        //adding two more space panels
        JPanel spacePanel4 = new JPanel();
        JPanel spacePanel5 = new JPanel();
        spacePanel4.setBackground(Color.lightGray);
        spacePanel5.setBackground(Color.lightGray);
        upperPanel.add(spacePanel4);
        upperPanel.add(spacePanel5);

        //adding the questions to the lower panel
        JPanel lower1 = new JPanel(new GridLayout(4,1));
        lower1.setBackground(Color.white);
        JLabel question = new JLabel("<html>The parents of a 16-year-old swimmer contact an athletic trainer seeking nutritional advice for the athlete's pre-event<br><\bmeal. What recommendation should the athletic trainer share share with the parents regrading ideal pre-event meals?</html>");
        question.setFont(new Font("Dialog", Font.PLAIN+Font.BOLD, 18));
        JPanel answer = new JPanel(new GridLayout(6,1));
        answer.setBackground(Color.white);
        JLabel type = new JLabel("Choose all that apply.");
        type.setFont(new Font("Dialog", Font.PLAIN+Font.BOLD+Font.ITALIC, 20));


        JPanel answerA = new JPanel(new FlowLayout(FlowLayout.LEFT));
        answerA.setBackground(Color.white);
        JRadioButton a = new JRadioButton();
        a.setBackground(Color.white);
        a.setSize(25,25);
        JLabel aFill = new JLabel("Include foods high in carbohydrates, high in proteins , and low in fats");
        aFill.setFont(font);
        answerA.add(a);
        answerA.add(aFill);
        JPanel answerB = new JPanel(new FlowLayout(FlowLayout.LEFT));
        answerB.setBackground(Color.white);
        JRadioButton b = new JRadioButton();
        b.setBackground(Color.white);
        b.setSize(25,25);
        JLabel bFill = new JLabel("Prepare meals without diuretics foods");
        bFill.setFont(font);
        answerB.add(b);
        answerB.add(bFill);
        JPanel answerC = new JPanel(new FlowLayout(FlowLayout.LEFT));
        answerC.setBackground(Color.white);
        JRadioButton c = new JRadioButton();
        c.setBackground(Color.white);
        c.setSize(25,25);
        JLabel cFill = new JLabel("Prepare meals for eating four hours prior to the competition");
        cFill.setFont(font);
        answerC.add(c);
        answerC.add(cFill);
        JPanel answerD = new JPanel(new FlowLayout(FlowLayout.LEFT));
        answerD.setBackground(Color.white);
        JRadioButton d = new JRadioButton();
        d.setBackground(Color.white);
        d.setSize(25,25);
        JLabel dFill = new JLabel("Prepare meals with food that delay gastric emptying");
        dFill.setFont(font);
        answerD.add(d);
        answerD.add(dFill);

        JPanel record = new JPanel(new FlowLayout(FlowLayout.LEFT));
        record.setBackground(Color.lightGray);
        JLabel unanswered = new JLabel("Unanswered: ");
        unanswered.setFont(font);
        record.add(unanswered);
        JLabel unansweredNumber = new JLabel("25");
        unansweredNumber.setFont(font);
        unansweredNumber.setForeground(Color.blue);
        record.add(unansweredNumber);
        JLabel space1 = new JLabel();
        record.add(space1);
        JLabel answered = new JLabel("Answered: ");
        answered.setFont(font);
        record.add(answered);
        JLabel answeredNumber = new JLabel("0");
        answeredNumber.setFont(font);
        answeredNumber.setForeground(Color.blue);
        record.add(answeredNumber);
        JLabel space2 = new JLabel();
        record.add(space2);
        JLabel flagged = new JLabel("Flagged: ");
        flagged.setFont(font);
        record.add(flagged);
        JLabel flaggedNumber = new JLabel("0");
        flaggedNumber.setFont(font);
        flaggedNumber.setForeground(Color.blue);
        record.add(flaggedNumber);

        answer.add(type);
        answer.add(answerA);
        answer.add(answerB);
        answer.add(answerC);
        answer.add(answerD);
        answer.add(record);

        lower1.add(question);
        lower1.add(answer);
        lowerPanel.add(lower1);



        getContentPane().setBackground(Color.white);
        upperPanel.setVisible(true);
        lowerPanel.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
         if(e.getSource() == submit){
            SubmitReview reviewScreen = new SubmitReview();
            this.setVisible(false);
            reviewScreen.setVisible(true);
        }else if(e.getSource() == show){
            if(timer.getTimeLabel().isVisible() == true){
                timer.getTimeLabel().setVisible(false);
            }else{
                timer.getTimeLabel().setVisible(true);
            }
        }

    }
}

FINAL SUBMIT

package edu.kings.pexam.student;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class SubmitReview extends JFrame implements ActionListener {

    private static final long serialVersionUID = 1L;

    private JButton hideTimerButton;
    private JButton showTimerButton;
    private JComboBox<String> languageBox;
    private JButton returnToQuizButton;
    private JButton endQuizButton;
    private JTextField textEnter;

    static QuizTimer timer = TakeQuiz.timer;


    /**
     * Runs the program to produce the screen for submission review.
     * @param args
     */
    public static void main(String[] args){
        SubmitReview window = new SubmitReview();
        //sets the window visible
        window.setVisible(true);

    }

    public SubmitReview(){


        //Extends the screen to maximum size 
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        //Creates a panel that will keep items pushed to the right.
        JPanel leftPanel = new JPanel();
        leftPanel.setPreferredSize(new Dimension(200,HEIGHT));
        leftPanel.setBackground(Color.white);
        this.add(leftPanel,BorderLayout.WEST);

        //Panel where everything on page will go. 
        JPanel rightPanel = new JPanel(new GridLayout(10,1));
        rightPanel.setBackground(Color.white);
        this.add(rightPanel, BorderLayout.CENTER);

        //font for the text
        Font textFont = new Font("Dialog",Font.PLAIN,15);

        //First panel in the grid. Grid moves from top to bottom.
        JPanel panel0 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        panel0.setBackground(Color.white);
        //hide timer button, visible when timer is shown.
        hideTimerButton = new JButton("Hide Timer");
        hideTimerButton.setBackground(Color.lightGray);
        Dimension hideTimerDimension = new Dimension(100,25);
        hideTimerButton.setSize(hideTimerDimension);
        hideTimerButton.setMinimumSize(hideTimerDimension);
        hideTimerButton.setMaximumSize(hideTimerDimension);
        hideTimerButton.setPreferredSize(hideTimerDimension);
        hideTimerButton.setVisible(true);
        //show timer button, visible when timer is not shown.
        showTimerButton = new JButton("Show Timer");
        showTimerButton.setBackground(Color.lightGray);
        Dimension showTimerDimension = new Dimension(125, 25);
        showTimerButton.setSize(showTimerDimension);
        showTimerButton.setMinimumSize(showTimerDimension);
        showTimerButton.setMaximumSize(showTimerDimension);
        showTimerButton.setPreferredSize(showTimerDimension);
        showTimerButton.setVisible(false);
        //creates functionality for the show and hide timer buttons
        hideTimerButton.addActionListener(this);
        showTimerButton.addActionListener(this);
        panel0.add(timer.getTimeLabel());
        panel0.add(hideTimerButton);
        panel0.add(showTimerButton);
        rightPanel.add(panel0);

        //Second panel in the grid.
        JPanel panel1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel1.setBackground(Color.white);
        //Splits this panel into a grid
        JPanel grid = new JPanel(new GridLayout(2,1));
        //A Panel to hold the language drop down menu
        JPanel languagePanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        languagePanel.setBackground(Color.white);
        //Creates a combo box of languages (drop down menu)
        String[] languages = {"English", "Spanish", "French", "Portuguese" };
        languageBox = new JComboBox<String>(languages);
        languageBox.setBackground(Color.white);
        languageBox.addActionListener(this);
        languagePanel.add(languageBox);
        //Text under the combo box
        JPanel textPanel = new JPanel();
        JLabel text = new JLabel("Do you want to end your exam now?");
        Font font = new Font("Dialog",Font.PLAIN,17);
        text.setFont(font);
        textPanel.setBackground(Color.white);
        textPanel.add(text);
        grid.add(languagePanel);
        grid.add(textPanel);
        //Stop sign picture
        File stopSign = new File("resources/stop_sign.png");
        ImageIcon stopSignIcon = null;
        try {
            stopSignIcon = new ImageIcon(ImageIO.read(stopSign));
        }
        catch (IOException e) {
            System.out.println("Caught exception:" + e);
        }
        JLabel stopLabel = new JLabel();
        stopLabel.setIcon(stopSignIcon);
        stopLabel.setBackground(Color.white);
        stopLabel.setBorder(null);
        panel1.add(grid);
        panel1.add(stopLabel);
        rightPanel.add(panel1);

        //third panel in the grid
        JPanel panel2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel2.setBackground(Color.white);
        //splits the panel into a grid
        JPanel textArea2 = new JPanel(new GridLayout(2,1));
        textArea2.setBackground(Color.white);
        JPanel warningText1 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        warningText1.setBackground(Color.white);
        JLabel warningText1Point = new JLabel("<html><li>You left the following questions unanswered. If you end your exam now,<b> you lose the chance to answer these questions.</b></html>");
        warningText1Point.setFont(textFont);
        warningText1.add(warningText1Point);
        JPanel breakPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        breakPanel.setBackground(Color.white);
        JLabel space = new JLabel("<html><t>  </t></html>");
        breakPanel.add(space);
        //adds FAKE question buttons to the panel
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel.setBackground(Color.white);
        JButton one = new JButton("1");
        JButton two = new JButton("5");
        JButton three = new JButton("12");
        one.setBackground(Color.lightGray);
        two.setBackground(Color.lightGray);
        three.setBackground(Color.lightGray);
        buttonPanel.add(one);
        buttonPanel.add(two);
        buttonPanel.add(three);
        breakPanel.add(buttonPanel);
        textArea2.add(warningText1);
        textArea2.add(breakPanel);
        panel2.add(textArea2);
        rightPanel.add(panel2);

        //fourth panel in the grid
        JPanel panel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel3.setBackground(Color.white);
        JPanel textArea3 = new JPanel(new GridLayout(2,1));
        textArea3.setBackground(Color.white);
        JPanel warningText3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        warningText3.setBackground(Color.white);
        JLabel warningText3Point = new JLabel("<html><li>You marked the following questions for later review. If you end your exam now, <b>you lose the chance to review these marked questions.</b></html>");
        textArea3.setBackground(Color.white);
        warningText3Point.setFont(textFont);
        JPanel breakPanel3 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        breakPanel3.setBackground(Color.white);
        JLabel space3 = new JLabel("<html><t>  </t></html>");
        breakPanel3.add(space3);
        JPanel buttonPanel3 = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        buttonPanel3.setBackground(Color.white);
        JButton four = new JButton("4");
        JButton five = new JButton("9");
        JButton six = new JButton("20");
        four.setBackground(Color.lightGray);
        five.setBackground(Color.lightGray);
        six.setBackground(Color.lightGray);
        buttonPanel3.add(four);
        buttonPanel3.add(five);
        buttonPanel3.add(six);
        breakPanel3.add(buttonPanel3);
        textArea3.add(warningText3Point);
        textArea3.add(warningText3);
        textArea3.add(breakPanel3);
        panel3.add(textArea3);
        rightPanel.add(panel3);

        //fifth panel in the grid
        JPanel panel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel4.setBackground(Color.white);
        JPanel grid4 = new JPanel(new GridLayout(2,1));
        grid4.setBackground(Color.white);
        JPanel border4 = new JPanel(new GridLayout(1,2));
        border4.setBackground(Color.white);
        JPanel spacer4 = new JPanel();
        spacer4.setBackground(Color.white);
        JPanel button4 = new JPanel();
        button4.setBackground(Color.white);
        returnToQuizButton = new JButton("No. Return to the Quiz" );
        returnToQuizButton.setBackground(Color.lightGray);
        returnToQuizButton.addActionListener(this);
        button4.add(returnToQuizButton,BorderLayout.SOUTH);
        JPanel textPanel4 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        textPanel4.setBackground(Color.white);
        JLabel label4 = new JLabel("<html><li>You still have time remaining.</b></html>");
        label4.setFont(textFont);
        textPanel4.add(label4);
        border4.add(spacer4);
        border4.add(button4);
        grid4.add(textPanel4);
        grid4.add(border4);
        panel4.add(grid4);
        rightPanel.add(panel4);

        //sixth panel in the grid
        JPanel panel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel5.setBackground(Color.white);
        JPanel textPanel5 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        textPanel5.setBackground(Color.white);
        JLabel text5 = new JLabel("<html><li>If you end your exam now,<b> you cannot return to the exam.</b></html>");
        text5.setFont(textFont);
        textPanel5.add(text5);
        panel5.add(textPanel5);
        rightPanel.add(panel5);

        //seventh panel in the grid
        JPanel panel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        panel6.setBackground(Color.white);
        JPanel textPanel6 = new JPanel(new FlowLayout(FlowLayout.LEFT));
        textPanel6.setBackground(Color.white);
        JLabel text6 = new JLabel("If you are ready to end the multiple-choice exam now, type the words 'I understand' in the box below.");
        text6.setFont(textFont);
        textPanel6.add(text6);
        panel6.add(textPanel6);
        rightPanel.add(panel6);

        //eight panel in the grid
        JPanel panel7 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel7.setBackground(Color.white);
        textEnter = new JTextField("Type 'I understand' here.");
        textEnter.setFont(textFont);
        textEnter.setColumns(13);
        //clears box on click of mouse
        textEnter.addMouseListener(new MouseAdapter(){ 
            public void mouseClicked(MouseEvent e){
                textEnter.setText("");  
            }
        });
        textEnter.addActionListener(this);
        JPanel textHolder = new JPanel();
        textHolder.setBackground(Color.white);
        textHolder.add(textEnter,BorderLayout.CENTER);
        panel7.add(textHolder);
        rightPanel.add(panel7);

        //ninth panel in the grid
        JPanel panel8 = new JPanel(new FlowLayout(FlowLayout.CENTER));
        panel8.setBackground(Color.white);
        endQuizButton = new JButton("Yes. End the Quiz Now");
        endQuizButton.setBackground(Color.lightGray);
        endQuizButton.addActionListener(this);
        endQuizButton.setEnabled(false);
        JPanel button8 = new JPanel();
        button8.setBackground(Color.white);
        button8.add(endQuizButton, BorderLayout.CENTER);
        panel8.add(button8);
        rightPanel.add(panel8);

        getContentPane().setBackground(Color.white);
        leftPanel.setVisible(true);
        rightPanel.setVisible(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String text = textEnter.getText();
        if(text.equals("I understand")){
            endQuizButton.setEnabled(true);
        }
        if(e.getSource() == hideTimerButton){
            hideTimerButton.setVisible(false);
            timer.getTimeLabel().setVisible(false);
            showTimerButton.setVisible(true);
        }else if(e.getSource() == showTimerButton){
            showTimerButton.setVisible(false);
            hideTimerButton.setVisible(true);
            timer.getTimeLabel().setVisible(true);
        }else if(e.getSource() == returnToQuizButton){
            TakeQuiz quizScreen = new TakeQuiz();
            this.setVisible(false);
            quizScreen.setVisible(true);            
        }else if(e.getSource() == endQuizButton){
            int response = JOptionPane.showConfirmDialog(null, "Are you sure you want to submit your quiz for grading?","Select an Option", JOptionPane.YES_NO_OPTION);
            if(response == JOptionPane.YES_OPTION){
                JOptionPane.showMessageDialog(null, "Your grade on this quiz is: 85");
                System.exit(0);             
            }

        }


    }

}

QUIZ TIMER

package edu.kings.pexam.student;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.Timer;

public class QuizTimer {

    private static double time = 1.44*Math.pow(10,7);
    private SimpleDateFormat setTime = new SimpleDateFormat("hh:mm:ss");
    private JLabel timeLabel;
    private Timer countDown;

    public QuizTimer(){

        countDown = new Timer(1000, new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                if (time >= 0) {
                    timeLabel.setText(setTime.format(time));
                    time = time-1000;
                }else{
                    JOptionPane.showMessageDialog(null, "Your quiz has been automatically submitted for grading.", "Out of Time", JOptionPane.OK_OPTION);
                    System.exit(0);
                }
            }   
        });

        timeLabel = new JLabel();
        timeLabel.setFont( new Font("Dialog", Font.PLAIN + Font.BOLD,24));
        timeLabel.setVisible(true);
    }

    public JLabel getTimeLabel(){
        return timeLabel;
    }

    public void start(){
        countDown.start();
    }
}

解决方案

The first problem (11:00:00 to 7:00:00) probably has to do with your timezone.

The second one may (from the top of my head) have to do with time field being static.

In any way, I'd be curious why it is static. Seems that this logic would break if you have two timers.

(P.S. Please vote.)

这篇关于Java Swing计时器未以正确的方式递减且未在正确的时间启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆