暂停按钮java

时间:2015-05-15 17:30:35

标签: java multithreading

我想设置"暂停"按钮,以便在我第一次点击时暂停我的线程。如果我再次按下该按钮,线程应该恢复。我设置了一个布尔值" marche"每当我改变它时它应该控制我的线程。

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

public class PauseButton {
   static int count = 0;
   static boolean marche = true;
   static Thread mythread;
   static JButton button = new JButton("Pause");

   public static void main(String[] args) {
      JFrame fen = new JFrame("EDT");
      fen.getContentPane().add(button);
      fen.setSize(200, 100);
      fen.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      fen.setLocationRelativeTo(null);
      fen.setVisible(true);
      updateBouton();
      setButtonLis();
   }

   public static void updateBouton() {
      mythread = new Thread() {
         public void run() {
            for (int i = 0; i < 100; i++) {
               while (marche) {
                  try {
                     Thread.sleep(1000);
                  } catch (InterruptedException e) {
                     e.printStackTrace();
                  }
                  button.setText("Pause " + ++count);
               }
            }
         }
      };

      mythread.start();

   }

   static public void setButtonLis() {

      button.addMouseListener(new MouseListener() {

         @Override
         public void mouseClicked(MouseEvent arg0) {

            if (marche)
               marche = false;
            else
               marche = true;
            new Thread() {
               public void run() {
                  System.out.print(marche + " ");
               }
            }.start();
         }

         @Override
         public void mouseEntered(MouseEvent arg0) {
         }

         @Override
         public void mouseExited(MouseEvent arg0) {
         }

         @Override
         public void mousePressed(MouseEvent arg0) {
         }

         @Override
         public void mouseReleased(MouseEvent arg0) {
            // button.setText("Pause");
         }
      });

   }
}

2 个答案:

答案 0 :(得分:1)

将您的updateBouton代码更改为以下内容...

public static void updateBouton() {
    th = new Thread() {
        public void run() {
//            for (int i = 0; i < 100; i++) {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if(marche) {
                        bouton.setText("Pause " + ++count);
                    }
                }
//            }
        }
    };
    th.start();
}

当你点击按钮然后变量marche为假所以线程中的while循环关闭并且线程完成了它的执行,所以创建一个无限的while循环并检查boolean来更新按钮上的文本..所以没有必要对于for(int i=0;i<100;i++)循环..

答案 1 :(得分:0)

也许这就是你正在寻找的东西。     公共课开始{

    public static WhateverThread thread;
    public static boolean isPaused = false;

    public static void main(String...strings){
        thread = new WhateverThread();
        thread.start(); 
        JFrame frame = new JFrame("pausebutton test");

        frame.setSize(200, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setLayout(null);

        JPanel content = new JPanel();

        content.setSize(frame.getSize());
        content.setLayout(null);

        JButton pause = new JButton("Pause");

        pause.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (isPaused) {
                    thread.resumeThread();
                    System.out.println("Thread runs");
                } else {
                    try {
                        thread.pauseThread();
                        System.out.println("Thread waits");
                    } catch (InterruptedException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                }

                isPaused = !isPaused;
            }

        });
        content.add(pause);
        pause.setLocation(10, 10);
        pause.setSize(100, 20);
        frame.add(content);
        frame.setVisible(true);
    }

    static class WhateverThread extends Thread{

        private boolean isRunning = true;

        @Override
        public void run() {
            System.out.print("I am running...");
        }

        public void pauseThread() throws InterruptedException
        {
            isRunning = false;
        }

        public void resumeThread()
        {
            isRunning = true;
        }
    }
}

类和变量只是静态的,因为这是一个快速而又脏的片段,我想从主方法中调用所有内容。