Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I need help with this
New Posts  All Forums:Forum Nav:

I need help with this

post #1 of 3
Thread Starter 
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();
}
}
}
Edited by Sin Cities Sin - 4/4/11 at 6:01pm
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
post #2 of 3
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:
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.");
}
}
}
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
Fractal Design
(15 items)
 
775 4 life
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7 2600K Biostar TP67XE NVidia GTX 570 Crucial Ballistix 
Hard DriveHard DriveCoolingOS
Crucial C300 RealSSD SDD Samsung F4 2TB Noctua NH-D14 Windows 7 Professional x64 
MonitorMonitorKeyboardPower
Asus VH202T 20'' 1600x900 Acer P244W 24" 1920 x 1080 Apple Keyboard with Numeric Keypad SeaSonic M12II 620W 
CaseMouseAudio
Fractal Design Define XL Titanium Grey Razor Abyssus Creative Sound Blaster X-FI Xtreme Gamer 
CPUMotherboardGraphicsRAM
Intel X3350 3.2Ghz @ 1.25v Gigabyte-GA-P35-DS3L (rev 2) XFX 4870 1GB 4GB OCZ Reaper PC2-6400 
RAMHard DriveHard DriveOptical Drive
2GB Corsair XMS2 PC2-6400 Crucial C300 64GB SSD 2TB Samsung Spinpoint F4 Sony Super Multi 
OSMonitorPowerCase
Windows 7 Professional x64 SP1 Asus VH202T 20'' 1600x900 SeaSonic M12II 620W Cooler Master Centurion 5 
Mouse
Razor Abyssus 
CPUMotherboardGraphicsRAM
Core i5-520M Lenovo 2522BF3 NVIDIA® Quadro® NVS3100M  Ramaxel Technology 4Gb DDR3 
Hard DriveOptical DriveOSMonitor
Samsung SSD 128GB 1.8" Micro SATA  hl-dt-st dvdram gu10n Windows 7 Enterprise (64-bit) 14.1" WXGA (1280x800) display, anti-glare, LED ... 
Power
9-cell plus Slice battery 
  hide details  
Reply
post #3 of 3
Thread Starter 
thank you so much lol its so obvious how to make this work i cant believe i didnt think of that thank you so much!!!!
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
My Rig
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 860 Asus P7P55D PRO GTX 480 sli wc Corsair Vengeance  
Hard DriveOSPowerCase
WD 1TB BLACK x2 Windows 7 64 bit Silverstone Strider 1000+ CM 690 2 Advanced 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › I need help with this