반응형

문제

컨텐트팬의 배경색은 초록색으로 하고 마우스를 드래깅하는 동안만 노란색으로 유지하는 스윙 응용프로그램을 작성하라.

 

소스 코드

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;

public class Main2 extends JFrame{
	public Main2() {
		setTitle("드래깅동안 YELLOW...");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		Panel p = new Panel();
		p.setBackground(Color.green);
		
		p.addMouseMotionListener(new MouseAdapter() {
			public void mouseDragged(MouseEvent e) {
				p.setBackground(Color.yellow);
			}
		});
		p.addMouseListener(new MouseAdapter() {
			public void mouseReleased(MouseEvent e) {
				p.setBackground(Color.green);
			}
		});
		c.add(p);
		
		setSize(300,200);
		setVisible(true);
	}
	public static void main(String[] args) {
		new Main2();
		
	}

}

 

반응형
반응형

문제

문제 13을 확장하여 다음 명령을 추가하라.

 

명령 사용 예시

>>rename phone txt p.txt //phone.txt를 p.txt로 변경. 파일과 디렉터리 이름 변경
>>mkdir xxx // 현재 디렉터리 밑에 xxx 디렉터리 생성

 

소스 코드 ( 실습문제 13번 디렉터리 이동 부분 NullPointerException 오류도 수정 하였습니다. )

import java.io.*;
import java.util.*;

public class Main13 {
	public static void ListFile(File f) {
		File[]subFiles = f.listFiles();
		for(int i = 0;i<subFiles.length;i++) {
			File file = subFiles[i];
			
			// 파일 종류
			if(file.isDirectory())
				System.out.print("dir\t");
			else if(file.isFile())
				System.out.print("file\t");
			
			System.out.printf(file.length()+"바이트"); // 파일 크기
			System.out.println("   \t"+file.getName()); // 파일이름
		}
	}
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		File f = new File("C:\\");
		Scanner s = new Scanner(System.in);
		System.out.println("***** 파일 탐색기입니다. *****");
		System.out.println("\t["+f.getPath()+"]");
		ListFile(f);
		while(true) {
			
			System.out.print(">>");
			String search = s.next();
			if(search.equals("그만")) {
				break;
			}
			else if(search.equals("..")) {
				File temp = new File(f.getParent());
				f = temp;
			}
			else if(search.equals("rename")) {
				String Changename = s.nextLine();
				try {
					StringTokenizer st = new StringTokenizer(Changename," ");
					while(st.hasMoreTokens())
					{
						File temp = new File(f.getPath()+"\\"+st.nextToken());
						File renameFile = new File(f.getPath()+"\\"+st.nextToken());
						temp.renameTo(renameFile);
					}
				}catch(NoSuchElementException e) {
					System.out.println("두 개의 파일명이 주어지지 않았습니다.!");
					continue;
				}
			}
			
			else if(search.equals("mkdir")) {
				String name = s.nextLine();
				File fout = new File(f.getPath()+"\\"+name);
				if(!fout.exists()) {
					System.out.println(fout.getName()+ " 디렉터리를 생성하였습니다.");
					fout.mkdir();
				}
				else {
					System.out.println("이미 존재하는 파일입니다.");
					continue;
				}
				
			}
			else {
					File temp = new File(f.getPath()+"\\"+search);	
					f = temp;		
			}
			try {
				System.out.println("\t["+f.getPath()+"]");
				ListFile(f);
			}catch(NullPointerException e) {
				System.out.println("없는 파일입니다.");
				continue;
			}
		}
		s.close();
	}
}
반응형
반응형

문제

간단한 파일 탐색기를 만들어보자. 처음 시작은 C:\에서부터 시작한다. 명령은 크게 2가지로서 ".."를 입력하면 부모 디렉터리로 이동하고, "디렉터리명"을 입력하면 서브 디렉터리로 이동하여 파일리스트를 보여준다.

(출력예시는 너무 길어 생략하겠습니다.)

 

소스 코드

import java.io.*;
import java.util.*;

public class Main13 {
	public static void ListFile(File f) {
		File[]subFiles = f.listFiles();
		for(int i = 0;i<subFiles.length;i++) {
			File file = subFiles[i];
			
			// 파일 종류
			if(file.isDirectory())
				System.out.print("dir\t");
			else if(file.isFile())
				System.out.print("file\t");
			
			System.out.printf(file.length()+"바이트"); // 파일 크기
			System.out.println("   \t"+file.getName()); // 파일이름
		}
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File f = new File("C:\\");
		Scanner s = new Scanner(System.in);
		System.out.println("***** 파일 탐색기입니다. *****");

		while(true) {
			System.out.println("\t["+f.getPath()+"]");
			ListFile(f);
			System.out.print(">>");
			String search = s.nextLine();
			if(search.equals("그만")) {
				break;
			}
			else if(search.equals("..")) {
				File temp = new File(f.getParent());
				f = temp;
			}
			else {
				File temp = new File(f.getPath()+"\\"+search);	
				f = temp;
			}
		}
		s.close();
	}
}
반응형

+ Recent posts