Overclock.net banner
1 - 3 of 3 Posts

· Registered
Joined
·
213 Posts
Discussion Starter · #1 ·
Im trying to make a timer using JFrame but im stuck
import java.util.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.event.*;
import java.awt.*;

public class TechComTimeKeeper {
public static void main(String[] args){
UserInput UI = new UserInput();
}
}// end TechComTimeKeeper

class UserInput extends JFrame{
private JPanel panel;
private JList list;
private JLabel enterExpectedTime;
private JLabel enterAllowedTime;
private JTextField expectedTime;
private JTextField allowedTime;
private JButton start;
private JButton finish;

int expTime;
int acceptibleTime;

int[] startTime;
int[] endTime;

public UserInput(){
setUp();
buildPanel();
addStuff();
pack();
setVisible(true);
}
public void setUp(){
setTitle("TechCom Time Keeper");
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void buildPanel(){
panel = new JPanel();
//panel.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(3,2));

enterExpectedTime = new JLabel("Enter the estimated time of your presentation.");
enterAllowedTime = new JLabel ("Enter the allowed time for the presentation.");

expectedTime = new JTextField(10);
expectedTime.addActionListener(new Listener());

allowedTime = new JTextField(10);
allowedTime.addActionListener(new Listener());

start= new JButton("Start the timer");
start.addActionListener(new Listener());

finish = new JButton("End the timer");
finish.addActionListener(new Listener());

panel.add(enterExpectedTime);
panel.add(expectedTime);
panel.add(enterAllowedTime);
panel.add(allowedTime);
panel.add(start);
panel.add(finish);
}
public void addStuff(){
add(panel);
}
private class Listener implements ActionListener{
double totalTime;
double startTime;
double endTime;

public void actionPerformed(ActionEvent e){

if(e.getSource() == start){
Date time1 = Calendar.getInstance().getTime();
startTime = time1.getTime();
System.out.println("You started at: " + startTime/1E12);
}
if(e.getSource() == finish){
Date time2 = Calendar.getInstance().getTime();
endTime = time2.getTime();
System.out.println("You ended at: " + endTime/1E12);
}
// here or above here i want to do totalTime = endTime - startTime;
}
}
}

in the listener class i need to take the endTime - startTime to get totalTime so that i can use that number for other things that the program will do. I also tried using the timer class with the same two buttons but one as start and the other as stop but the timer never stopped. I can not get the timer or the math to work as i can not get both numbers. When i push the start button start is a real time but end is 0.0 and vice versa so i need help. BTW it has to be 2 buttons and this is not an assignment just a practice program on JFrame. I want to enter an expected time and an allowed time then i will need to take 5-6 different time measurements add them all together an compare it to the expected and actual times. This is just something i thought up as i had an oral report recently in which 5 people had 3 minutes to talk and if we went over our allowed time we where cut off. This is my way of keeping track of time. Please help me
Thank you

by the way i was trying to use the timer class like this
private class Listener implements ActionListener{
double totalTime;
double startTime;
double endTime;
Timer timer = new Timer();
public void actionPerformed(ActionEvent e){

if(e.getSource() == start){
timer.start();
}
if(e.getSource() == finish){
timer.stop();
}
}
}
 

· Registered
Joined
·
831 Posts
i just kinda ran with what you had and came up with this. I did a few short tests with the timer on my iphone and it came up with the right answer.

Code:
Code:
import java.util.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import java.awt.event.*;
import java.awt.*;

public class TechComTimeKeeper {
public static void main(String[] args){
UserInput UI = new UserInput();
}
}// end TechComTimeKeeper

class UserInput extends JFrame{
private JPanel panel;
private JList list;
private JLabel enterExpectedTime;
private JLabel enterAllowedTime;
private JTextField expectedTime;
private JTextField allowedTime;
private JButton start;
private JButton finish;
public long time1=0;

int expTime;
int acceptibleTime;

int[] startTime;
int[] endTime;

public UserInput(){
setUp();
buildPanel();
addStuff();
pack();
setVisible(true);
}
public void setUp(){
setTitle("TechCom Time Keeper");
setSize(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void buildPanel(){
panel = new JPanel();
//panel.setLayout(new BorderLayout());
panel.setLayout(new GridLayout(3,2));

enterExpectedTime = new JLabel("Enter the estimated time of your presentation.");
enterAllowedTime = new JLabel ("Enter the allowed time for the presentation.");

expectedTime = new JTextField(10);
expectedTime.addActionListener(new Listener());

allowedTime = new JTextField(10);
allowedTime.addActionListener(new Listener());

start= new JButton("Start the timer");
start.addActionListener(new Listener());

finish = new JButton("End the timer");
finish.addActionListener(new Listener());

panel.add(enterExpectedTime);
panel.add(expectedTime);
panel.add(enterAllowedTime);
panel.add(allowedTime);
panel.add(start);
panel.add(finish);
}
public void addStuff(){
add(panel);
}
private class Listener implements ActionListener{
double totalTime;
double startTime;
double endTime;

public void actionPerformed(ActionEvent e){

if(e.getSource() == start){
time1 = Calendar.getInstance().getTimeInMillis();
System.out.println("You started at: " + time1 + " milliseconds.");
}
if(e.getSource() == finish){
long time2 = Calendar.getInstance().getTimeInMillis();
System.out.println("You ended at: " + time2 + " milliseconds.");
if(time1 != 0)
diff(time1, time2);
}
}
// here or above here i want to do totalTime = endTime - startTime;
public void diff(long time1, long time2){
long diff = time2 - time1;
long diffSeconds = diff / 1000;
    long diffMinutes = diff / (60 * 1000);
    System.out.println("Time difference in seconds: " + diffSeconds
     + " seconds.");
        System.out.println("Time difference in minutes: " + diffMinutes 
    + " minutes.");
}
}
}
 
1 - 3 of 3 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top