I'm doing a quiz where a user can choose from 3 different quizzez. The questions are stored in a textfile. I wish that the questions are answered within a specified amount of time (i.e a count down timer). If the user isn't able to answer the answers within a specified amount of time, the quiz issues a message showing time is over, and then it should stop showing the questions and display the points scored. I managed to make a count down timer, but my problem is that after the message "time is over" is displayed it continues to display the questions, and so the user can continue answer them, and this is a bit ridiculous. Can someon pls help me figure out my mistake? Thanks a lot. :-)
This are the classes of the quiz:
1.ActionSupport:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.event.EventListenerList;
public class ActionSupport {
protected EventListenerList listenerList = new EventListenerList();
private Object bean;
public ActionSupport(Object registeredBean) {
this.bean = registeredBean;
}
public void fireActionPerformed(String actionCommand) {
Object[] listeners = listenerList.getListenerList();
ActionEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == ActionListener.class) {
e = new ActionEvent(bean, ActionEvent.ACTION_PERFORMED, actionCommand, Calendar.getInstance().getTimeInMillis(), 0);
((ActionListener) listeners[i + 1]).actionPerformed(e);
}
}
}
public void addActionListener(ActionListener l) {
listenerList.add(ActionListener.class, l);
}
public void removeActionListener(ActionListener l) {
listenerList.remove(ActionListener.class, l);
}
}
2.CountDown :
import java.awt.event.ActionListener;
public class CountDown implements Runnable {
private Thread timer;
private int delay;
private int timePassed = 0;
private ActionSupport actionSupport = new ActionSupport(this);
public CountDown(int delay) {
this.delay = delay;
timer = new Thread(this);
timer.setName("Timer");
timer.start();
}
public void addActionListener(ActionListener l) {
actionSupport.addActionListener(l);
}
public void removeActionListener(ActionListener l) {
actionSupport.removeActionListener(l);
}
public void run() {
boolean ended = false;
while (Thread.currentThread().equals(timer) && !ended) {
try {
Thread.sleep(999);
timePassed++;
if (timePassed >= delay) {
actionSupport.fireActionPerformed("ended");
ended = true;
} else {
actionSupport.fireActionPerformed("tick");
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public int getDelay() {
return delay;
}
public int getTimePassed() {
return timePassed;
}
}
3. MainPanel : The gui class
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class MainPanel extends JPanel implements ActionListener {
private JLabel label;
private CountDown countDown;
public MainPanel() {
countDown = new CountDown(10);
countDown.addActionListener(this);
setSize(100, 100);
setLayout(new BorderLayout());
add(getLabel(), BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("ended")) {
label.setText(String.valueOf(countDown.getDelay() - countDown.getTimePassed()));
JOptionPane.showMessageDialog(null, "Time Over");
} else {
label.setText(String.valueOf(countDown.getDelay() - countDown.getTimePassed()));
}
}
public JLabel getLabel() {
if (label == null) {
label = new JLabel();
label.setText(String.valueOf(countDown.getDelay()));
}
return label;
}
public static void main(String[] args) {
JFrame frame = new JFrame("CountDown");
MainPanel panel = new MainPanel();
frame.setSize(panel.getSize());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(panel);
frame.setVisible(true);
}
}
4) And this is the class were i call the CountDown timer
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class JPasswordField1 {
public static void main(String[] argv) {
final JFrame frame = new JFrame("JPassword");
JLabel jlbPassword = new JLabel("Enter the password: ");
JPasswordField jpwName = new JPasswordField(10);
jpwName.setEchoChar('*');
jpwName.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JPasswordField input = (JPasswordField)e.getSource();
char[] password = input.getPassword();
if (isPasswordCorrect(password)) {
JOptionPane.showMessageDialog(frame, "Correct password.");
JOptionPane.showMessageDialog(null, "This is a Geography Quiz");
JOptionPane.showMessageDialog(null, "Good Luck");
JOptionPane.showMessageDialog(null, "You got 50 seconds to answer all the questions in each quiz");
GQ frame = new GQ();
frame.setVisible(true);
MainPanel mp = new MainPanel(); // calling the class for the count down timer
mp.setVisible(true);
} else {
JOptionPane.showMessageDialog(frame, "Sorry. Try again.",
"Error Message", JOptionPane.ERROR_MESSAGE);
}
}
});
JPanel jplContentPane = new JPanel(new BorderLayout());
jplContentPane.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
jplContentPane.add(jlbPassword, BorderLayout.WEST);
jplContentPane.add(jpwName, BorderLayout.CENTER);
frame.setContentPane(jplContentPane);
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { System.exit(0); }
});
frame.pack();
frame.setVisible(true);
}
private static boolean isPasswordCorrect(char[] inputPassword) {
char[] actualPassword = { 'q', 'u', 'i', 'z' };
if (inputPassword.length != actualPassword.length)
return false; //Return false if lengths are unequal
for (int i = 0; i < inputPassword.length; i ++)
if (inputPassword[i] != actualPassword[i])
return false;
return true;
}
}
Thanks for helping.
