반응형

문제

클릭 연습용 스윙 응용프로그램을 작성하라. "C"를 출력하는 JLabel을 하나 만들고 초기 위치를 (100, 100)으로 하고, "C"를 클릭할 때마다 컨텐트팬 내에 랸덤한 위치로 움직이게 하라.

 

소스 코드

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

public class Main6 extends JFrame{
	public Main6() {
		setTitle("클릭 연습 용 응용프로그램");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(null);
		
		JLabel l = new JLabel("C");
		l.setSize(10,10);
		l.setLocation(100, 100);
		
		c.add(l);
		
		l.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				int x = (int)(Math.random()*250);
				int y = (int)(Math.random()*250);
				l.setLocation(x, y);
			}
		});
		
		setSize(300,300);
		setVisible(true);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new Main6();
	}

}
반응형
반응형

문제

20개의 10 x 10 크기의 JLabel 컴포넌트가 프레임 내에 (50, 50) 위치에서 (250, 250) 영역에서 랜덤한 위치에 출력되도록 스윙 프로그램을 작성하라. 프레임의 크기를 300 X 300으로 하고, JLabel의 배경색은 모두 파란색으로 하라.

 

소스 코드

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

public class Main6 extends JFrame{
	public Main6() {
		setTitle("Random Labels");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		Container c = getContentPane();
		
		c.setLayout(null);
		JLabel [] label = new JLabel[20];
		
		for(int i = 0;i<20;i++) {
			label[i] = new JLabel();
			label[i].setBackground(Color.BLUE);
			int x = (int)(Math.random()*200)+50;
			int y = (int)(Math.random()*200)+50;
			label[i].setLocation(x, y);
			label[i].setSize(10,10);
			label[i].setOpaque(true);
			c.add(label[i]);
		}
		setSize(300,300);
		setVisible(true);
	}
	
	public static void main(String[] args)
	{
		new Main6();
	}
}

반응형
반응형

문제

사용자로부터 두 개의 텍스트 파일 이름을 입력받고 첫 번째 파일 뒤에 두 번째 파일을 덧붙인 새로운 파일을 생성하는 프로그램을 작성하라. 아래 실행 예시에서는 프로젝트 폴더에 elvis1.txt와 elvis2.txt를 미리 준비해 두었다.

 

출력 예시

전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.
첫번째 파일 이름을 입력하세요>>elvis1.txt
두번째 파일 이름을 입력하세요>>elvis2.txt
프로젝트 폴더 밑에 appended.txt. 파일에 저장하였습니다.

 

소스 코드

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

public class Main6 {
	public static void main(String[] args) {
		FileReader fin1 = null;
		FileReader fin2 = null;
		FileWriter fin3 = null;
		BufferedReader bufReader1 = null,bufReader2 = null;
		String Line1 = "",Line2 = "";
		String File1,File2;
		Scanner s = new Scanner(System.in);
		
		System.out.println("전체 경로명이 아닌 파일 이름만 입력하는 경우, 파일은 프로젝트 폴더에 있어야 합니다.");
		try {
			System.out.print("첫번째 파일 이름을 입력하세요>>");
			File1 = s.nextLine();
			System.out.print("두번째 파일 이름을 입력하세요>>");
			File2 = s.nextLine();
			fin1 = new FileReader("파일경로"+File1);
			fin2 = new FileReader("파일경로"+File2);
			fin3 = new FileWriter("파일경로\\appended.txt");
			bufReader1 = new BufferedReader(fin1);
			bufReader2 = new BufferedReader(fin2);
			while((Line1 = bufReader1.readLine())!=null) {
				fin3.write(Line1,0,Line1.length());
				fin3.write("\r\n", 0, 2);	
			}
			while((Line2 = bufReader2.readLine())!=null) {
				fin3.write(Line2,0,Line2.length());
				fin3.write("\r\n", 0, 2);	
			}
			
			System.out.println("프로젝트 폴더 밑에 appended.txt 파일에 저장하였습니다.");
			fin1.close();
			fin2.close();
			fin3.close();
			bufReader1.close();
			bufReader2.close();
		}
		catch(IOException e) {
			System.out.println("입출력 오류");
		}
		s.close();
	}
}

elvis1.txt
0.00MB
elvis2.txt
0.00MB
appended.txt
0.00MB

반응형

+ Recent posts