2015-10-01 23 views
0

我一直在嘗試使它成爲buttonb(Stop)可以打破循環函數內部的循環。我只需要循環停止或被按鈕b(停止)按鈕中斷。謝謝。 :) [我試過尋找,但是,我的結果還沒有找到任何可以幫助我的結果。 ]另一個按鈕函數(Java)中的循環函數的中止(停止)按鈕

/* 
* 
*/ 

package stopwatch; 

import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JTextField; 

/** 
* 
* @author C 
*/ 

public class Stopwatch { 

    /** 
    * @param args the command line arguments 
    */ 

    private static int abc = 0; 

    private static void print (int x, String b) { 

     if (x == 69) { 

      System.out.println(b); 

     } 

     if (b == null) { 

      System.out.println(x); 

     } 

    } 

    private static void changea(int val) { 

     abc = val; 

    } 

    public static void main(String[] args) { 


     JFrame aeeb = new JFrame("Stopwatch : Start"); 
     aeeb.setVisible(true); 
     aeeb.setSize(50, 150); 

     JButton button = new JButton("Start"); 
     button.setVisible(true); 
     aeeb.add(button); 

     JFrame aeec = new JFrame("Stopwatch : Stop"); 
     aeec.setVisible(true); 
     aeec.setSize(50, 150); 

     final JButton buttonb = new JButton("Stop"); 
     buttonb.setVisible(true); 
     aeec.add(buttonb); 

     button.addActionListener(new ActionListener() { 

      int x = 1; 
      int msec = 0; 
      int sec = 0; 
      int min = 0; 
      int hour = 0; 

      public void actionPerformed(ActionEvent e) 

      { 

       if (x == 1) { 

        print(69, "Stopwatch is running!"); 
        x = 0; 

        int xd = 10; 

         while(xd < 20) { 

          if (msec == 1000) { 

           sec = sec + 1; 
           msec = 0; 

          } 

          if (sec == 60) { 

           min = min + 1; 
           sec = 0; 

          } 

          if (min == 60) { 

           hour = hour + 1; 
           min = 0; 

          } 

          msec++; 

          if (msec == 1000) { 

           sec = sec + 1; 
           msec = 0; 

          } 

          if (sec == 60) { 

           min = min + 1; 
           sec = 0; 

          } 

          if (min == 60) { 

           hour = hour + 1; 
           min = 0; 

          } 

         try { 
          Thread.sleep(1); 

         }  catch (InterruptedException ex) { 
          Logger.getLogger(Stopwatch.class.getName()).log(Level.SEVERE, null, ex); 
         } 

         } 

       } 

       if (x == 0) { 

        print(69, "Stopwatch is running already!"); 

       } 

      } 

     }); 

    } 
} 
+1

應該從來沒有**在事件處理程序中的一個長操作。你正在舉辦事件調度線程。此外,你的循環是一個無限循環,因爲'xd'永遠不會改變。您應該使用[javax.swing.Timer](http://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html)設計秒錶,並且始終僅將事件處理程序用於短操作比如在一個變量中設置一個值。 – RealSkeptic

回答

0

當按下停止按鈕時,使用一個標誌並將其設置爲false。

public class Stopwatch { 
... 
private boolean isRunning = false; 
... 

你的循環啓動按鈕動作裏面:

... 
    isRunning = true; 
    ... 
    while(isRunning) { 
    ... 

停止按鈕:

final JButton buttonb = new JButton("Stop"); 
    buttonb.setVisible(true); 
    aeec.add(buttonb); 
    aeec.addActionListener(new ActionListener(){ 
     isRunning = false; 
    }); 
0

也許做這樣的事情:

public class stopwatch{ 
    public boolean isOn = true; 

    // stuff 
    // loop: 
    if(!something.isOn){ 
     break; 
    } 
    // rest of loop 
} 

而且按鈕請按:

Stopwatch.isOn = false; 
相關問題