import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class OnClickLesson extends JFrame {
private int value = 0, clicked = 0, countdown = 1;
private Timer timer = new Timer(1000, null);
public OnClickLesson() {
timer = new Timer(1000, new countDownTimer());
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
if(value%2 == 0) {
clicked++;
if(clicked == 2) {
System.out.println("even");
timer.start();
}
}
else if(value%2 == 1) {
System.out.println("odd");
timer.start();
}
}
});
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
pack();
setSize(400,600);
setLocationRelativeTo(null);
setVisible(true);
}
private class countDownTimer implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
if (countdown == 0) {
timer.stop();
value++;
}
else {
System.out.println("Countdown " + countdown--);
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new OnClickLesson();
}
});
}
}
Desmond_1
0
Newbie Poster
Recommended Answers
Jump to PostPlease give a proper description of what should happen ve what actually happens.
Jump to PostIs that the desired or the actual behaviour? What's wrong with it?
Jump to PostCountdown starts at 1 and decrements in the timer, but seems never to be reset, so it hits zero exactly once, then continues -1, -2, -3 etc. Keep going 2^32 times and you should get back to zero again!
All 11 Replies
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Desmond_1
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Desmond_1
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Desmond_1
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Desmond_1
0
Newbie Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
JamesCherrill
4,733
Most Valuable Poster
Team Colleague
Featured Poster
Desmond_1
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.