Skip to main content

NEED HELP ASAP

2 replies [Last post]
LanceBot
Offline
Joined: 2013-03-20
Points: 0

the timer in this program does not decrement.can someone please help me? I need this for my Case Study.this is a rapid clicker.here's the code:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Click extends JFrame {

Timer timer;
int countTime;
int countClick;
JLabel directions, enterTime, clickLabel, timeLeft;
JButton start, click;
JTextField field;

public Click()
{
Container con = this.getContentPane();
con.setLayout(new GridLayout(3, 1, 2, 2));

JPanel top = new JPanel();
top.setLayout(new GridLayout(1, 1));

directions = new JLabel("Enter Time, press start, press CLICK! repeatedly", SwingConstants.CENTER);
top.add(directions);
con.add(top);

JPanel mid = new JPanel();
mid.setLayout(new GridLayout(1, 3));

enterTime = new JLabel("Enter Time(sec):", SwingConstants.CENTER);
mid.add(enterTime);

field = new JTextField();
mid.add(field);

start = new JButton("Start");
mid.add(start);
con.add(mid);

JPanel bot = new JPanel();
bot.setLayout(new GridLayout(1, 3));

click = new JButton("CLICK!");
click.setEnabled(false);
bot.add(click);

clickLabel = new JLabel("Clicks: 0", SwingConstants.CENTER);
bot.add(clickLabel);

timeLeft = new JLabel("Time Remaining: ?", SwingConstants.CENTER);
bot.add(timeLeft);
con.add(bot);

StartButtonClass startbutton = new StartButtonClass();
start.addActionListener(startbutton);

ClickButtonClass clickbutton = new ClickButtonClass();
click.addActionListener(clickbutton);
}
public class StartButtonClass implements ActionListener{
public void actionPerformed(ActionEvent startbutton){
try{
int countTime = (int)(Integer.parseInt(field.getText()));

if (countTime<=0){
field.setText("Positive number only!");
}else{
timeLeft.setText("Time remaining: " + countTime);
TimeClass time = new TimeClass(countTime);
timer = new Timer(1000, time);
timer.start();
start.setEnabled(false);
click.setEnabled(true);
}
}catch(NumberFormatException ex){
field.setText("Numbers only!");
}
}
}

public class ClickButtonClass implements ActionListener{
public void actionPerformed(ActionEvent clickbutton){
countClick++;
clickLabel.setText("Clicks: " + countClick);
}
}

public class TimeClass implements ActionListener{
int countTime;

public TimeClass(int countTime){
this.countTime = countTime;
}

public void ActionPerformed(ActionEvent time){
countTime--;

if(countTime>=1){
timeLeft.setText("Time Remaining: " + countTime);
}else{
timer.stop();
timeLeft.setText("Game Over!");
click.setEnabled(false);
}
}

public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub

}
}

public class ResetClass implements ActionListener{
public void actionPerformed(ActionEvent reset){
click.setEnabled(false);
start.setEnabled(true);
countClick = 0;
clickLabel.setText("Clicks: 0");
field.setText("");
timeLeft.setText("Time Remaining: ?");
}

public void addActionListener(ResetClass reset) {
// TODO Auto-generated method stub

}
}
public class ExitClass implements ActionListener{
public void actionPerformed(ActionEvent reset){
System.exit(0);
}

public void addActionListener(ExitClass exit) {
// TODO Auto-generated method stub

}
}

public static void main(String[] args){
Click frame = new Click();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(375, 135);
frame.setTitle("Rapid Click");
frame.setVisible(true);
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
dgodbey
Offline
Joined: 2006-10-27
Points: 0

Are you using an IDE like Eclipse or JDeveloper that will allow you to step through your code?

Agree that this code is not readable. Please post with better formatting.

MackSix
Offline
Joined: 2013-03-27
Points: 0

If it is so urgent, then why didn't you use the code tags so your code is readable? :)