Paste ljSLZ

Submitted on 15 July 2016 at 20:46 UTC
856 views

Section 1 (java)

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();
    }
}

Raw · Download

Pastee