Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Help on this program

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help on this program

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

    public class Timer implements ActionListener
    {
    public static int time;

    //containers
    private JFrame f;
    private JButton start, stop, reset;

    //components
    private JLabel hour, min, sec;
    private JTextField tf1, tf2, tf3;
    private JPanel p1, p2;

    public Timer()
    {
    //containers
    f = new JFrame("Rad's Timer");

    p1 = new JPanel();
    p2 = new JPanel();

    //components
    start = new JButton("Start");
    stop = new JButton("Stop");
    reset = new JButton("Reset");

    hour = new JLabel("Hour: ");
    min = new JLabel("Minute: ");
    sec = new JLabel("Second: ");

    tf1 = new JTextField("0", 7);
    tf2 = new JTextField("0", 7);
    tf3 = new JTextField("0", 7);
    }

    public void launchFrame()
    {
    p1.add(hour);
    p1.add(tf1);
    p1.add(min);
    p1.add(tf2);
    p1.add(sec);
    p1.add(tf3);
    p2.add(start);
    p2.add(stop);
    p2.add(reset);

    p2.setLayout(new GridLayout());

    f.setLayout(new BorderLayout(2,1));

    f.add(p1, BorderLayout.NORTH);
    f.add(p2, BorderLayout.SOUTH);

    f.pack();
    f.setVisible(true);

    //event handlers registration
    start.addActionListener(this);
    reset.addActionListener(this);

    f.addWindowListener(new MyCloseButtonHandler());
    }

    public void actionPerformed(ActionEvent ae)
    {
    Object source = ae.getSource();
    int num1, num2, num3;

    if (tf1.getText() != null && tf2.getText() != null && tf3.getText() != null)
    {
    /*num1 = Integer.parseInt(tf1.getText());
    num2 = Integer.parseInt(tf2.getText());*/
    num3 = Integer.parseInt(tf3.getText());

    if (source == start)
    {
    for (int i = time; i >= 0; i--)
    {
    try
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException x) {}
    tf3.setText(Integer.toString(num3));
    }
    }
    if (source == reset)
    {
    tf1.setText("0");
    tf2.setText("0");
    tf3.setText("0");
    }
    }
    }

    /*public void count()
    {
    for (int i = time; i >= 0; i--)
    {
    while (i > 0)
    {
    try
    {
    Thread.sleep(1000);
    }
    catch (InterruptedException x) {}
    tf3.setText("Time: " + i--);
    }
    }
    }*/

    private class MyCloseButtonHandler extends WindowAdapter
    {
    public void windowClosing(WindowEvent we)
    {
    System.exit(0);
    }
    }

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

    I need help getting the start button to work on my code. so when you click start, the number you input on TextField for seconds will start a countdown.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Help on this program

    Welcome to the forum! Please read this topic to learn how to post code correctly and other useful info for new members.

    Too much unformatted code posted incorrectly. You should be able to edit your post to add code/highlight tags as described in the link I've provided. The Edit Post control is on the frame at the bottom of the existing post. If that eludes you, then add a reply and post the code correctly there.

    Also describe what currently happens when the Start button is pressed. Nothing? An error occurs? If an error, please post that too.

    Recommendation: Don't give your classes the same name as existing Java classes.

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    rad1996 (March 23rd, 2014)

Similar Threads

  1. How to restart the math in the program without geting out of the program ?
    By Dragon3002 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 15th, 2014, 06:10 AM
  2. Replies: 1
    Last Post: February 6th, 2014, 01:11 PM
  3. Invoke a Java program with windows program
    By jackhard in forum Object Oriented Programming
    Replies: 1
    Last Post: February 21st, 2013, 07:16 AM
  4. Program goes into infinite compilation. University project - Library Program.
    By clarky2006 in forum What's Wrong With My Code?
    Replies: 35
    Last Post: November 10th, 2012, 03:56 PM
  5. Replies: 1
    Last Post: July 8th, 2012, 10:23 AM