Write a JAVA program Design a screen with two buttons start thread and stop thread. Clicking on start ,it should start printing “Thread running” until stop button is pressed.

import java.awt.*;
import java.awt.event.*;

public class s19 extends Frame implements

ActionListener,Runnable
 {
   Button start,stop;
                 TextField tf;
      int x=0,y=0;
     String msg="";
   Thread t1=new Thread(this);
      public s19()
       {
             setLayout(new FlowLayout());
             start=new Button("start");
             stop=new Button("stop");
            add(start);
            add(stop);

         start.addActionListener(this);
         stop.addActionListener(this);
                  addWindowListener(new

WindowAdapter()
                     {
                        public void

windowClosing(WindowEvent e)
                         {
                           System.exit(0);
                            }
                      });
                     
                     setSize(200,200);
                     setVisible(true);
              }
 
  public void actionPerformed(ActionEvent ae)
     {
             Button btn=(Button)ae.getSource();
              if(btn==start)
              {
                  t1.start();
                 }
       

      if(btn==stop)
              {
                  t1.stop();
                 }
}


public void run()
 {
   try
   {
      while(true)
       {
          repaint();
          Thread.sleep(350);
         }
  }


catch(Exception e)
{
  }
 }

public void paint(Graphics g)
  {
   msg=msg+"thread running";
  g.drawString(msg,10,y+=10);
    }

  public static void main(String args[])
    {
     new s19();
      }
   }

No comments:

Post a Comment