Vamshi Rakela
Vamshi Rakela

Reputation: 43

Button b2 =stop is not listening

I have added two buttons in stopclock application; one for start and another for stop. Start is working, but stop is not. I have used a thread for this.

Also the code is creating two frames, how to avoid that?

We have four classes: one main and two for implementing actionListeners for two buttons, and another for implementing threads.

import java.awt.*;
import java.awt.event.*;
public class stopclock 
{
Frame f;
Panel p;
Button b1,b2;
Label l;
stopclock(boolean k)
{
    f=new Frame();
    p=new Panel();
    b1=new Button("Start");
    b2=new Button("Stop");
    l=new Label("00:00:00");
    p.add(l);
    p.add(b1);
    p.add(b2);
    f.add(p);
    f.setTitle("Stop Clock");
    f.setSize(300,300);
    f.setVisible(true);
    b1.addActionListener(new one());
    b2.addActionListener(new two());
}
public static void main(String[] args) 
{
    stopclock s=new stopclock(false);
}
}
class one  implements ActionListener
{

//Thread th=new Thread();


public void actionPerformed(ActionEvent ae)
{
    new th().start();

}
}

class two implements ActionListener
{
public void actionPerformed(ActionEvent ae)
{

    th run=new th();
    Thread anb=new Thread(run);
    anb.suspend();
}

}
class th extends Thread
{
    int sec=0;
    int hr=0;
    int min=0;
    public void run()
    {
        stopclock st=new stopclock(true);
        //System.out.println("Hello");

        //st.f.setVisible(true);
        Label l2=st.l;
        try
        {

            while(true)
            {
                if(sec>59)
                {
                    min++;
                    sec=0;
                }
                if(min>59)
                {
                    hr++;
                    min=0;
                }
                if(hr>12)
                {
                    hr=0;
                }
                sec++;
                String str=hr+":"+min+":"+sec;
                l2.setText(str);



                th.sleep(1000);
            }
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
}

Upvotes: 1

Views: 76

Answers (1)

user1803551
user1803551

Reputation: 13427

I changed your components to Swing components. You are getting 2 frames because inside your thread you are creating a new one with stopclock st = new stopclock(true). Furthermore, you are going to have a lot of trouble using Thread for this. I used a Swing Timer instead:

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

public class StopClock {

    JLabel label = new JLabel("00:00:00");;
    Timer timer = new Timer(1000, new TimerListener());

    StopClock() {

        JButton startButton = new JButton("Start");
        JButton stopButton = new JButton("Stop");
        startButton.addActionListener(new StartActionListener());
        stopButton.addActionListener(new StopActionListener());

        JPanel panel = new JPanel();
        panel.add(label);
        panel.add(startButton);
        panel.add(stopButton);

        JFrame frame = new JFrame();
        frame.add(panel);
        frame.setTitle("Stop Clock");
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {

        EventQueue.invokeLater(() -> new StopClock());
    }

    class StartActionListener implements ActionListener {

        public void actionPerformed(ActionEvent ae) {

            timer.start();
        }
    }

    class StopActionListener implements ActionListener {

        public void actionPerformed(ActionEvent ae) {

            timer.stop();
        }
    }

    class TimerListener implements ActionListener {

        int sec = 0;
        int hr = 0;
        int min = 0;

        @Override
        public void actionPerformed(ActionEvent e) {

            if (sec > 59) {
                min++;
                sec = 0;
            }
            if (min > 59) {
                hr++;
                min = 0;
            }
            if (hr > 12) {
                hr = 0;
            }
            sec++;
            String str = hr + ":" + min + ":" + sec;
            label.setText(str);
        }
    }
}

There are some things which can be improved, but I tried to keep it close to what you wrote.

Notes:

  • Class names should start with an uppercase.
  • Give meaningful names to your variables.
  • Removed the boolean argument from the constructor.
  • Use pack() instead of setSize(...).
  • Best to call setVisible(...) as the last thing you do.
  • Don't use fields when local variables will do (e.g., the buttons).

Upvotes: 1

Related Questions