/*
import java.awt.*;
import javax.swing.*;
class TimerThread extends Thread {
int n = 0;
private JLabel timerLabel;
public TimerThread(JLabel timerLabel,int n) {
this.timerLabel = timerLabel;
this.n=n;
}
public void run() {
while (true) {
timerLabel.setText(Integer.toString(n));
n+=2;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main() {
setTitle("에제13-1");
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel1 = new JLabel();
timerLabel1.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel1);
JLabel timerLabel2 = new JLabel();
timerLabel2.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel2);
TimerThread th1 = new TimerThread(timerLabel1,0);
TimerThread th2 = new TimerThread(timerLabel2,1);
setSize(300,170);
setVisible(true);
th1.start();
th2.start();
}
public static void main(String[] args) {
new Main();
}
}
import java.awt.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
int n=0;
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel,int n) {
this.timerLabel=timerLabel;
this.n=n;
}
public void run() {
while(true) {
timerLabel.setText(Integer.toString(n));
n+=2;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame {
public Main(){
setTitle("예제 13-2");
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel1 = new JLabel();
timerLabel1.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel1);
JLabel timerLabel2 = new JLabel();
timerLabel2.setFont(new Font("Gothic",Font.ITALIC,80));
c.add(timerLabel2);
TimerRunnable runnable1 = new TimerRunnable(timerLabel1,0);
TimerRunnable runnable2 = new TimerRunnable(timerLabel2,1);
Thread th1 = new Thread(runnable1);
Thread th2 = new Thread(runnable2);
setSize(250,150);
setVisible(true);
th1.start();
th2.start();
}
public static void main(String[] args) {
new Main();
}
}*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
int n=0;
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel=timerLabel;
}
public void run() {
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class Main extends JFrame{
private Thread th;
JButton b = new JButton("Stop!!");
public Main() {
setTitle("예제13-5");
setSize(300,170);
setVisible(true);
Container c = getContentPane();
c.setLayout(new FlowLayout());
JLabel timerLabel= new JLabel();
timerLabel.setFont(new Font("Gothic",Font.ITALIC,80));
TimerRunnable runnable = new TimerRunnable(timerLabel);
th = new Thread (runnable);
c.add(timerLabel);
th.start();
c.add(b);
b.addActionListener(new MyActionListener());
}
class MyActionListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
th.interrupt(); // 쓰레드 종료
b.setEnabled(false); //버튼 못누르게
}
}
public static void main(String[] args) {
new Main();
}
}



