mojo's Blog
스레드 만들어보기 본문
Thread를 상속받아서 1초 단위로 출력하는 타이머 스레드 만들기
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class ThreadTimeEx extends JFrame{
class TimerThread extends Thread{
private JLabel timerLabel;
public TimerThread(JLabel timerLabel) {
this.timerLabel = timerLabel;
}
public void run() {
int n=0;
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
ThreadTimeEx(){
setTitle("13장 연습하기");
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(300, 200);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new ThreadTimeEx();
}
}
Runnable 인터페이스를 이용하여 1초 단위로 출력하는 타이머 스레드 만들기
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class RunnableTimerEx extends JFrame{
class TimerRunnable implements Runnable{
private JLabel timerLabel;
public TimerRunnable(JLabel timerLabel) {
this.timerLabel = timerLabel;
}
public void run() {
int n=0;
while(true) {
timerLabel.setText(Integer.toString(n));
n++;
try {
Thread.sleep(1000);
}
catch(InterruptedException e) {
return;
}
}
}
}
RunnableTimerEx(){
setTitle("13장 연습하기");
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(300, 200);
setVisible(true);
th.start();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new RunnableTimerEx();
}
}
깜빡이는 문자열을 가진 레이블 만들기
import javax.swing.*; // JFrame
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*; // Container
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionAdapter;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import java.awt.*;
public class FlickeringLabelEx extends JFrame{
class FlickeringLabel extends JLabel implements Runnable{
private long delay;
FlickeringLabel(String text, long delay){
super(text);
this.delay = delay;
setOpaque(true);
Thread th = new Thread(this);
th.start();
}
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;
}
}
}
}
FlickeringLabelEx(){
setTitle("13장 연습하기");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
FlickeringLabel fLabel1 = new FlickeringLabel("깜박",500);
JLabel label = new JLabel("안깜빡");
FlickeringLabel fLabel2 = new FlickeringLabel("여기도 깜빡",300);
c.add(fLabel1);
c.add(label);
c.add(fLabel2);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new FlickeringLabelEx();
}
}
'Java' 카테고리의 다른 글
Thread Synchronization (0) | 2021.08.09 |
---|---|
스레드 정보 출력 / 타이머 스레드 강제 종료 / flag를 이용한 스레드 종료 (0) | 2021.08.09 |
Multitasking (0) | 2021.08.08 |
명품 JAVA Programming 제 12장 실습문제 (3~4 제외) (0) | 2021.08.04 |
명품 JAVA Programming 제 12장 Open Challenge (0) | 2021.08.04 |
Comments