import java.awt.*;
import javax.swing.*;
class TimerThread extends Thread {
private JLabel timerLabel; // 타이머 값이 출력되는 레이블
public TimerThread(JLabel timerLabel) {
this.timerLabel = timerLabel;
}
// 스레드 코드. run()이 종료하면 스레드 종료
@Override
public void run() {
int n=0; // 타이머 카운트 값
while(true) { // 무한 루프
timerLabel.setText(Integer.toString(n));
n++; // 카운트 증가
try {
Thread.sleep(1000); // 1초동안 잠을 잔다.
}
catch(InterruptedException e) { return;}
}
}
}
public class ThreadTimerEx extends JFrame {
public ThreadTimerEx() {
setTitle("Thread를 상속받은 타이머 스레드 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// 타이머 값을 출력할 레이블 생성
JLabel timerLabel = new JLabel();
timerLabel.setFont(new Font("Gothic", Font.ITALIC, 80));
c.add(timerLabel);
TimerThread th = new TimerThread(timerLabel);
setSize(250,150);
setVisible(true);
th.start(); // 타이머 스레드의 실행을 시작하게 한다.
}
public static void main(String[] args) {
new ThreadTimerEx();
}
}
import java.awt.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel = timerLabel;
}
// 스레드 코드. run()이 종료하면 스레드 종료
@Override
public void run() {
int n=0; // 타이머 카운트 값
while(true) { // 무한 루프
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000); // 1초동안 잠을 잔다.
}
catch(InterruptedException e) { return; }
}
}
}
public class ThreadEx extends JFrame {
public ThreadEx() {
setTitle("Runnable을 구현한 타이머 스레드 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// 타이머 값을 출력할 레이블 생성
JLabel timerLabel = new JLabel();
timerLabel.setFont(new Font("Gothic", Font.ITALIC, 80));
c.add(timerLabel); // 레이블을 컨텐트팬에 부착
TimerRunnable runnable = new TimerRunnable(timerLabel);
Thread th = new Thread(runnable); // 스레드 객체 생성
setSize(250,150);
setVisible(true);
th.start(); // 타이머 스레드가 실행을 시작하게 한다.
}
public static void main(String[] args) {
new ThreadEx();
}
}
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable {
private long delay;
public FlickeringLabel(String text, long delay) {
super(text);
this.delay = delay;
setOpaque(true);
Thread th = new Thread(this);
th.start();
}
@Override
public void run() {
int n=0;
while(true) {
if(n == 0)
setBackground(Color.YELLOW);
else
setBackground(Color.GREEN);
if(n == 0) n = 1;
else n = 0;
try {
Thread.sleep(delay);
}
catch(InterruptedException e) {
return;
}
}
}
}
public class ThreadEx extends JFrame {
public ThreadEx() {
setTitle("FlickeringLabelEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// 깜박이는 레이블 생성
FlickeringLabel fLabel = new FlickeringLabel("깜박",500);
// 깜박이지 않는 레이블 생성
JLabel label = new JLabel("안깜박");
// 깜박이는 레이블 생성
FlickeringLabel fLabel2 = new FlickeringLabel("여기도 깜박",300);
c.add(fLabel);
c.add(label);
c.add(fLabel2);
setSize(300,150);
setVisible(true);
}
public static void main(String[] args) {
new ThreadEx();
}
}
- ThreadInterruptEx
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class TimerRunnable implements Runnable {
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel = timerLabel;
}
@Override
public void run() {
int n=0;
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000); // 1초 동안 잠을 잔다.
}
catch(InterruptedException e) {
return; // 예외가 발생하면 스레드 종료
}
}
}
}
public class ThreadEx extends JFrame {
private Thread th;
public ThreadEx() {
setTitle(" hreadInterruptEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
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);
// 버튼을 생성하고 Action 리스너 등록
JButton btn =new JButton("kill Timer");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
th.interrupt(); // 타이머 스레드 강제 종료
JButton btn = (JButton)e.getSource();
btn.setEnabled(false); // 버튼 비활성화
}
});
c.add(btn);
setSize(300,170);
setVisible(true);
th.start(); // 스레드 동작시킴
}
public static void main(String[] args) {
new ThreadEx();
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class RandomThread extends Thread {
private Container contentPane;
private boolean flag=false; // 스레드의 종료 명령을 표시하는 플래그. true : 종료 지시
public RandomThread(Container contentPane) {
this.contentPane = contentPane;
}
void finish() { // 스레드 종료 명령을 flag에 표시
flag = true;
}
@Override
public void run() {
while(true) {
int x = ((int)(Math.random()*contentPane.getWidth()));
int y = ((int)(Math.random()*contentPane.getHeight()));
JLabel label = new JLabel("Java"); //새 레이블 생성
label.setSize(80, 30);
label.setLocation(x, y);
contentPane.add(label);
contentPane.repaint();
try {
Thread.sleep(300); // 0.3초 동안 잠을 잔다.
if(flag==true) {
contentPane.removeAll();
label = new JLabel("finish");
label.setSize(80, 30);
label.setLocation(100, 100);
label.setForeground(Color.RED);
contentPane.add(label);
contentPane.repaint();
return; // 스레드 종료
}
}
catch(InterruptedException e) { return; }
}
}
}
public class ThreadEx extends JFrame {
private RandomThread th; // 스레드 레퍼런스
public ThreadEx() {
setTitle("ThreadFinishFlagEx 예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
c.addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
th.finish(); // RandomThread 스레드 종료 명령
}
});
setSize(300,200);
setVisible(true);
th = new RandomThread(c); // 스레드 생성
th.start(); // 스레드 동작시킴
}
public static void main(String[] args) {
new ThreadEx();
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyLabel extends JLabel {
private int barSize = 0; // 바의 크기
private int maxBarSize;
public MyLabel(int maxBarSize) {
this.maxBarSize = maxBarSize;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.MAGENTA);
int width = (int)(((double)(this.getWidth()))
/maxBarSize*barSize);
if(width==0) return;
g.fillRect(0, 0, width, this.getHeight());
}
synchronized void fill() {
if(barSize == maxBarSize) {
try {
wait();
} catch (InterruptedException e) { return; }
}
barSize++;
repaint(); // 바 다시 그리기
notify();
}
synchronized void consume() {
if(barSize == 0) {
try {
wait();
} catch (InterruptedException e)
{ return; }
}
barSize--;
repaint(); // 바 다시 그리기
notify();
}
}
class ConsumerThread extends Thread {
private MyLabel bar;
public ConsumerThread(MyLabel bar) {
this.bar = bar;
}
@Override
public void run() {
while(true) {
try {
sleep(200);
bar.consume();
} catch (InterruptedException e)
{ return; }
}
}
}
public class ThreadEx extends JFrame {
private MyLabel bar = new MyLabel(100);
public ThreadEx(String title) {
super(title);
this.setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(null);
bar.setBackground(Color.ORANGE);
bar.setOpaque(true);
bar.setLocation(20, 50);
bar.setSize(300, 20);
c.add(bar);
c.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent e)
{
bar.fill();
}
});
setSize(350,200);
setVisible(true);
c.setFocusable(true);
c.requestFocus();
ConsumerThread th = new
ConsumerThread(bar);
th.start(); // 스레드 시작
}
public static void main(String[] args) {
new ThreadEx(
"아무키나 빨리 눌러 바 채우기");
}
}
'PROGRAM > JAVA' 카테고리의 다른 글
Intellij 시작하기 (0) | 2020.11.09 |
---|---|
JDK 1.8.0_271 설치하기 (0) | 2020.10.21 |
인터페이스 (0) | 2020.06.08 |
추상클래스 (0) | 2020.06.08 |
Circle Class (0) | 2020.05.05 |