O que estou tentando fazer é o seguinte, criar uma janela com dois botões um “inicia” e o outro “para” a execução de um laço de repetição. Quando clico no botão “parar” ele até termina o laço, mas o programa continua executando, como faço para terminar o programa quando clicar no botão “parar” ?
Sou iniciante em java estou cursando o segundo semestre na faculdade se o código estiver ruim por favor entendam kkkk.
Segue o código abaixo caso alguém possa me ajudar.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class Botoes extends Thread implements ActionListener{
//CRIA UM OBJETO DA CLASSE BOTAO. PARA CRIAR BOTOES
JButton bt_inicia = new JButton("Inicia");
JButton bt_para = new JButton("Para");
boolean sinalizador = false;
//AÇÕES PARA OS BOTÕES
public void actionPerformed(ActionEvent e){
//thread_inicio
Thread thread = new Thread(){
public void run() {
while(sinalizador == false){
try{
if(e.getSource() == bt_inicia){
System.out.println("Inicia");
Thread.sleep(500);
}
if(e.getSource() == bt_para){
sinalizador = true;
System.out.println("Parou");
}
}catch (Exception e1) {
e1.printStackTrace();
}
}//FIM WHILE
}//FIM RUN
};//fim_Thread
thread.start();
}//FIM_DECLARA AÇÕES PARA BOTÃO
public Botoes(){
JFrame jf = new JFrame();
//ADICIONANDO AÇÃO AO BOTÃO
bt_inicia.addActionListener(this);
bt_para.addActionListener(this);
//CRIAR BOTÕE COM AS ESPECIFICACOES ABAIXO
jf.setLayout(null);
bt_inicia.setBounds(80, 50, 100, 60);
jf.add(bt_inicia);
bt_para.setBounds(80,150,100,60);
jf.add(bt_para);
//FIM
//CRIA UMA JANELA COM AS ESPECIFICACOES ABAIXO
jf.setTitle("Minha Janela");
jf.setSize(300,300);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setResizable(true);
jf.setVisible(true);
//FIM
}//CLASS FIM