자바 프로그래밍

package net.jeongsam.gui;

import java.awt.*;
import java.awt.event.*;
import java.io.*;

@SuppressWarnings("serial")
class MyViewer extends Frame implements ActionListener {
	private TextField txtFileName;
	private Button btnOpen;
	private TextArea txtTextArea;
	
	public MyViewer() {
		super("텍스트 뷰어");
		
		txtFileName = new TextField(20);
		btnOpen = new Button("열기");
		txtTextArea = new TextArea(20, 20);
		
		btnOpen.addActionListener(this);
		
		Panel p = new Panel();
		
		p.add(new Label("파일명:"));
		p.add(txtFileName);
		p.add(btnOpen);
		
		add(p, BorderLayout.NORTH);
		add(txtTextArea, BorderLayout.CENTER);
		
		setSize(400, 300);
		setVisible(true);
	}

	public static void main(String[] args) {
		new MyViewer();
	}
	
	public void actionPerformed(ActionEvent e) {
		try {
			int rByte = 0;
			FileInputStream fis = new FileInputStream(txtFileName.getText());
			
			while ((rByte = fis.read()) != -1) {
				txtTextArea.setText(txtTextArea.getText() + (char)rByte);
			}
			
			fis.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();	
		}
	}
}
package net.jeongsam.excise;

class SortTest implements Comparable {
	int a, b;
	
	public SortTest() {
		this(5, 5);
	}
	
	public SortTest(int a, int b) {
		this.a = a;
		this.b = b;
	}
	/**
	 * 두 SortTest 타입의 객체간의 비교는 객체가 가지고 있는 멤버변수의
	 * 합을 기준으로 비교.
	 */
	@Override
	public int compareTo(SortTest obj) {
		int sum = a + b;
		return ((sum < (obj.a + obj.b)) ? -1 :
			(sum == (obj.a + obj.b) ? 0 : 1));
	}
	
	@Override
	public String toString() {
		return ("a : " + a + ", b : " + b); 
	}

}
package net.jeongsam.excise;

import java.util.Arrays;

class SortMain {
	public SortTest[] st;
	
	public SortMain() {
		st = new SortTest[5];
		
		st[0] = new SortTest(1, 3);
		st[1] = new SortTest(2, 3);
		st[2] = new SortTest(1, 2);
		st[3] = new SortTest(2, 1);
		st[4] = new SortTest(3, 3);
	}

	public static void main(String[] args) {
		SortMain main = new SortMain();
		
		System.out.println("--- 정렬전 ---");
		for (SortTest s : main.st) {
			System.out.println(s);
		}
		
		Arrays.sort(main.st);
		
		System.out.println("--- 정렬후 ---");
		for (SortTest s : main.st) {
			System.out.println(s);
		}
	}
}

교재연습 문제 풀이 7-4

2009. 9. 1. 11:44
package net.jeongsam.gui;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
public class EventEx03 extends Frame implements ActionListener {
	Button[] btnCity;
	Button btnExit;
	TextField txtMsg;
	Panel p1, p2;
	
	public EventEx03() {
		super("버튼/텍스트 필드 생성과 이벤트 처리");
		
		String[] citys = {"천안", "당진", "속초", "서울", "제주"};
		btnCity = new Button[citys.length];
		for (int i = 0; i < citys.length; i++) {
			btnCity[i] = new Button(citys[i]);
			btnCity[i].addActionListener(this);
		}
		
		btnExit = new Button("종료");
		btnExit.addActionListener(this);
		txtMsg = new TextField("선택한 지명: ", 12);
		
		p1 = new Panel();
		p1.setLayout(new FlowLayout(FlowLayout.CENTER));
		p1.add(new Label("당신이 좋아하는 지명을 선택하기 바랍니다. "));
		for (Button b : btnCity)
			p1.add(b);
		
		p2 = new Panel();
		p2.setLayout(new FlowLayout(FlowLayout.CENTER));
		p2.add(txtMsg);
		p2.add(btnExit);
		
		add(p1, BorderLayout.NORTH);
		add(p2, BorderLayout.CENTER);
		
		setSize(600, 300);
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource() == btnExit) {
			dispose();
			System.exit(0);
		}
		
		for (Button e : btnCity) {
			if (arg0.getSource() == e) {
				txtMsg.setText("선택한 지명: " + e.getLabel());
			}
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new EventEx03();
	}

}
package net.jeongsam.gui;

import java.awt.Button;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.*;

@SuppressWarnings("serial")
class EventEx02 extends Frame
		implements ActionListener, WindowListener {
	Button b1, b2;
	
	public EventEx02() {
		super("File Dialog Sample");
		b1 = new Button("파일 대화 상자 생성 버튼");
		b2 = new Button("종료 버튼");
		
		b1.addActionListener(this);
		b2.addActionListener(this);
		
		setLayout(new FlowLayout(FlowLayout.CENTER));
		add(b1);
		add(b2);
		
		addWindowListener(this);
		
		setSize(400, 300);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == b1) {
			FileDialog fdia = new FileDialog(
					this, "열기 대화창에 온 것을 환영합니다.",
					FileDialog.LOAD);
			fdia.setVisible(true);
			b1.requestFocus();
		}
		
		if (e.getSource() == b2) {
			dispose();
			System.exit(0);
		}
	}
	
	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		dispose();
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new EventEx02();
	}
}
package net.jeongsam.gui;

import java.awt.*;
import java.awt.event.*;

@SuppressWarnings("serial")
class EventEx01 extends Frame implements ActionListener {
	/**
	 * 프레임에 버튼을 추가하고 윈도우 닫기 이벤트 핸들러 추가.
	 * 버튼에 클릭 이벤트 핸들러 추가.
	 */
	public EventEx01() {
		super("이벤트 예제1"); // 프레임 인스턴스 생성
		Button b = new Button("클릭"); // 버튼 인스턴스 생성
		b.addActionListener(this); // 버튼 인스턴스에 액션 리스너 인터페이스 등록
		setLayout(new FlowLayout()); // 플로우 레이아웃 매니저 지정
		add(b); // 프레임에 버튼 인스턴스 추가
		/**
		 * 프레임에 종료 버튼의 구현을 위해 윈도우 리스너 등록.
		 * 내부 익명 클래스를 사용하여 윈도우 리스너 인터페이스 대신 윈도우 어댑터 클래스 추가.
		 * -- 윈도우 리스너의 경우 7개의 메서드를 구현해야 하는 반면, 윈도우 어댑터는 필요한
		 * -- 메서드만 구현하면 된다.
		 */
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				dispose(); // 현재 리소스 반납후 프레임 종료.
				System.exit(0); // 프로그램 종료.
			}
		});
		setSize(400, 300); // 프레임의 폭, 높이 값을 지정. 
		setVisible(true); // 프레임 화면에 표시.
	}
	
	/**
	 * 버튼을 눌렀을 경우 동작하는 이벤트 핸들러 구현
	 */
	@Override
	public void actionPerformed(ActionEvent arg0) {
		System.out.println("버튼을 눌렀음.");	
	}

	public static void main(String[] args) {
		new EventEx01();

	}
}

+ Recent posts