Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用按钮停止和启动循环_Java_Swing_Button_While Loop - Fatal编程技术网

Java 用按钮停止和启动循环

Java 用按钮停止和启动循环,java,swing,button,while-loop,Java,Swing,Button,While Loop,我试图控制我的程序中的while循环,以根据用户输入停止和启动。我用一个按钮尝试了这一点,它的“开始”部分工作,但是代码进入了一个无限循环,如果不手动终止它,我就无法停止。以下是我的全部代码: 标题类 package test; import javax.swing.JFrame; public class headerClass { public static void main (String[] args){ frameClass

我试图控制我的程序中的while循环,以根据用户输入停止和启动。我用一个按钮尝试了这一点,它的“开始”部分工作,但是代码进入了一个无限循环,如果不手动终止它,我就无法停止。以下是我的全部代码: 标题类

package test;

    import javax.swing.JFrame;

    public class headerClass {
        public static void main (String[] args){
            frameClass frame = new frameClass();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150,75);
            frame.setVisible(true);
        }
    }
package test;

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

public class frameClass extends JFrame {

    private JButton click;

    public frameClass(){
        setLayout(new FlowLayout());
        click = new JButton("Stop Loop");
        add(click);

        thehandler handler = new thehandler();
        click.addActionListener(handler);
    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==click){
                looper loop = new looper();
                looper.buttonSet = !looper.buttonSet;
            }
        }
    }
}
package test;

public class looper {
    public static boolean buttonSet;

    public looper(){
        while (buttonSet==false){
            System.out.println("aaa");
        }       
    }
}
框架类

package test;

    import javax.swing.JFrame;

    public class headerClass {
        public static void main (String[] args){
            frameClass frame = new frameClass();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150,75);
            frame.setVisible(true);
        }
    }
package test;

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

public class frameClass extends JFrame {

    private JButton click;

    public frameClass(){
        setLayout(new FlowLayout());
        click = new JButton("Stop Loop");
        add(click);

        thehandler handler = new thehandler();
        click.addActionListener(handler);
    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==click){
                looper loop = new looper();
                looper.buttonSet = !looper.buttonSet;
            }
        }
    }
}
package test;

public class looper {
    public static boolean buttonSet;

    public looper(){
        while (buttonSet==false){
            System.out.println("aaa");
        }       
    }
}
循环类

package test;

    import javax.swing.JFrame;

    public class headerClass {
        public static void main (String[] args){
            frameClass frame = new frameClass();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(150,75);
            frame.setVisible(true);
        }
    }
package test;

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

public class frameClass extends JFrame {

    private JButton click;

    public frameClass(){
        setLayout(new FlowLayout());
        click = new JButton("Stop Loop");
        add(click);

        thehandler handler = new thehandler();
        click.addActionListener(handler);
    }

    private class thehandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
            if(e.getSource()==click){
                looper loop = new looper();
                looper.buttonSet = !looper.buttonSet;
            }
        }
    }
}
package test;

public class looper {
    public static boolean buttonSet;

    public looper(){
        while (buttonSet==false){
            System.out.println("aaa");
        }       
    }
}

如果进入无限循环,如何修复此问题并停止?

Swing是一个单线程框架,这意味着在循环运行时,事件调度线程被阻止,无法处理新事件,包括重新绘制请求

您需要在自己的线程上下文中启动Looper类。这也意味着您的循环标志需要声明为
volatile
,或者您应该使用
AtomicBoolean
,以便跨线程边界检查和修改状态

例如

public class Looper implements Runnable {

    private AtomicBoolean keepRunning;

    public Looper() {
        keepRunning = new AtomicBoolean(true);
    }

    public void stop() {
        keepRunning.set(false);
    }

    @Override
    public void run() {
        while (keepRunning.get()) {
            System.out.println("aaa");
        }
    }

}
那么你也许可以使用像

private class thehandler implements ActionListener {

    private Looper looper;

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == click) {
            if (looper == null) {
                looper = new Looper();
                Thread t = new Thread(looper);
                t.start();
            } else {
                looper.stop();
                looper = null;
            }
        }
    }
}
要运行它

查看和了解更多详细信息


还要注意,Swing不是线程安全的,您永远不应该从EDT上下文的外部创建或修改UI。问题是您启动了一个无限循环,并试图在同一线程中终止它。这不起作用,因为VM在一个线程中执行一个又一个任务。它将在循环结束后直接执行命令停止
循环器
,但无限循环永远不会结束。所以它不能像这样被阻止。
您需要为活套创建第二个
线程。这样,您就可以从主线程停止它

你必须在一个单独的线程中启动“looper”。只是一个注释。。。Java中的类名应该是PascalCase,而不是camelCase。