• 명품 자바 프로그래밍(개정4판) 제 13장 실습문제 1~4번

    2020. 12. 8.

    by. Hyeon-Uk

    반응형

    [1번]

    import java.util.Scanner;
    
    class myth implements Runnable{
    	public void run() {
    		// TODO Auto-generated method stub
    		System.out.println("스레드 시작");
    		for(int i=0;i<10;i++) {
    			System.out.print(i+1+" ");
    		}
    		System.out.println();
    		System.out.println("스레드 종료");
    	}
    }
    
    
    public class p1301 {
    	
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Thread th=new Thread(new myth());
    		Scanner sc=new Scanner(System.in);
    		System.out.print("아무 키나 입력>> ");
    		sc.nextLine();
    		th.run();
    	}
    }
    
    

    [2번]

    import java.awt.Color;
    import java.awt.Container;
    import java.awt.Graphics;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.util.Scanner;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    class MyPanel extends JPanel{
    	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{
    	MyPanel jp;
    	public myThread(MyPanel j) {
    		jp=j;
    	}
    	public void run() {
    		while(true) {
    			try {
    				sleep(500);
    				jp.repaint();
    			} catch (Exception e) {
    				return;
    			}
    		}
    	}
    }
    
    class myFrame extends JFrame{
    	MyPanel myp=new MyPanel();
    	myThread th=new myThread(myp);
    	public myFrame() {
    		setTitle("원을 0.5초 간격으로 이동하는 frame");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		setContentPane(myp);
    		myp.setLayout(null);
    		myp.setOpaque(true);
    		myp.addMouseListener(new MouseAdapter() {
    			public void mouseClicked(MouseEvent e) {
    				th.start();
    			}
    		});
    		
    		
    		setVisible(true);
    		setSize(500,500);
    	}
    }
    
    public class p1302 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new myFrame();
    	}
    }
    
    

    반응형

    [3번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class myth extends Thread{
    	JLabel clock;
    	public myth(JLabel clock) {
    		this.clock=clock;
    	}
    	
    	public void run() {
    		while(true) {
    			try {
    			Calendar c=Calendar.getInstance();
    			int hour=c.get(Calendar.HOUR_OF_DAY);
    			int min=c.get(Calendar.MINUTE);
    			int sec=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(sec));
    					
    			clock.setText(clockText);
    			sleep(1000);
    			}catch(Exception e) {
    				return;
    			}
    		}
    	}
    }
    
    class myFrame extends JFrame{
    	JLabel clock=new JLabel();
    	public myFrame() {
    		setTitle("디지털 시계 만들기");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		Container c=getContentPane();
    		c.setLayout(new FlowLayout());
    		
    		myth th=new myth(clock);
    		c.add(clock);
    		
    		th.start();
    		setVisible(true);
    		setSize(500,500);
    	}
    }
    
    public class p1303 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new myFrame();
    	}
    }
    
    

    [4-1번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class myth extends Thread{
    	JFrame jf;
    	int x=300;
    	public myth(JFrame jf) {
    		this.jf=jf;
    	}
    	
    	public void run() {
    		while(true) {
    			try {
    				if(x==300) x=330;
    				else x=300;
    				jf.setLocation(x,x);
    				sleep(100);
    			}catch(Exception e) {
    				return;
    			}
    		}
    	}
    }
    
    class myFrame extends JFrame{
    	public myFrame() {
    		setTitle("진동하는 프레임 만들기");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    		
    		myth th=new myth(this);
    		th.start();
    		setVisible(true);
    		setSize(500,500);
    	}
    }
    
    public class p1304 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new myFrame();
    	}
    }
    
    

    [4-2번]

    import java.awt.Container;
    import java.awt.FlowLayout;
    import java.util.Calendar;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    
    class myth extends Thread{
    	JLabel jf;
    	int x=300;
    	public myth(JLabel jf) {
    		this.jf=jf;
    	}
    	
    	public void run() {
    		while(true) {
    			try {
    				if(x==100) x=130;
    				else x=100;
    				jf.setLocation(x,x);
    				sleep(100);
    			}catch(Exception e) {
    				return;
    			}
    		}
    	}
    }
    
    class myFrame extends JFrame{
    	JLabel jl=new JLabel("진동 레이블");
    	public myFrame() {
    		setTitle("진동하는 레이블 만들기");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setLayout(null);
    		
    		jl.setBounds(100,100,200,300);
    		add(jl);
    		myth th=new myth(jl);
    		th.start();
    		setVisible(true);
    		setSize(500,500);
    	}
    }
    
    public class p1304 {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		new myFrame();
    	}
    }
    
    

     

    반응형

    댓글