728x90
반응형
📃 열거형 예제
📝 EnumTest 클래스
public class EnumTest {
public static void main(String[] args) {
final int EAST = 1;
final int WEST = 2;
final int SOUTH = 3;
final int NORTH = 4;
int origin = EAST; // 동쪽으로 초기화
origin = SOUTH; // 남쪽으로 대입
if(origin == WEST) {} // 서쪽이면
if(origin != NORTH) {} // 북쪽이 아니면
}
}
📃 스레드 예제(1)
📝 ThreadTest 클래스
class ThreadTest {
public static void main(String[] args) {
// 메인 스레드
// java.lang 패키지에서 지원해주는 PrintThread클래스와 start메서드
PrintThread worker = new PrintThread();
// 스레드 시작
worker.start();
for(int num = 0; num < 30; num++) {
System.out.print("O");
// sleep메서드 1/1000초 단위
try { Thread.sleep(200); } // 예외가 발생할만한 부분을 try로 묶기
catch (InterruptedException e) {;} // InterruptedException이 발생시 예외 처리
}
}
}
class PrintThread extends Thread {
// 작업 스레드
// 스레드 실행
public void run() {
for(int num = 0; num < 30; num++) {
System.out.print("X");
try { Thread.sleep(10); }
catch (InterruptedException e) {;}
}
}
}
📃 스레드 예제(2)
📝 ThreadTest2 클래스
// 싱글 스레드
class Threadtest2 {
public static void main(String[] args) {
for(int num = 0; num < 30; num++) {
System.out.print("O");
try {Thread.sleep(200);}
catch(InterruptedException e) {;}
}
for(int num = 0; num <30; num++) {
System.out.print("X");
try{Thread.sleep(100);}
catch(InterruptedException e) {;}
}
}
}
📃 스레드 예제(3)
📝 ThreadTest2 클래스
class ThreadTest2 {
public static void main(String[] args) {
for(int num = 0; num < 30; num++) {
System.out.print("O");
}
for(int num = 0; num <30; num++) {
System.out.print("X");
}
}
}
📃 스레드 예제(4) - 싱글 스레드
📝 ThreadTest2 클래스
// 싱글 스레드
class ThreadTest2 {
public static void main(String[] args) {
for(int num = 0; num < 30; num++) {
try{Thread.sleep(200);}
catch(InterruptedException e) {;}
}
for(int num = 0; num <30; num++) {
try{Thread.sleep(100);}
catch(InterruptedException e) {;}
}
System.out.print("X");
}
}
📃 스레드 예제(5)
📝 ThreadTest3, PrintThread, BeepThread 클래스
import java.awt.Toolkit;
class ThreadTest3 {
public static void main(String[] args) {
PrintThread worker1 = new PrintThread('X');
worker1.start();
PrintThread worker2 = new PrintThread('.');
worker2.start();
BeepThread beep = new BeepThread(5, 1000); // 1000 -- 스레드 속도
beep.start();
for(int num = 0; num < 30; num++) {
System.out.print("O");
try{Thread.sleep(200);} catch(InterruptedException e ) {;}
}
}
}
class PrintThread extends Thread {
char ch;
PrintThread(char ch) {
super();
this.ch = ch;
}
public void run() {
for(int num=0; num<30; num++) {
System.out.print(ch);
try{Thread.sleep(100);} catch(InterruptedException e) {;}
}
}
}
class BeepThread extends Thread {
int count;
int gap;
Toolkit tool = Toolkit.getDefaultToolkit();
BeepThread(int count, int gap) {
this.count = count;
this.gap = gap;
}
public void run() {
for(int num=0; num<count; num++) {
tool.beep();
try {Thread.sleep(gap);} catch(InterruptedException e) {;}
}
}
}
📃 스레드 예제(6) + 자바 스윙
📝 FlickeringLabel, FlickeringLabelEx 클래스
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable{
public FlickeringLabel(String text) {
super(text); // JLabel생성자호출
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(500); // 0.5초 동안 잠을 잔다.
}
catch(InterruptedException e){
return;
}
}
}
}
public class FlickeringLabelEx extends JFrame{
public FlickeringLabelEx() {
setTitle("FlickeringLabelEx예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// 깜박이는 레이블 생성
FlickeringLabel fLabel= new FlickeringLabel("깜박");
// 깜박이지 않는 레이블 생성
JLabel label = new JLabel("안깜박");
// 깜박이는 레이블 생성
FlickeringLabel fLabel2 = new FlickeringLabel("여기도 깜박");
c.add(fLabel);
c.add(label);
c.add(fLabel2);
setSize(300,150);
setVisible(true);
}
public static void main(String[] args) {
new FlickeringLabelEx();
}
}
📃 스레드 예제(7) + 자바 스윙
📝 FlickeringLabel, FlickeringLabelEx 클래스
import java.awt.*;
import javax.swing.*;
class FlickeringLabel extends JLabel implements Runnable {
public FlickeringLabel(String text) {
super(text); // JLabel생성자호출
setOpaque(true); // 배경색 변경이 가능하도록 설정
Thread th = new Thread(this);
th.start();
}
public void run() {
int n = 0;
while (true) {
if (n == 0) {
setBackground(Color.YELLOW);
n = 1;
}
else if(n == 1) {
setBackground(Color.RED);
n = 2;
}
else {
setBackground(Color.GREEN);
n = 0;
}
try {
Thread.sleep(500); // 0.5초 동안 잠을 잔다.
} catch (InterruptedException e) {
return;
}
}
}
}
public class FlickeringLabelEx extends JFrame {
public FlickeringLabelEx() {
setTitle("FlickeringLabelEx예제");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container c = getContentPane();
c.setLayout(new FlowLayout());
// 깜박이는 레이블 생성
FlickeringLabel fLabel = new FlickeringLabel("깜박");
// 깜박이지 않는 레이블 생성
JLabel label = new JLabel("안깜박");
// 깜박이는 레이블 생성
FlickeringLabel fLabel2 = new FlickeringLabel("여기도 깜박");
c.add(fLabel);
c.add(label);
c.add(fLabel2);
setSize(300, 150);
setVisible(true);
}
public static void main(String[] args) {
new FlickeringLabelEx();
}
}
728x90
반응형
'Language > JAVA' 카테고리의 다른 글
JAVA 12일차 - 실습문제 02 수정 (0) | 2022.07.06 |
---|---|
JAVA 12일차 - 실습문제 02 (0) | 2022.07.05 |
JAVA 12일차 - 컬렉션, 제네릭 (0) | 2022.07.05 |
JAVA 11일차 - 실습 문제 01 수정 (0) | 2022.07.05 |
JAVA 11일차 - 실습 문제 01 (0) | 2022.07.04 |
댓글