12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import javax.swing.JButton;
import javax.swing.JFrame;
public class NovaClasse extends JFrame implements ActionListener {
boolean iniciado = true;
JButton jb = new JButton("Cancelado");
public NovaClasse() {
setVisible(true);
setSize(610, 200);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
setLayout(null);
jb.setBounds(240, 90, 120, 30);
add(jb);
jb.addActionListener(this);
jb.addKeyListener(new KeyAdapter() {
public void keyPressed(java.awt.event.KeyEvent e) {
if (e.getKeyCode() == java.awt.event.KeyEvent.VK_SCROLL_LOCK) {
Verificar();
}
};
});
}
public void Verificar() {
if (iniciado == true) {
jb.setText("Inciado");
loop();
iniciado = false;
} else {
jb.setText("Cancelado");
loop();
iniciado = true;
}
}
public void loop() {
new Thread() {
public void run() {
if (iniciado == true) {
while (true) {
System.out.println("Looping");
}
} else {
Thread.interrupted();
}
}
}.start();
}
public static void main(String[] args) {
new NovaClasse();
}
public void actionPerformed(ActionEvent e) {
Verificar();
}
}