在另一个按钮函数(Java)

时间:2015-10-01 14:31:38

标签: java loops jbutton break

我一直试图让它成为按钮b(Stop)可以在循环函数内部打破循环。我只需要通过buttonb(停止)按钮停止或打破循环。谢谢。 :) [我已经尝试过了,但是,对我来说还没有找到任何可以帮助我的结果。 ]

/*
 * 
 */

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!");

                }

            }

        });

    }
}

2 个答案:

答案 0 :(得分: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;
   });

答案 1 :(得分:0)

也许做这样的事情:

public class stopwatch{
    public boolean isOn = true;

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

按下按钮:

Stopwatch.isOn = false;