Хелпикс

Главная

Контакты

Случайная статья





Приклад 1.



 

      import javax. swing. *;

      import java. awt. event. *;

      import java. awt. *;

      import com. bruceeckel. swing. *;

 

public class Counter1 extends JApplet {

private int count = 0;

private JButton

      start = new JButton (“Почати”),

       on0ff = new JButton (“Переключити”);

private JTextField t = new JTextField(10);

private  boolean runFlag = true;

public void init() {

       Container cp = getContentPane();

       cp. setLayout (new FlowLayout());

       cp. add(t);

       start. addActionLisener(new StartL());

       cp. add(start);

       on0ff. addActionLisener(new 0n0ffL());

       cp. add(on0ff);

       }

    public void go()   {

         while (true) {

             try {

               Thread. sleep(100); }

     catch(InterruptedException e) { 

        System. err. println(“Виконання перервано”);

        }

      if (runFlag)

          t. setText(Integer. toString(count++));

         }

       }

    class StartL implements ActionListener {

          public void actionPerformed(ActionEvent e) {

               go();

           }

         }

       class 0n0ffL implements ActionListener {

             public void actionPerformed(ActionEvent e) {

                    runFlag =! runFlag;

           }

          }

public static void main(Sting[] args) {

       Console. run(new Counter1(), 300, 100);

   }

}

 

Приклад 2.         Простий приклад з використанням потоків.

 

    public class SimpleThread extends Thread {

        private int countdown = 5;

        private static int threadCount = 0;

         private int threadNumber = ++threadCount;

         public SimpleThread() {

                 System. out. println(“Cтворення ” + threadNumber);

             }

         public void run() {

                while (true) {

                System. out. println(“Потік ” + threadNumber + “(“ + countdown + “)”);

              if (--countDown == 0) return;

                         }

                      }

                   public static void main(Sting[] args) {

                      for (int i = 0; i< 5; i++)

                          new  SimpleThread(). start();

                              System. out. println(“Усі потоки запущені ”);

                            }

                       }

 

      Приклад 3. Чутливий користувацький інтерфейс з потоками

 

      import javax. swing. *;

      import java. awt. event. *;

      import java. awt. *;

      import com. bruceeckel. swing. *;

 

         public class Counter2 extends JApplet {

         private class SeparateSubTask extends Thread {

                     private int count = 0;

                     private boolean runFlag = true;

                     SeparateSubTask() { start(); }

                      void  invertFlag() { runFlag =! runFlag; }

                      public void run() {

                           while (true) {

                              try {

                                     sleep(100);

                               }

                       catch(InterruptedException e) { 

        System. err. println(“ Перервано”); }

         if (runFlag)

              t. setText(Integer. toString(count++));

         }

       }

       }

       private SeparateSubTask sp = null;

       private JTextField t = new JTextField(10);

       private JButton

                     start = new JButton (“Почати”),

                     on0ff = new JButton (“Переключити”);

          class StartL implements ActionListener {

          public void actionPerformed(ActionEvent e) {

                   if (sp == null)

                         sp = new SeparateSubTask();

                    }

                  }

                class 0n0ffL implements ActionListener {

          public void actionPerformed(ActionEvent e) {

                   if (sp! == null)

                         sp. invertFlag();

                      }

                    }

       public void init() {

       Container cp = getContentPane();

       cp. setLayout (new FlowLayout());

       cp. add(t);

       start. addActionLisener(new StartL());

       cp. add(start);

       on0ff. addActionLisener(new 0n0ffL());

       cp. add(on0ff);  

       }

         public static void main(Sting[] args) {

       Console. run(new Counter1(), 300, 100);

   }

}

 

Приклад 4.   Використання інтерфейсу Runnable для перетворення головного класу в потік

     import javax. swing. *;

      import java. awt. event. *;

      import java. awt. *;

      import com. bruceeckel. swing. *;

 

         public class Counter3 extends JApplet implements Runnable {

         private int count = 0;

         private boolean runFlag = true;

         private Thread selfThread = null;

            private JButton

                 start = new JButton (“Почати”),

                 on0ff = new JButton (“Переключити”);

            private JTextField t = new JTextField(10);

               public void run() {

                           while (true) {

                              try {

                                     selfThread. sleep(100);

                               }

                       catch(InterruptedException e) { 

                       System. err. println(“ Перервано”); }

                        if (runFlag)

                        t. setText(Integer. toString(count++));

                                }

                             }

          class StartL implements ActionListener {

          public void actionPerformed(ActionEvent e) {

                   if (selfThread == null) {

                         selfThread = new Thread(Counter3. this);

                         selfThread. start(); }

                    }

                  }

                class 0n0ffL implements ActionListener {

                public void actionPerformed(ActionEvent e) {

                 runFlag =! runFlag;

                       }

                    }

       public void init() {

       Container cp = getContentPane();

       cp. setLayout (new FlowLayout());

       cp. add(t);

       start. addActionLisener(new StartL());

       cp. add(start);

       on0ff. addActionLisener(new 0n0ffL());

       cp. add(on0ff);  

       }

         public static void main(Sting[] args) {

       Console. run(new Counter1(), 300, 100);

   }

}

 

       

 



  

© helpiks.su При использовании или копировании материалов прямая ссылка на сайт обязательна.