counter

public class Counter implements Runnable {

    JLabel outputLabel;
    private int value;
    Thread thread;
    private boolean start = true;

    /** Creates a new instance of Counter */
    public Counter() {
        setValue(0);
    }

    /** Creates a new instance of Counter */
    public Counter(JLabel output) {
        outputLabel = output;
        setValue(0);
        thread = new Thread(this);

        thread.start();
    }

    public void run() {
        while (isStart()) {
            outputLabel.setText("Counter: " + getValue());
            try {
                thread.sleep(500);
                setValue(getValue() + 1);
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }
    }

    public boolean isStart() {
        return start;
    }

    public void setStart(boolean start) {
        this.start = start;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

Komentar

Postingan populer dari blog ini

Menyesap Rindu Dalam Coding (Sastra Edan #7)

Bikin Amplop Cantik

Kost Anyar Nyah...