Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

need help on java timer

  • 02-05-2012 6:49pm
    #1
    Closed Accounts Posts: 1


    hi i am still a noob in java and i need to make a timer whit gui interface that when the time runs out it will close an application any help would be greatly appreciated tnx.


Comments

  • Registered Users, Registered Users 2 Posts: 2,089 ✭✭✭henryporter


    So what does the GUI do; alow you to input a time for countdown, show the countdown or what?

    For Timer look here: http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Timer.html

    It's simply a matter of putting the time on screen and then updating it with the timer counting down (multiplied by 1000 as its in milliseconds), then when the timer hits zero call System.exit()


  • Closed Accounts Posts: 5,482 ✭✭✭Kidchameleon


    Very quick uncommented throw together of this. May help.

    Use the timer class to get accurate timing.

    Put a JTextField and a JButton on the frame to allow the user to set their own time.
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class Timer extends JFrame
    {
        private JLabel numbers;
        private int time;
        boolean progOver;
        
        public static void main(String[] args)
        {
            Timer prog = new Timer();
            prog.go();
        }
        
        public void go()
        {
            time = 60;
            numbers = new JLabel();
            progOver = false;
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setSize(350, 500);
            setVisible(true);
            getContentPane().setLayout(null);
            
            Font aFont = numbers.getFont();
            numbers.setFont(new Font(aFont.getFontName(), aFont.getStyle(), 50));
            numbers.setBounds(100, 130, 2000, 40);
            
            getContentPane().add(numbers);
            
            long startTime = 0;
            long endTime = 0;
            
            while(!progOver)
            {
                numbers.setText("Time: " + time);
                
                startTime = System.currentTimeMillis() / 1000;
                
                //decrease the timer every second
                if(startTime != endTime)
                {
                    time--;
                }
                
                repaint();
                
                endTime = System.currentTimeMillis() / 1000;
                
                if(time <= 0)
                {
                    progOver = true;
                }
            }
            
            System.exit(0);
        }
    }
    


  • Posts: 0 [Deleted User]


    Another way you could do it is by using thread (a mini process), although, I don't know if you know what they are or how they are used. But if you're interested, here is a simple sample

    I just modified the code posted above...

    [HTML]
    import java.util.*;
    import javax.swing.*;
    import java.awt.*;

    public class Timer extends JFrame {
    private JLabel numbers;
    private int time;
    boolean progOver;

    public static void main(String[] args) {
    Timer prog = new Timer();
    prog.go();
    }

    public void go() {
    time = 60;
    numbers = new JLabel();
    progOver = false;
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(350, 500);
    setVisible(true);
    getContentPane().setLayout(null);

    Font aFont = numbers.getFont();
    numbers.setFont(new Font(aFont.getFontName(), aFont.getStyle(), 50));
    numbers.setBounds(100, 130, 2000, 40);

    getContentPane().add(numbers);

    long startTime = 0;
    long endTime = 0;

    //create thread
    Thread t = new Thread();

    //start thread
    t.start();

    while (!progOver) {
    numbers.setText("Time: " + time);

    try {
    //sleep thread for 1 second. It will never actually be one full second, but if will be very close
    t.sleep(1000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }

    time--;

    repaint();
    if (time < 0)
    progOver = true;

    }

    System.exit(0);
    }
    }
    [/HTML]


Advertisement