Results 1 to 2 of 2
  1. #1
    Parthptl is offline Member
    Join Date
    Oct 2009
    Posts
    1
    Rep Power
    0

    Default Need Help making a Stop Watch

    OK So i want to make a button disappear when i click the other one.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
      
    class StopWatch implements ActionListener {
      
      JLabel jlab;
      long start;
      
      StopWatch() {
        
        JFrame jfrm = new JFrame("The cool stopWatch");
        
        jfrm.getContentPane().setLayout(new FlowLayout());
        
        jfrm.setSize(230,90);
        
        jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton jbtnStart = new JButton("Start");
        JButton jbtnStop = new JButton("Stop");
        
        jbtnStart.addActionListener(this);
        jbtnStop.addActionListener(this);
        
         jfrm.getContentPane().add(jbtnStart);
         jfrm.getContentPane().add(jbtnStop);
        
        jlab = new JLabel("Press Start to begin timing.");
        
        jfrm.getContentPane().add(jlab);
        
        jfrm.setVisible(true);
      }
      public void actionPerformed(ActionEvent ae) {
        Calendar cal = Calendar.getInstance();
        
        if(ae.getActionCommand().equals("Start")) {
          jbtnStart.setVisible(false);
        
         start = cal.getTimeInMillis();
         jlab.setText("Stopwatch is Running...");
        }
        
        if(ae.getActionCommand().equals("Stop")) {
          jbtnStop.setVisible(false);
          jlab.setText("Elapsed time is "
                         + (double) (cal.getTimeInMillis() - start)/1000);
        }
      
    }
    
    }

  2. #2
    zweibieren is offline Senior Member
    Join Date
    Aug 2009
    Location
    Pittsburgh, PA
    Posts
    284
    Rep Power
    6

    Default

    Sigh :( a number of problems in the code above.
    With a quick read I see these; there may be more.

    There will be an undefined-variable compile error
    because the declarations of jbtnStart and jbtnEnd must be out in the class
    -- near the declaration of jLabel --
    not inside the constructor.

    Rather than changing visibility it is better GUI style to enable/disable buttons.
    Then they will be grayed out rather than vanishing.

    The tests inside actionPerformed will always fail because the action command is not set.
    The StopWach() constructor ought to say something like
    jbtnStart.setActionCommand("Start");
    For more, see the Java Tutorial
    How to Use Buttons, Check Boxes, and Radio Buttons

    {Personally I would test the source instead:
    if (ae.getSource() == jbtnStart) ...
    This is a simple compare rather than the loop needed to compare strings.}
    Last edited by zweibieren; 10-28-2009 at 11:52 PM. Reason: add namby-pamby disclaimer

Similar Threads

  1. show watch
    By mahdi-farzami in forum CLDC and MIDP
    Replies: 7
    Last Post: 09-11-2009, 06:09 PM
  2. watch expression in debug mode
    By bizso in forum Eclipse
    Replies: 6
    Last Post: 07-08-2009, 09:07 AM
  3. Watch TV on PC - 12,000 TV Channels and Movies, save money Get $10 rebate.
    By arturmoniswork in forum Reviews / Advertising
    Replies: 0
    Last Post: 12-30-2008, 06:56 AM
  4. [SOLVED] Folder watch Issue
    By jazz2k8 in forum New To Java
    Replies: 13
    Last Post: 07-24-2008, 06:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •