명품자바 programming 13장 실습문제
2020. 12. 8. 06:24ㆍProgramming Language/JAVA
2번 3번 스레드가 동작을 안 해서 디버깅 돌리고 난리를 쳤는데
스레드 run() 안에 while문을 안 넣어줬던 거였다....
Aㅏ
1.
import java.util.Scanner; public class Ex01 { public Ex01() { Scanner scanner = new Scanner(System.in); System.out.print("아무거나 입력"); scanner.nextLine(); scanner.close(); Thread th = new Thread(new PrintThread()); th.start(); } class PrintThread implements Runnable{ @Override public void run() { for (int i=0; i<10; i++) { System.out.print(i+1 + " "); } System.out.println("\n스레드 종료"); } } public static void main(String[] args) { new Ex01(); } }
2.
import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Graphics; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.JFrame; import javax.swing.JPanel; public class Ex02 extends JFrame{ private MyPanel panel = new MyPanel(); public Ex02() { super("원을 0.5초 간격으로 이동"); setDefaultCloseOperation(EXIT_ON_CLOSE); setContentPane(panel); setSize(300,300); setVisible(true); Container c = getContentPane(); MyThread th = new MyThread(panel); c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { th.start(); } }); setFocusable(true); requestFocus(); } class MyPanel extends JPanel{ @Override public void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.MAGENTA); int x = (int)(Math.random()*300); int y = (int)(Math.random()*300); g.drawOval(x, y, 50, 50); } } class MyThread extends Thread{ private MyPanel panel; public MyThread(MyPanel panel) { this.panel = panel; } @Override public void run() { while(true) { panel.repaint(); try { sleep(400); } catch (InterruptedException e) { return; } } } } public static void main(String[] args) { new Ex02(); } }
3.
import java.awt.Container; import java.awt.Font; import java.util.Calendar; import javax.swing.JFrame; import javax.swing.JLabel; public class Ex03 extends JFrame { private JLabel label = new JLabel(""); public Ex03() { super("디지털 시계 만들기"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); label.setFont(new Font("Gothic", Font.ITALIC, 100)); c.add(label); TimerThread th = new TimerThread(label); th.start(); setSize(400,200); setVisible(true); } class TimerThread extends Thread{ private JLabel label = null; public TimerThread(JLabel label) { this.label = label; } public String getNowTime() { Calendar c = Calendar.getInstance(); int hour = c.get(Calendar.HOUR_OF_DAY); int min = c.get(Calendar.MINUTE); int second = c.get(Calendar.SECOND); String ClockText = Integer.toString(hour); ClockText = ClockText.concat(":"); ClockText = ClockText.concat(Integer.toString(min)); ClockText = ClockText.concat(":"); ClockText = ClockText.concat(Integer.toString(second)); return ClockText; } @Override public void run() { while(true) { label.setText(getNowTime()); try { sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } } public static void main(String[] args) { new Ex03(); } }
4.(1)
import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class Ex04 extends JFrame { public Ex04() { super("진동하는 프레임 만들기"); setDefaultCloseOperation(EXIT_ON_CLOSE); VibThread th = new VibThread(this); th.start(); setSize(500,500); setVisible(true); } class VibThread extends Thread{ private JFrame f = null; private int x = 500; public VibThread(JFrame f){ this.f = f; } @Override public void run() { while(true) { if (x == 500) x += 5; else x -= 5; f.setLocation(x,x); } } } public static void main(String[] args) { new Ex04(); } }
4.(2)
import java.awt.Color; import java.awt.Container; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; public class Ex04 extends JFrame { public Ex04() { super("진동하는 프레임 만들기"); setDefaultCloseOperation(EXIT_ON_CLOSE); Container c = getContentPane(); JLabel label = new JLabel("진동 레이블"); label.setFont(new Font("Gothic", Font.ITALIC, 50)); c.add(label); label.setLocation(50,50); VibThread th = new VibThread(label); th.start(); setSize(500,500); setVisible(true); } class VibThread extends Thread{ private JLabel label = null; public VibThread(JLabel label){ this.label = label; } @Override public void run() { while(true) { int x = label.getX(); if (x == 50) x += 5; else x -= 5; label.setLocation(x,x); } } } public static void main(String[] args) { new Ex04(); } }
반응형
'Programming Language > JAVA' 카테고리의 다른 글
명품자바 programming 11장 실습문제 (0) | 2020.12.04 |
---|---|
명품자바 programming 실습문제 10장 (0) | 2020.12.04 |
명품자바 programming 실습문제 9장 (0) | 2020.12.04 |
\n과 \r (Escape String) (0) | 2020.12.02 |