Download Java_study

Document related concepts
no text concepts found
Transcript
Java ์†Œ์Šค ์ •๋ฆฌ(ํ•˜์ดํผ๋งํฌ)
1. ๊ธฐ๋ณธ๋ฐ์ดํ„ฐํƒ€์ž…(ํ˜•๋ณ€ํ™˜)
37. JTree ์™€ JTable ํด๋ž˜์Šค
2. ์•„์Šคํ‚ค์ œ์–ด๋ฌธ์ž ๋ฐ ๋ฌธ์ž(์—”ํ„ฐ๋ฌธ์ž)
38. TCPํ†ต์‹ (InetAddress,Socket, rverSocket)
3. Wrapper ํด๋ž˜์Šค(4byte)
4. ๊ธฐ๋ณธ ์ž…์ถœ๋ ฅ
5. ํŒŒ์ผ์˜ ์ž…์ถœ๋ ฅ
6. 1byte ์ž…์ถœ๋ ฅ
7. ํ…์ŠคํŠธ ์ž…์ถœ๋ ฅ(Scanner ํด๋ž˜์Šค)
8. ๊ฐ์ฒด ์ž…์ถœ๋ ฅ
9. ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„
10. ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
11. ๋ฌธ์ž์—ด ๊ฒ€์ƒ‰
12. ๋ฌธ์ž์—ด์˜ NULL๊ฐ’
13. ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž ์ถ”์ถœํ•˜๊ธฐ
14. ๋ฌธ์ž์—ด ๊ฐ์ฒด ๋ณ€๊ฒฝํ•˜๊ธฐ(StringBuffer)
15. String์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ
16. ๊ธฐ๋ณธํด๋ž˜์Šค(์ ‘๊ทผ์ œํ•œ์ž, ์ง€์ •์˜ˆ์•ฝ์–ด)
17. ์ž๋ฐ” 4 ๋Œ€ ํด๋ž˜์Šค
18. ์ƒ์†๊ณผ ๋‹ค์–‘์„ฑ
19. abstract(์ถ”์ƒ์ ) ์™€ interface
20. ์Šค๋ž˜๋“œ Multi-Thread
21. ์˜ˆ์™ธ์™€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ
22. Package
23. assertion
24. Collection ๊ณผ APIs
25. Frame ํด๋ž˜์Šค์™€ Label
26. Layout Manager
27. Panel ํด๋ž˜์Šค
28. ์ฃผ๋ณ€ ํด๋ž˜์Šค๋“ค
29. AWT ๊ด€๋ จ ์ปดํฌ๋„ŒํŠธ
30. Event ์ƒ์„ฑ๋ฐฉ๋ฒ•
31. Event ํ™œ์šฉ ๋ฒ”์œ„
32. Graphics์˜ paint ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•
33. Applet ์ž‘์„ฑ๋ฒ•
34. Applet์˜Image์™€ Sound
35. Swing(JFC) ์ปดํฌ๋„ŒํŠธ
36. JApplet ์ž‘์„ฑ๋ฒ•
1.๊ธฐ๋ณธ๋ฐ์ดํƒ€ํƒ€์ž…(Primitive Data Type) <์ฒ˜์Œ์œผ๋กœ>
byte(1), char(2), int(4), long(8), float(4), double(8), boolean(1)
โ–ฃ ์ •์ˆ˜ํ˜•
โ—ˆ 1 byte -128~127
โ—ˆ 2 short -32768~32767
โ—ˆ 4 int -2147483648 ~
โ—ˆ 8 long
2147483647
-922 ๊ฒฝ ~ 922 ๊ฒฝ long e = 1232312L;
โ—ˆ 2 char 0 ~ 65535 (๊ณ„์‚ฐ์œผ๋กœ ๋˜๋ฉด ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ๋œ๋‹ค)
โ–ฃ ์‹ค์ˆ˜ํ˜•
โ—ˆ 4 float
โ—ˆ 8 double
โ–ฃ ๋…ผ๋ฆฌํ˜•
โ—ˆ 1 boolean ํ˜•๋ณ€ํ™˜ ๋ถˆ๊ฐ€ ๋ฐ˜๋“œ์‹œ ์ดˆ๊ธฐํ™” ๊ฐ’
ํ˜•๋ณ€ํ™˜
Promotion : ์ž๋™ ํ˜•๋ณ€ํ™˜ Casting : ๊ฐ•์ œ ํ˜•๋ณ€ํ™˜
a= Byte.parseByte(๋ณ€์ˆ˜) //byte ๋กœ ๋ณ€ํ™˜
b = Integer.parseInt(๋ณ€์ˆ˜) //์ •์ˆ˜๋กœ ๋ณ€ํ™˜
c = Float.parseFloat(๋ณ€์ˆ˜) //float ๋กœ ๋ณ€ํ™˜
๊ทธ๋ฐ–์— API ์—์„œ parse ๋ ˆํผ ํด๋ž˜์Šค๋ฅผ ํ™•์ธ ๊ฐ€๋Šฅ
๋ฌธ์žํ˜• ์ €์žฅ
String
2.์•„์Šคํ‚ค ์ œ์–ด๋ฌธ์ž ๋ฐ ๋ฌธ์ž<์ฒ˜์Œ์œผ๋กœ>
0 โ€˜\0โ€™ null
8 โ€˜\bโ€™ backspace
9 โ€˜\tโ€™ tab
10 โ€˜\nโ€™ ๋ฐ‘์œผ๋กœ
13 โ€˜\rโ€™ ์ฒ˜์Œ์œผ๋กœ
์—”ํ„ฐํ‚ค โ€˜\n\rโ€™
27 esc
48 ~ 57 '0' ~'9'
65 ~ 90'A' ~'Z'
97 ~ 122'a' ~'z'
3. Wrapper ํด๋ž˜์Šค (4byte) <์ฒ˜์Œ์œผ๋กœ>
๋ฐ์ดํ„ฐ๋ฒ•์œ„์˜ ์ฃผ์†Œ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค
์ง€์—ญ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ์ง€์ •ํ•ด ์ค˜์•ผํ•œ๋‹ค
๋ณ€์ˆ˜๋Š” {} ๋ฐ–์œผ๋กœ ๋‚˜์˜ค๋ฉด ๋ณ€์ˆ˜๋Š” ์‚ด์•„์ง„๋‹ค.
4. ๊ธฐ๋ณธ์ž…์ถœ๋ ฅ<์ฒ˜์Œ์œผ๋กœ>
System.err.println = ์—๋Ÿฌ์ถœ๋ ฅ
System.in.read() = ์ž…๋ ฅ
System.out.println = ์ถœ๋ ฅ
System.out.write(65);
//์•„์Šคํ‚ค์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ๋ฌธ์ž๊ฐ€ ๋ฒ„ํผ์— ์ €์žฅ๋œ๋‹ค.(write Method)
System.out.flush();
//๋ฒ„ํผ์— ์ €์žฅ๋œ ๋ฌธ์ž๋ฅผ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ์ž…์ถœ๋ ฅ
byte[] by = new byte[]{'j', 'a','v','a'};
System.out.write(by,0 ,4);
//Autoflush ์‚ฌ์šฉ์•ˆํ•ด๋„ ๋ฐฐ์—ด๋กœ ํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค
๋ฌธ์ž์—ด ์ž…๋ ฅ(๋ฐ˜๋“œ์‹œ์˜ˆ์˜์ฒ˜๋ฆฌ)
byte[] by = new byte[10];
System.out.println("๊ณผ๋ชฉ =");
System.in.read(by);
System.out.println("๊ฒฐ๊ณผ="+new String(by));
๋ฌธ์ž ๋ฐ ์ˆซ์ž ์ž…๋ ฅ
System.in.read(); //์•„์Šคํ‚ค ์ž…๋ ฅ๋ฐฉ๋ฒ•
System.in.read()-48; //or 'o' ์ˆซ์ž์ž…๋ ฅ๋ฐฉ๋ฒ•
(char)System.in.read(); //๋ฌธ์ž์ž…๋ ฅ๋ฐฉ๋ฒ•
โ€ป ๋‹จ ํ‚ค ํ•˜๋‚˜์— ํ•œ๋ฒˆ ์ €์žฅ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฏ€๋กœ byte[] ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ๋‹ค.
5. ํŒŒ์ผ ์ž…์ถœ๋ ฅ<์ฒ˜์Œ์œผ๋กœ>
String a = File.pathSeparator; //๋‹ต= ; ํŒŒ์ผ๊ตฌ๋ถ„์ž
char b= File.pathSeparatorChar;//๋‹ต= ;
String c= File.separator; //๋‹ต = \
char d= File.separatorChar;
//์–ด๋–ค ์šด์˜์ฒด์ œ์—์„œ๋„ ์‚ฌ์šฉํ• ์ˆ˜ ์žˆ๋„๋ก ํ•˜๋Š” ๊ตฌ๋ถ„์ž
File f = new File("Exam_01.java");
//์กด์žฌํ•˜๋“ , ์กด์žฌ ์•ˆํ•˜๋“  ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•œ๊ฐœ๋งŒ์žˆ์œผ๋ฉด ํ˜„์žฌ ํด๋”์—์„œ ์ฐพ๊ฒŒ๋œ๋‹ค.
File f2 =
new File("E:\\์›น์—ฐ์Šต\\java\\๊ณต๋ถ€๋ฐฉ\\Day016\\src\\Exam_02.java");
//์ ˆ๋Œ€๊ฒฝ๋กœ๋กœ ํ•ด๋„ ๋œ๋‹ค.
File dir = new File("E:\\์›น์—ฐ์Šต\\java\\๊ณต๋ถ€๋ฐฉ\\Day016\\src"); //๋””๋ ‰ํ† ๋ฆฌ๋กœ
์ž…๋ ฅ๊ฐ€๋Šฅ
File f3 = new File(dir,"Exam_03.java");
//๋””๋ ‰ํ† ๋ฆฌ ์‚ฌ์šฉํ•˜์—ฌ ํŒŒ์ผ ๊ฒ€์ƒ‰
boolean b = f.exists(); //ํŒŒ์ผ ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
System.out.println("read ? " + f.canRead()); //์ฝ์„์ˆ˜ ์žˆ๋Š”๊ฐ€
System.out.println("write ? "+ f.canWrite()); // ์“ธ์ˆ˜ ์žˆ๋Š”๊ฐ€
try{
boolean bool = f1.createNewFile();//ํŒŒ์ผ์ด ์—†์œผ๋ฉด ์ƒ์„ฑํ•œ๋‹ค.
System.out.println("bool = "+bool);
}catch(IOException e){}
try{
File ff = new File(".");
File f2 = File.createTempFile("teme", ".txt", ff); //์ž„์‹œ ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ
System.out.println("f2 ="+f2);
f2.delete(); // ์ฆ‰์‹œ ์ง€์šด๋‹ค.
}catch(IOException e){}
f1.deleteOnExit();
//deleteOnExit () ํ”„๋กœ๊ทธ๋žจ์ด ๋๋‚ ๋•Œ ์ง€์›Œ๋ผ
System.out.println("f abs path = " + f.getAbsolutePath()); //์ ˆ๋Œ€ ๊ฒฝ๋กœ
System.out.println("f ์ƒ๋Œ€๊ฒฝ๋กœ๋Š” " + f.getCanonicalPath());//์ƒ๋Œ€๊ฒฝ๋กœ
System.out.println("f ์ƒ๋Œ€๊ฒฝ๋กœ๋Š” " + f.getCanonicalFile());//์ƒ๋Œ€๊ฒฝ๋กœ
System.out.println("name = "+f.getName()); //ํŒŒ์ผ์ด๋ฆ„
System.out.println("parent = " + f.getParent()); //ํŒŒ์ผ ์ƒ์œ„
System.out.println("path" + f.getPath()); //์ „๋ถ€
System.out.println("fff is dir = " + fff.isDirectory()); //๋””๋ ‰ํ† ๋ฆฌ์ธ๊ฐ€
System.out.println("f is File = " + f.isFile()); //ํŒŒ์ผ์ธ๊ฐ€
System.out.println("f is hidden =" + f.isHidden());//์ˆจ๊ธด ํŒŒ์ผ์ธ๊ฐ€
System.out.println("f is lastModified" +
new Date(f.lastModified())); //์ตœ์ข… ์ˆ˜์ •์ผ DATA ์‚ฌ์šฉ
f.setLastModified(new Date().getTime());
//setLastModified ์ตœ์ข… ์ˆ˜์ •์ผ ๋ณ€๊ฒฝ
System.out.println("f is lastModified" +
new Date(f.lastModified()));
System.out.println("f is size =" + f.length() + "bytes");
//ํŒŒ์ผ ๋ฐ”์ดํŠธ์šฉ๋Ÿ‰์„ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค
String[] s = fff.list();
///ํ•ด๋™ ๋””๋ ‰ํ† ๋ฆฌ์•ˆ์— ๋“ค์–ด์žˆ๋Š” ํŒŒ์ผ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
for(String i : s){
System.out.println(i);
}
File[] roots = File.listRoots(); //๋“œ๋ผ์ด๋ฒ„(๋ฃจํŠธ) ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
for(File i: roots){
System.out.println(i);
}
File f2 = new File("AAA");
System.out.println(f2.mkdir()); //๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•จ ์ƒ์„ฑํ•˜๋ฉด true ๊ฐ’
File f3 = new File("bbb/ccc/ddd");
System.out.println(f3.mkdirs());
//ํ•˜์œ„๋””๋ ‰ํ† ๋ฆฌ์™€ ํ•จ๊ป˜ ์ƒ์„ฑํ• ๋•Œ ์‚ฌ์šฉ True ๊ฐ’ ์ „๋‹ฌ
f.setReadOnly(); //์ฝ๊ธฐ ์ „์šฉ์œผ๋กœ ๋ณ€๊ฒฝ
์ „๋‹ฌ
6. 1byte ์ž…์ถœ๋ ฅ<์ฒ˜์Œ์œผ๋กœ>
โ€ข
1byte ์ถœ๋ ฅ
import java.io.*;
public class Exam_05 {
public static void main(String [] ar) throws IOException{
FileOutputStream fos1 = new FileOutputStream(FileDescriptor.out); //๊ธฐ๋ณธ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ์œผ๋กœ
๋Œ€์ƒ์„ ์ฝ˜์†”๋กœ
File f= new File("aaa.txt");
FileOutputStream fos2 = new FileOutputStream(f,true);
//๊ธฐ๋ณธ์ถœ๋ ฅ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋Œ€์ƒ์„ ํŒŒ์ผ๋กœ ๋’ค์—
boolean ๊ฐ’์ด ๋“ค์–ด์˜ค๋ฉด ์—ฐ๊ฒฐํ•ด์„œ ์ถœ๋ ฅํ• ๊ฒƒ์ธ์ง€
byte[] by = new byte[]{'H','e','l','l','e',' ','j','a','v','a'};
//System.out.println("Hello java");
fos1.write(by);//๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ //์ฝ˜์†”
fos1.write(by,6,4);//๋ฐ”์ดํŠธ ๋ฐฐ์—ด๋กœ //์ฝ˜์†” 6์นธ๋ถ€ํ„ฐ4์นธ์„ ์ถœ๋ ฅ
fos2.write(by);//๋ฐ”์ดํŠธ๋ฐฐ์—ด๋กœ //ํŒŒ์ผ
fos1.write(65); //A์ถœ๋ ฅ
fos1.close();
fos1.close();
}
}
import java.io.*;
public class Exam_06 {
public static void main(String[] ar) throws IOException{
FileOutputStream fos1
= new FileOutputStream(FileDescriptor.out);
BufferedOutputStream bos1
= new BufferedOutputStream(fos1, 2048);
//๋กœ์ปฌ 2048 ํŒŒ์ผ 1024 ๋„คํŠธ์›Œํฌ 512 ์ ๋‹น
DataOutputStream dos1 = new DataOutputStream(bos1);
//๊ฐ๊ฐ์˜ ์ž๋ฃŒํ˜•์œผ๋กœ ์ถœ๋ ฅํ• ๋•Œ
FileOutputStream fos2
= new FileOutputStream(new File("bbb.txt"),false);
BufferedOutputStream bos2 = new BufferedOutputStream(fos2,1024);
DataOutputStream dos2 = new DataOutputStream(bos2);
dos1.writeInt(20);
dos1.writeDouble(12.34);
dos1.flush(); //ํ•ด๋‹น ๋ฒ„ํผ์—์„œ ๋‚˜์˜ค๊ฒŒํ•œ๋‹ค.
dos1.writeChar('A');
dos1.writeByte('\n'); //ํ•œ์นธ ๋›ฐ์›€
dos1.writeByte('B');
dos1.close(); //์ŠคํŠธ๋ฆผ์„ ๋‹ซ๋Š”๋‹ค.
dos2.writeInt(20);
dos2.writeDouble(12.34);
dos2.flush(); //ํ•ด๋‹น ๋ฒ„ํผ์—์„œ ๋‚˜์˜ค๊ฒŒํ•œ๋‹ค.
dos2.writeChar('A');
dos2.writeByte('\n'); //ํ•œ์นธ ๋›ฐ์›€
dos2.writeByte('B');
dos2.close(); //์ŠคํŠธ๋ฆผ์„ ๋‹ซ๋Š”๋‹ค.
}
}
โ€ข
1byte ์ž…๋ ฅ
import java.io.*;
public class Exam_07 {
public static void main(String[] ar) throws IOException{
FileInputStream fis1
= new FileInputStream(FileDescriptor.in);
FileInputStream fis2
= new FileInputStream(new File("aaa.txt"));
System.out.print("๋ฌธ์ž = ");
char ch =(char)fis1.read(); //๋ฌธ์ž ํ•œ๊ธ€์ž
System.in.read();
System.out.print("๋ฌธ์ž์—ด = ");
byte[] by = new byte[1024];
fis1.read(by); //๋ฌธ์ž๋Š” ๋ฐฐ์—ด
System.out.println();
System.out.println("ch = " + ch);
System.out.println("by = " + new String(by).trim());
//๋ฌธ์ž์—ด ์‚ฌ์ด์˜ ๊ณต๋ฐฑ์„ ์—†์•ค๋‹ค
fis1.close();
while(true){
int x = fis2.read(); //ํ•œ๋ฐ”์ดํŠธ๋กœ ์ž…๋ ฅ
if(x==-1) break; //ํŒŒ์ผ์•ˆ์— ๊ธ€์˜ ๋์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
System.out.print((char)x); //์ธํŠธ๋กœ ๋ฐ›์€ ๊ฒƒ์„ char ๋ณ€๊ฒฝ
}
fis2.close();
}
}
import java.io.*;
public class Exam_09 {
public static void main(String[] ar) throws IOException{
File f = new File("bbb.txt");
FileInputStream fis
= new FileInputStream(f);
BufferedInputStream bis
= new BufferedInputStream(fis, 1024);
DataInputStream dis
= new DataInputStream(bis);
int a = dis.readInt();
double b = dis.readDouble();
char c = dis.readChar();
byte d = dis.readByte();
byte e = dis.readByte();
/*while(true){
int x = dis.read(); //ํ•œ๋ฐ”์ดํŠธ๋กœ ์ž…๋ ฅ
if(x==-1) break; //ํŒŒ์ผ์•ˆ์— ๊ธ€์˜ ๋์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
System.out.print((char)x); //์ธํŠธ๋กœ ๋ฐ›์€ ๊ฒƒ์„ char ๋ณ€๊ฒฝ
}*/
dis.close();
System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + (char)e);
}
}
7. ํ…์ŠคํŠธ ์ž…์ถœ๋ ฅ<์ฒ˜์Œ์œผ๋กœ>
ํ…์ŠคํŠธ์ž…๋ ฅ๊ณผ์ถœ๋ ฅ
โ€ข
ํ…์ŠคํŠธ ์ถœ๋ ฅ
import java.io.*;
public class Exam_10 {
public static void main(String[] ar) throws IOException{
//ํ•œ๊ธ€(2byte) ํ• ๋•Œ๋Š” ํ…์ŠคํŠธ ์ถœ๋ ฅ์œผ๋กœ ์‚ฌ์šฉ ํ•œ๋‹ค
OutputStreamWriter osw
= new OutputStreamWriter(System.out);
//๋ฐ˜๋“œ์Šค 1byte ๋ฉ”๊ฐœ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
BufferedWriter bw = new BufferedWriter(osw, 1024);
PrintWriter pw = new PrintWriter(bw);
File f = new File("ccc.html");
try{
boolean bool = f.createNewFile();
}catch(IOException e){}
FileWriter fw = new FileWriter(f);
BufferedWriter bw1 = new BufferedWriter(fw,1024);
PrintWriter pw1 = new PrintWriter(bw1);
pw.println(10);
pw.println("test ์•ˆ๋…•ํ•˜์„ธ์š”");
pw1.println(10);
pw1.println("text ์•ˆ๋…•ํ•˜์„ธ์š”");
pw.close();
pw1.close();
}
}
โ€ข
ํ…์ŠคํŠธ ์ž…๋ ฅ
import java.io.*;
public class Exam_11 {
public static void main(String[] ar) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
// BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
File f = new File("ccc.txt");
FileReader fr = new FileReader(f);
// new FileReader(ํŒŒ์ผ๊ฐ์ฒด or ๋ฌธ์ž์—ด);
BufferedReader br1 = new BufferedReader(fr);
System.out.print("๋ฌธ์ž์—ด = ");
String str = br.readLine();
System.out.println("str = " + str);
System.out.print("์ˆซ์ž = ");
int x = Integer.parseInt(br.readLine());
System.out.println("x = " + x);
System.out.println("================================");
while(true) {
String s = br1.readLine();
if(s == null) break;
System.out.println("s = " + s);
} //์ถœ๋ ฅ๋ถ€๋ถ„
br.close();
br1.close();
}
}
*java.util.Scanner ํด๋ž˜์Šค
import java.io.*;
import java.util.*;
public class Exam_12 {
public static void main(String [] ar) throws IOException{
Scanner in = new Scanner(System.in);
System.out.print("๋ฌธ์ž์—ด = ");
String str = in.next();
System.out.print("์ˆซ์ž = ");
int x = in.nextInt();
System.out.print("๋”๋ธ” ์ˆซ์ž = ");
double y = in.nextDouble();
System.out.println("str = " + str);
System.out.println("x = " + x);
System.out.println("y = " + y);
String input = "1 fish 2 fish red fish blue fish";
Scanner s = new Scanner(input).useDelimiter("\\s*fish\\s*");
//API์ฐธ๊ณ 
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
s.close();
}
}
import java.io.*;
import java.util.*;
public class Exam_13 {
public static void main(String [] ar) throws IOException{
if(ar.length < 1) System.exit(0); //ar๋ฐฐ์—ด์ด ์—†์œผ๋ฉด ์ข…๋ฃŒ
String str = "";
for(int i = 0; i < ar.length; ++i){
str += ar[i] + " ";
}
//System.out.println(str);
Scanner in = new Scanner(str);
int i = 0;
int tot = 0;
while(in.hasNext()){
i++;
//System.out.println(in.next());
tot += in.nextInt();
}
System.out.println("์ด ๊ฐœ์ˆ˜ = " + i + "๊ฐœ");
System.out.println("์ด ํ•ฉ
= " + tot + "์ ");
System.out.println("ํ‰ ๊ท 
= " + tot/i + "์ ");
//๋›ฐ์–ด ์“ฐ๊ธฐ๋Š” useDelimiter ์‚ฌ์šฉ์•ˆํ•ด๋„ ๊ธฐ๋ณธ์œผ๋กœ ๋œ๋‹ค.
}
}
8. ๊ฐ์ฒด ์ž…์ถœ๋ ฅ<์ฒ˜์Œ์œผ๋กœ>
//์ง์—ดํ™”
โ€ข
๊ฐ์ฒด ์ถœ๋ ฅ
import java.io.*;
class AAA implements
Serializable {
//๋ฐ˜๋“œ์‹œ ๊ฐ์ฒด๋ฅผ ์ง์—ดํ™” ์‹œํ‚ด
int x= 100;
int y = 200;
int z = 300;
//์ฝ๋Š” ์ชฝ์—๋„ ๋™์ผ ํด๋ž˜์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
}
public class Exam_14 {
public static void main(String [] ar) throws IOException{
AAA ap = new AAA();
File f = new File("ddd.txt");
FileOutputStream fos = new FileOutputStream(f);
BufferedOutputStream bos
= new BufferedOutputStream(fos, 1024);
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(ap);
oos.close();
System.out.println("์ถœ๋ ฅ์™„๋ฃŒ");
}
}
โ€ข
๊ฐ์ฒด ์ž…๋ ฅ
import java.io.*;
public class Exam_15 {
public static void main(String [] ar) throws IOException {
File f = new File("ddd.txt");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis
= new BufferedInputStream(fis,1024);
ObjectInputStream ois = new ObjectInputStream(bis);
Object obj = null;
try{
obj = ois.readObject();
} catch(ClassNotFoundException e){}
AAA ap = (AAA)obj;
System.out.println(ap.x);
System.out.println(ap.y);
System.out.println(ap.z);
}
}
9. ์—ฐ์‚ฐ์ž ์šฐ์„ ์ˆœ์œ„<์ฒ˜์Œ์œผ๋กœ>
์ตœ์šฐ์„  ์—ฐ์‚ฐ์ž - ๋‹จํ•ญ์—ฐ์‚ฐ์ž - ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž - ์‰ฌํ”„ํŠธ ์—ฐ์‚ฐ์ž - ๊ด€๊ณ„ ์—ฐ์‚ฐ์ž - ๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž - ์‚ผํ•ญ์—ฐ์‚ฐ์ž ๋ฐฐ์ •๋Œ€์ž…์—ฐ์‚ฐ์ž - ์ฆ๊ฐ ํ›„์œ„ ์—ฐ์‚ฐ์ž - ์ˆœ์ฐจ์—ฐ์‚ฐ์ž
1. .(์ ) , [], ()
2. !(๋…ผ๋ฆฌ๋ถ€์ •), ~(๋น„ํŠธ๋ถ€์ •), +,-(๋ถ€ํ˜ธ์—ฐ์‚ฐ), ++,-- , ํ˜„๋ณ€ํ™˜์—ฐ์‚ฐ โ€œ(cast ์ž๋ฃŒํ˜•)โ€,
Tvpe ๋น„๊ต : โ€œinstanceof" ์˜ˆ) Boolean bool = ex instanceof object
3. *, /, %(๋‚˜๋จธ์ง€) , +, 4. <<, >>, >>> ๋น„ํŠธ๋ฅผ ๋ฝ‘์„ ๋•Œ
โ€ป >>> : unsigned right shift ์—ฐ์‚ฐ์ž
์ด๋™ํ•˜๋ฉด ์•ž์ž๋ฆฌ๋Š” ๋ฌด์กฐ๊ฑด 0 ์œผ๋กœ ์ง€์ •ํ•ด์ค˜๋ผ
5. > < >= <= , == !=
6. ๋น„ํŠธ ์—ฐ์‚ฐ์ž : ๋น„ํŠธ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ โ€œ&โ€-and, "|"-or, "^"-XOR
๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž : && -and ||-or
๋น„๊ตํ•  ๋•Œ ๋น„ํŠธ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ž์—์„œ ์ƒํ™ฉํŒ๋‹จ์ด ๊ฐ€๋Šฅํ•ด๋„ ๋’ค์— ๊นŒ์ง€ ๊ณ„์‚ฐํ•œ๋‹ค.
7. ์กฐ๊ฑดํ•ญ ? ํ•ญ 1 : ํ•ญ 2
8. ๋ฐฐ์ • ๋Œ€์ž… ์—ฐ์‚ฐ์ž โ€œ+=โ€œ ๋“ฑ
9. ,(์ฝค๋งˆ) ์˜ˆ)byte b = 10, a = 20
10. ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ<์ฒ˜์Œ์œผ๋กœ>
String str = "ABC|DEF|GHI|JKL";
StringTokenizer tokens = new StringTokenizer(str,"|");
// import java.util.*; ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ธ€์ž๋ฅผ ํ† ๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆˆ๋‹ค
for(int i = 1;tokens.hasMoreTokens(); i++){
System.out.println("๋ฌธ์ž์—ด " + i + ":" + tokens.nextToken());
}
}
String str1 = "ABC|DEF|GHI|JKL";
String[] str2 = str1.split("|");
// split ์„ ์ด์šฉํ•œ ๋ฌธ์ž์—ด ๋‚˜๋ˆ„๊ธฐ
for(int i = 0 ;i < str2.length : i++){
System.out.println("๋ฌธ์ž์—ด " + i + ":" + str2[i]);
}
11. ๋ฌธ์ž์—ด ๊ฒ€์‚ฌ<์ฒ˜์Œ์œผ๋กœ>
a. equals(b); //๋ฌธ์ž์—ด a ์™€ b ๊ฐ€ ๊ฐ™์€๊ฐ€?
- ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๋ฌด์‹œํ•˜๊ณ  ๋น„๊ต ์œ„ํ•ด์„œ๋Š” equalsIgnoreCase()๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
โ€ป int -> String ๋กœ ๋ณ€๊ฒฝ
Integer.toString(int);
String str = "abc";
char[] data = {'a', 'b', 'c'};
String str = new String(data);
12. ๋ฌธ์ž์—ด์˜ NULL ๊ฐ’<์ฒ˜์Œ์œผ๋กœ>
public class StringTest {
public static void main(String[] args) throws java.io.IOException {
String a;
String b=null;
if (b == null) {
System.out.println("b ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค...");
}
//์•„๋ž˜์˜ ๊ฒฝ์šฐ Compile ์‹œ NullPointEXception ์ด ๋ฐœ์ƒํ•œ๋‹ค.
//System.out.println("b=null ์ผ๋•Œ b ์˜ ๊ธธ์ด -->"+b.length());
//์•„๋ž˜์˜ ๋‘๊ฒฝ์šฐ ๋ชจ๋‘ a ๊ฐ€ ์ดˆ๊ธฐํ™” ๋˜์ง€ ์•Š์•˜๋‹ค๋Š” ์˜ค๋ฅ˜๋ฐœ์ƒ
//if (a == "1234"){
//System.out.println("a ๋Š” '1234'์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค...");
//}
//if (a == null) {
// System.out.println("a ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค...");
//}
/////////////////////////////////////////////////////////
if (c == null) { System.out.println("c ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค..."); }
if (c == "") { System.out.println("c ๋Š” \"\" ์ž…๋‹ˆ๋‹ค...");}
if (c == " ") { System.out.println("c ๋Š” \" \" ์ž…๋‹ˆ๋‹ค...");}
if (c.equals(null)){ System.out.println("c ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค...");}
if (c.equals("")){ System.out.println("c ๋Š” \"\" ์ž…๋‹ˆ๋‹ค...");}
if (c.equals(" ")) { System.out.println("c ๋Š” \" \" ์ž…๋‹ˆ๋‹ค...");
}
//์•„๋ž˜์˜ ๊ฒฝ์šฐ c ๊ฐ€ ""์ด๋ฏ€๋กœ NullPointException ์ด ๋ฐœ์ƒํ•œ๋‹ค.
//if (c.compareTo("")==0){
// System.out.println("c ๋Š” \"\" ์ž…๋‹ˆ๋‹ค...");
//}
System.out.println("\"\"์˜ ๊ธธ์ด -->"+c.length());
/////////////////////////////////////////////////////////
String d=" ";
System.out.println(d.hashCode());
if (d == null) { System.out.println("d ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค..."); }
if (d == "") { System.out.println("d ๋Š” \"\" ์ž…๋‹ˆ๋‹ค...");}
if (d == " ") { System.out.println("d ๋Š” \" \" ์ž…๋‹ˆ๋‹ค...");}
if (d.equals(null)){ System.out.println("d ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค...");}
if (d.equals("")){ System.out.println("d ๋Š” \"\" ์ž…๋‹ˆ๋‹ค...");}
if (d.equals(" ")) { System.out.println("d ๋Š” \" \" ์ž…๋‹ˆ๋‹ค...");}
// ์ด ๊ฒฝ์šฐ์—๋„ NullPointException ์ด ๋ฐœ์ƒํ•œ๋‹ค.(์ฃผ์˜)
//if (d.compareTo(" ")==0){
// System.out.println("d ๋Š” \" \" ์ž…๋‹ˆ๋‹ค...");
//}
System.out.println("\" \"์˜ ๊ธธ์ด -->"+d.length());
d = d.trim();
System.out.println("trim ํ›„ \" \"์˜ ๊ธธ์ด -->"+d.length());
}
}
13. ๋ฌธ์ž์—ด์—์„œ ๋ฌธ์ž ์ถ”์ถœํ•˜๊ธฐ <์ฒ˜์Œ์œผ๋กœ>
- charAt() : ๋ณ€์œ„(์ฒ˜์Œ ๋ถ€ํ„ฐ ์–ผ๋งˆ๋‚˜ ๋–จ์–ด์ ธ ์žˆ๋‚˜)๋ฅผ ์ธ์ž๋กœ ์‚ฌ์šฉ
- length() : ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋ฅผ ๋Œ๋ ค์คŒ
(๋ฐฐ์—ด์˜ ๊ฒฝ์šฐ์—” ๋ฐ์ดํ„ฐ ๋ฉค๋ฒ„์ธ length ๋ฅผ ์‚ฌ์šฉ)
- indexOf() : ๋ฌธ์ž์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๋ณ€์œ„๊ฐ’์„ ๋Œ๋ ค์คŒ, ๋ชป์ฐพ์„๊ฒฝ์šฐ์—” โ€“1 Return
String text = โ€œI love koreaโ€;
int I = text.indexOf(โ€˜oโ€™); //โ€™oโ€™๊ฐ€ ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ ์ƒ‰์ธ ์ฐพ์•„์คŒ
int j = text.lastIndexOf(โ€˜oโ€™);//๊ฐ€์žฅ๋์˜ โ€˜oโ€™๋ฅผ ์ฐพ์•„์คŒ
int k= text.indexOf(โ€˜oโ€™,3); //startIndex ์ธ 3 ์ดํ›„ ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰
int l = text.indexOf(โ€œKoreaโ€); //Korea ๊ฐ€ ์žˆ๋Š” ์ฒซ๋ฒˆ์งธ ์ƒ‰์ธ
int m = text.indexOf(โ€œKoreaโ€,3); //startIndex ์ธ 3 ์ดํ›„ ๋ถ€ํ„ฐ ๊ฒ€์ƒ‰
- subString() : ๋ฌธ์ž์—ด ์ถ”์ถœ
String s = โ€œI love Koreaโ€;
String lastWord = s.subString(7); // 7 ๋ฒˆ์งธ ์ƒ‰์ธ ์ดํ›„ ์ „๋ถ€
String seg = s.subString(7,11);//7~11 ๋ฒˆ์งธ ์ƒ‰์ธ์˜ ๋ฌธ์ž Return
14. ๋ฌธ์ž์—ด ๊ฐ์ฒด ๋ณ€๊ฒฝํ•˜๊ธฐ(StringBuffer) <์ฒ˜์Œ์œผ๋กœ>
- replace() : ๋ฌธ์ž์—ด์„ ๋Œ€์ฒด์‹œํ‚ด
String text = โ€œI love Koreaโ€;
String newText = text.replace(โ€˜ โ€˜, โ€˜/โ€™);
//์•ž์— ์žˆ๋Š” ์ธ์ˆ˜๋Š” ๋ฐ”๋€” ๋ฌธ์ž ๋‘๋ฒˆ์งธ ์žˆ๋Š” ์ธ์ˆ˜๋Š” ๋ฐ”๊ฟ€ ๋ฌธ์ž
- trim() : ๋ฌธ์ž์—ด ์•ž๋’ค์˜ ๊ณต๋ฐฑ์ œ๊ฑฐ
String text = โ€œ I love Korea โ€œ;
String newText = text.trim();
StringBuffer
- String ๊ฐ์ฒด๋Š” ๊ฐ์ฒด๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ˆ˜์ •ํ•˜๋Š”๋“ฑ ๋ณ€๊ฒฝ์„ ๊ฐ€ํ• ์ˆ˜ ์—†๋Š” ๋ถˆ๋ณ€๋ฌธ์ž์—ด
StringBuffer ๋Š” ์ง์ ‘ ๋ณ€๊ฒฝ์„ ๊ฐ€ํ• ์ˆ˜ ์žˆ๋Š” ๊ฐ€๋ณ€๋ฌธ์ž์—ด ์ž„. ๋ฌธ์ž์—ด ๋ณ€ํ™˜์ž‘์—…
(์ถ”๊ฐ€, ์ž‘์ œ, ๋Œ€์ฒด๋“ฑโ€ฆ)์— ์šฉ์ดํ•จ
์ž๋ฐ”์—์„œ String ์— ๋Œ€ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ• ๋•Œ ๋‚ด๋ถ€์ ์œผ๋กœ๋Š” StringBuffer ์‚ฌ์šฉ
๋ฌธ์ž์—ด์„ ํฌํ•จํ• ์ˆ˜๋„, ์•ˆํ• ์ˆ˜๋„ ์žˆ๋Š” ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ๋ฒ„ํผ๋กœ ๊ตฌ์„ฑ
- ์ƒ์„ฑ๋ฐฉ๋ฒ•
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€); //์•„๋ž˜์™€ ๊ฐ™์€ ํšจ๊ณผ
StringBuffer sb = null; //๊ฐ์ฒด์ฐธ์กฐ๋ณ€์ˆ˜ sb ๋Š” ์•„๋ฌด๊ฒƒ๋„ ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š”๋‹ค.
sb = new StringBuffer(I love Korea);
- StringBuffer ์˜ ์šฉ๋Ÿ‰
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
int myLength = sb.length(); //12
int myCapacity = sb.capacity(); //24
StringBuffer sb = new StringBuffer(50); //์šฉ๋Ÿ‰์„ ์ง€์ •
์šฉ๋Ÿ‰์„ ์ž‘๊ฒŒ ์žก์•˜๋Š”๋ฐ, ํฐ ๋ฌธ์ž์—ด์ด ๋“ค์–ด์˜จ๋‹ค๋ฉด ์ž๋™์œผ๋กœ ํ™•์žฅํ•˜๊ฒŒ ๋˜์–ด
(๋ณ„๋„์˜ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ํ• ๋‹น) ์‹œ๊ฐ„์ด ๊ฑธ๋ฆฌ๊ฒŒ ๋œ๋‹ค.
- StringBuffer ๊ธธ์ด ๋ณ€๊ฒฝํ•˜๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
sb.setLength(6); //I love, ์ž˜๋ฆฐ ๋ถ€๋ถ„์€ ์—†์–ด์ง
sb.setLength(12); //๊ธธ์ด๋ฅผ ์ฆ๊ฐ€
//I love \u0000(6 ๊ฐœ)์™€ ๊ฐ™์ด \u0000 ์œผ๋กœ ์ฑ„์›Œ์ง„๋‹ค.
- ๊ฐ์ฒด์ถ”๊ฐ€
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
sb.append(โ€œ me too!โ€);
ํ˜น์€ ๋‹ค๋ฅธ StringBuffer ๊ฐ์ฒด์— ํ• ๋‹น๋„ ๊ฐ€๋Šฅ
StringBuffer sb2 = sb.append(me too!โ€);
- ๊ธฐ๋ณธ๋ฐ์ดํ„ฐ ์œ ํ˜• ๋ถ™์ด๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
int I=99;
StringBuffer sb = sb.append(I); //I love Korea99
sb.append(12,45); //I love Korea9912.45
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
char[] c = {โ€˜Aโ€™,โ€™Bโ€™,โ€™Cโ€™};
sb.append(c,1,2); //I love KoreaBC
- ๋ฌธ์ž์—ด ์‚ฝ์ž…ํ•˜๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
sb.insert(0, โ€œOh~โ€); //Oh~I love Korea
- ๋ฌธ์ž๋Œ€์ฒดํ•˜๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
sb.setCharAt(2, โ€˜Lโ€™); //I Love Korea
- ๋ฌธ์ž์—ด ๋’ค์ง‘๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
sb.reverse(); //aeroK evol I
- StringBuffer ๋ฅผ String ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ธฐ
StringBuffer sb = new StringBuffer(โ€œI love Koreaโ€);
String s = sb.toString();
- String ์€ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ์ƒˆ๋กœ์šด String ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. ์ฆ‰, ํ•˜๋‚˜์˜ String ๊ฐ์ฒด๋Š” ์‚ฌ์‹ค์ƒ
๋” ์ด์ƒ ๋ณ€๊ฒฝ์ด ๋˜์ง€ ์•Š๋Š” ๊ฒƒ์ด๋‹ค. ๋ฐ˜๋ฉด, StringBuffer ๋Š” ํ•˜๋‚˜์˜ ๊ฐ์ฒด์— ์ƒˆ๋กœ์ด ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๊ณ 
๋ณ€๊ฒฝํ• 
์ˆ˜
์žˆ๋Š”
์žฅ์ ์ด
์žˆ๋‹ค.
๊ทธ๋Ÿฌ๋ฏ€๋กœ
ํ•˜๋‚˜์˜
๋ฌธ์ž์—ด์—
์žฆ์€
๋ณ€๊ฒฝ์ด
์žˆ๋Š”
๊ฒฝ์šฐ์—๋Š”
StringBuffer ๋ฅผ ์‚ฌ์šฉํ•จ์ด ์žฆ์€ ๊ฐ์ฒด ์ƒ์„ฑ์„ ๋ง‰๊ณ , ๋ฉ”๋ชจ๋ฆฌ ํšจ์œจ์„ฑ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค.
15. String ์œผ๋กœ๋ถ€ํ„ฐ ๋ฌธ์ž๋ฐฐ์—ด ๋งŒ๋“ค๊ธฐ <์ฒ˜์Œ์œผ๋กœ>
- toCharArray() : char ์œ ํ˜•์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ ๋‹ค.
char[] charArray = text.toCharArray();
- getChars(int p1, int p2, char[] p3, int p4) : ๋ฌธ์ž์—ด์„ ๋ฌธ์ž๋ฐฐ์—ด๋กœ ์ถ”์ถœ
p1 : ์ฒ˜์Œ์ƒ‰์ธ, p2 : ๋งˆ์ง€๋ง‰์ƒ‰์ธ, p3 : ๋ฐฐ์—ด์ด๋ฆ„, p4 : ๋ฐฐ์—ด์˜ ์‹œ์ž‘์ƒ‰์ธ
String text = โ€œI love Koreaโ€;
char[] charArray = new char[3];
text.getChars(0, text.length()-1, charArray, 0);
- getBytes() : String ์„ ํ˜„์žฌ ์‹œ์Šคํ…œ์˜ ์ธ์ฝ”๋”ฉ์„ ๋”ฐ๋ผ 8 ๋น„ํŠธ๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฌธ์ž
๋ฅผ byte ํ˜•ํƒœ๋กœ ์ถ”์ถœ
String text = โ€œI love Koreaโ€;
byte[] byteArray = text.getBytes();
char[] ์œ ํ˜•์˜ ๋ฐฐ์—ด์„ String ๊ฐ์ฒด๋กœ ๋งŒ๋“ค๊ธฐ
- copyValueOf() : static Method
char[] c = {โ€˜Aโ€™,โ€™Bโ€™,โ€™Cโ€™};
String text = String.copyValueOf(c);
16. ๊ธฐ๋ณธํด๋ž˜์Šค(์ ‘๊ทผ์ œํ•œ์ž, ์ง€์ •์˜ˆ์•ฝ์–ด)<์ฒ˜์Œ์œผ๋กœ>
this ์ •์˜ : ํด๋ž˜์Šค ๋‚ด๋ถ€์—์„œ ์ž์‹ ์˜ ํด๋ž˜์Šค๋ฅผ ์ง€์นญํ•˜๋Š” ๊ฐ์ฒด
this() ์ •์˜ : ์ƒ์„ฑ์ž ๋‚ด๋ถ€์—์„œ ์ž์‹  ํด๋ž˜์Šค์˜ ๋˜ ๋‹ค๋ฅธ ์ƒ์„ฑ์ž๋ฅผ ์ง€์นญํ•˜๋Š” ๋ฉ”์„œ๋“œ
๋ฉ”์„œ๋“œ์˜ 0 ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜
์ž์‹ _ํด๋ž˜์Šค๋ช… this this ๋Š” ์˜ˆ์•ฝ์–ด
0 ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ‘œ๊ธฐํ•  ์ˆ˜ ์—†๋‹ค.
ํด๋ž˜์Šค๋Š” ์ƒ์„ฑ์ž๊ฐ€ ํ•„์š”ํ•œ๋‹ค. ์ƒ์„ฑ์ž๋Š” ํด๋ž˜์Šค๋ช…๊ณผ ๋™์ผ ์ƒ์„ฑ์ž๋Š” ์—ฌ๋Ÿฌ๊ฐœ ๊ฐ€๋Šฅํ•˜๋‚˜ ๋งค๊ฐœ๋ณ€์ˆ˜๊ฐ€
๋‹ฌ๋ผ์•ผํ•œ๋‹ค. ์ƒ๋žตํ•˜๋ฉด ๊ธฐ๋ณธ ํด๋ž˜์Šค๊ฐ€ ์ž๋™์œผ๋กœ ์„ค์ •๋œ๋‹ค.
์˜ˆ)
public class Bog(){
Bog(){}//์ƒ๋žต๋˜์–ด์žˆ๋‹ค.
}
public static void main(String[]ar){
Bog ๋ณ€์ˆ˜ = new Bog()
}
Garbage Collection
null ํ˜ธ์ถœ ๋˜๋ฉด ๋งŒ์•ฝ ํ•œ ๊ฐœ๋ผ๋„ ์ฐธ์†Œ๋˜๋ฉด ํ˜ธ์ถœ์ด ๋˜์ง€ ์•Š๋Š”๋‹ค.
์ ‘๊ทผ ์ œํ•œ์ž
- private : ํ•˜๋‚˜์˜ ํด๋ž˜์Šค ๋‚ด์—์„œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ฉค๋ฒ„๋ฅผ ์ง€์ •ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์ œํ•œ์ž
- package : ๋™์ผ ํŒŒ์ผ + ๋™์ผ ํด๋”
- protected : ๋™์ผ ํŒŒ์ผ + ๋™์ผ ํด๋” + ์ƒ์†
- public : ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง„ ๋ชจ๋“  ์˜์—ญ
๋ฉค๋ฒ„ ํ•„๋“œ(Member Field)
-ํ˜•์‹ : ์ ‘๊ทผ์ œํ•œ์ž ์ง€์ •์˜ˆ์•ฝ์–ด ์ž๋ฃŒํ˜• ํ•„๋“œ๋ช… = ๊ฐ’
๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ(Member Method)
-ํ˜•์‹ : ์ ‘๊ทผ์ œํ•œ์ž ์ง€์ •์˜ˆ์•ฝ์–ด ๊ฒฐ๊ณผํ˜•๋ฆฌํ„ด๊ฐ’
๋ฉ”์„œ๋“œ๋ช…(๋งค๊ฐœ๋ณ€์ˆ˜๋“ค) throws ์˜ˆ์™ธํด๋ž˜์Šค๋“ค {
๋‚ด์šฉ๋ถ€โ€ฆ
}
์ง€์ •์˜ˆ์•ฝ์–ด
static ํ•„๋“œ : ๋ฐ์ดํ„ฐ ๊ฐ’์˜ ๊ณต์œ ๋ฅผ ์œ„ํ•ด ์„ ์–ธํ•˜๋Š” ๊ณต๊ฐ„ (๋ฉค๋ฒ„ํ•„๋“œ๋กœ๋งŒ ๊ฐ€๋Šฅ)
-ํด๋ž˜์Šค ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ ๊ฐ€๋Šฅ
-๊ฐ์ฒด ๋ฐœ์ƒ ์ „ ๋ฉ”๋ชจ๋ฆฌ ํ• ๋‹น
static ์ดˆ๊ธฐํ™” ์˜์—ญ : static ๋ฉค๋ฒ„ ํ•„๋“œ์˜ ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•˜๊ธฐ ์œ„ํ•œ ์˜์—ญ
์ƒ์„ฑ์ž์—์„œ ์ดˆ๊ธฐํ™” ํ•˜์ง€ ์•Š๊ณ  ์‹œ์ž‘ํ•  ๋•Œ ํ˜•์‹์— ๋”ฐ๋ผ ์‚ฌ์šฉ
ํ˜•์‹) static { ์ดˆ๊ธฐํ™” ๊ตฌ๋ฌธโ€ฆ }
static ๋ฉ”์„œ๋“œ : static ํ•„๋“œ ์ปจํŠธ๋กค ๋ชฉ์ 
final ํ•„๋“œ : ์ƒ์ˆ˜ ๊ฐ’์„ ์ €์žฅํ•˜๊ธฐ ์œ„ํ•œ ๊ณต๊ฐ„์œผ๋กœ ๊ฐ’์ด ๋ณ€๊ฒฝ๋  ์ˆ˜ ์—†์Œ
-์ง€์—ญ๋ณ€์ˆ˜๋‚˜ ๋ฉค๋ฒ„ ํ•„๋“œ ๋ชจ๋‘ ๊ฐ€๋Šฅ
final ๋ฉ”์„œ๋“œ : ์ƒ์† ์‹œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋˜์ง€ ์•Š์•„์•ผ ํ•  ๋ฉ”์„œ๋“œ๋ฅผ ์ง€์ •
-๋งŒ์•ฝ overriding ํ•˜๊ฒŒ ๋˜๋ฉด ์—๋Ÿฌ ๋ฐœ์ƒ
transient : ์ง๋ ฌํ™” ๋˜์ง€ ์•Š๋Š” ๋ฉค๋ฒ„ Data ์ง€์ •
instanceof : ํŠน์ • ํด๋ž˜์Šค์˜ instance ์—ฌ๋ถ€๋ฅผ ์ฒดํฌ
ex) A ap = new A();
boolean a = ap instanceof a;
ap ๊ฐ€ a ์˜ ์ธ์Šคํ„ด์Šค์ธ๊ฐ€?
17.์ž๋ฐ” 4 ๋Œ€ ํด๋ž˜์Šค <์ฒ˜์Œ์œผ๋กœ>
1. ์ค‘์ฒฉ ํด๋ž˜์Šค(Inner Class)
-ํด๋ž˜์Šค ๋‚ด๋ถ€์— ๋˜ ๋‹ค๋ฅธ ํด๋ž˜์Šค๋ฅผ ๊ฐ€์ง์œผ๋กœ ํด๋ž˜์Šค ๊ด€๋ฆฌ์˜ ํšจ์œจ์„ ๋†’์ธ ๊ฒƒ
(static ํฌํ•จ๋ถˆ๊ฐ€)
์ค‘์ฒฉ ํด๋ž˜์Šค์˜ ํ˜•์‹๊ณผ ์ƒ์„ฑํŒŒ์ผ
ํ˜•์‹) class Outer { class Inner { โ€ฆ } }
์ƒ์„ฑํŒŒ์ผ) Outer.class, Outer$Inner.class
์ค‘์ฒฉ ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
Outer.Inner oi = new Outer().new Inner();
์˜ˆ)
class Outer{
private int x= 100;
private static int y = 200;
public Outer(){}
public void disp(){
System.out.println("x = "+x);
System.out.println("y = "+y);
}
class Inner{
private int a= 100;
public Inner(){}
public void disp_in(){
System.out.println("a = "+a);
disp();
}
class Other_Inner{};
}
}
public class Exam_01 {
public static void main(String [] ar){
Outer ot = new Outer();
Outer.Inner oa = ot.new Inner();
oa.disp_in();
}
}
2. ์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค(static Inner Class)
-์ค‘์ฒฉ ํด๋ž˜์Šค ๋‚ด๋ถ€์— static ๋ฉค๋ฒ„๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋Š” ํ˜•ํƒœ(Outer ์˜ non-static ๋ฉค๋ฒ„ ํฌํ•จ ๋ถˆ๊ฐ€)
์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค์˜ ํ˜•์‹๊ณผ ์ƒ์„ฑํŒŒ์ผ
-ํ˜•์‹) class Outer { static class Inner {โ€ฆ} }
-์ƒ์„ฑํŒŒ์ผ) Outer.class, Outer$Inner.class
์ •์  ์ค‘์ฒฉ ํด๋ž˜์Šค ๊ฐ์ฒด ์ƒ์„ฑ
-Outer.Inner oi = new Outer.Inner();
์˜ˆ)
class Outer1{
private int x = 100;
private static int y = 200;
public Outer1(){}
public void disp(){
System.out.println("x = "+x);
System.out.println("y = "+y);
}
static class Inner1{
private int a= 10;
static int b = 20;
public void disp_in(){
System.out.println("a = "+a);
System.out.println("b = "+b);
//System.out.println("x = "+x); non-static
System.out.println("y = "+y);
}
}
}
public class Exam_02 {
public static void main(String[] ar){
Outer1.Inner1 oi = new Outer1.Inner1();
oi.disp_in();
System.out.println("b = "+ Outer1.Inner1.b);
}
}
3. ์ง€์—ญ ์ค‘์ฒฉ ํด๋ž˜์Šค(local Inner class)
๋ฉ”์„œ๋“œ ์‹คํ–‰ ์‹œ์— ์‚ฌ์šฉ๋˜๋Š” ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•œ ํ˜•์‹์œผ๋กœ ์ ‘๊ทผ์ž๋‚˜ ์ง€์ •์–ด๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์—†๋‹ค.
์ง€์—ญ ์ค‘์ฒฉ ํด๋ž˜์Šค์˜ ํ˜•์‹ ๋ฐ ์ƒ์„ฑ ํŒŒ์ผ
-ํ˜•์‹) class Outer { method() { class Inner { โ€ฆ } } }
-์ƒ์„ฑํŒŒ์ผ) Outer.class, Outer$์ˆซ์ž Inner.class
๊ฐ์ฒด ์ƒ์„ฑ์€ ์™ธ๋ถ€์—์„œ ํ•  ์ˆ˜ ์—†๋‹ค.
class Outer2{
private int x = 100;
private static int y = 200;
public void disp(){
class Inner2{
private int a =10;
//private static int b =20;
public void disp_in(){
System.out.println("a = " + a);
System.out.println("x = " + x);
System.out.println("y = " + y);
}
}
Inner2 in = new Inner2();
in.disp_in();
}
}
public class Exam_03 {
public static void main(String[]ar){
Outer2 ot = new Outer2();
//Outer2.Inner2 oi = ot.new Inner2();
ot.disp();
}
}
4. ์ต๋ช… ์ค‘์ฒฉ ํด๋ž˜์Šค(anonymous Inner class)
โ€ข
์ต๋ช… ์ค‘์ฒฉ ํด๋ž˜์Šค(Anonymous Inner Class)
โ€“
๊ธฐ์กด ํด๋ž˜์Šค์˜ ํŠน์ • ๋ฉ”์„œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ์žฌ์ •์˜ ํ•˜์—ฌ
์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹
โ€“
โ€ข
์™ธ๋ถ€ ๋ฉค๋ฒ„ ์ค‘ final ๋งŒ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.
์ต๋ช… ์ค‘์ฒฉ ํด๋ž˜์Šค์˜ ํ˜•์‹ ๋ฐ ์ƒ์„ฑํŒŒ์ผ
โ€“
ํ˜•์‹) class Inner { โ€ฆ }
class Outer { method() { new Inner() {โ€ฆ}}}
โ€“
โ€ข
์ƒ์„ฑํŒŒ์ผ) Outer.class, Outer$์ˆซ์ž.class
new Inner() ์ž์ฒด๊ฐ€ ๊ฐ์ฒด ์ƒ์„ฑ์ž„.
class Inner3{
public void aaa(){
System.out.println("aaa");
}
public void bbb(){
System.out.println("bbb");
}
public void ccc(){
System.out.println("ccc");
}
}
public class Exam_04 {
public static void main(String[]ar){
final int x = 100;
Inner3 in = new Inner3(){
public void bbb(){
System.out.println("ddd");
System.out.println("x = " + x);
ddd();//ddd()๋Š” ์žฌ์ •ํ•  ํด๋ž˜์Šค์—์„œ ์‚ฌ์šฉ
//์žฌ์ •
}
public void ddd(){
System.out.println("ddd");
}
};
in.aaa();
in.bbb();
in.ccc();
//in.ddd(); ์•ˆ๋จ
}
}
18. ์ƒ์†๊ณผ ๋‹ค์–‘์„ฑ <์ฒ˜์Œ์œผ๋กœ>
1.
Object์™€ super
๋ชจ๋“  ์ž๋ฐ” ํด๋ž˜์Šค(๋‹จ interface)๋Š” Object์—์„œ ์ƒ์† ๋ฐ›๋Š”๋‹ค.
ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด๋„ Object์—์„œ ์ƒ์† ๋ฐ›๋Š”๋‹ค.
โ˜…์ตœ์ƒ์˜ ํด๋ž˜์Šค : java.lang.Object
์ฐธ๊ณ 
getClass() ํ•ด๋‹น ๊ฐ์ฒด์˜ ํด๋ž˜์Šค์ด๋ฆ„๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.
toString() ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์–ด์ค๋‹ˆ๋‹ค.(์ƒ๋žต๊ณผ ๊ฐ™์Œ)
class A extends Object ์ด ์ž๋™์œผ๋กœ ๋œ๋‹ค.
โ€ข
ํฌํ•จ Object์˜ ๋‹จ์  : ๊ฐ์ฒด๋ช…์˜ ์ ‘๊ทผ
์ƒ์†์˜ ์˜ˆ
Class A extends b
โ€ข
์ž๋ฐ”์—์„œ ์ƒ๋žต๋œ ํ˜•ํƒœ๋“ค
โ€“
import, = import java.lang.*;
โ€“
default์ƒ์„ฑ์ž,= class aaa{ aaa(){} }
โ€“
this, = this.x
โ€“
0๋ฒˆ์งธ๋งค๊ฐœ๋ณ€์ˆ˜, = ์ด๋กœ์ธ๋ฐ this์‚ฌ์šฉ ๊ฐ€๋Šฅํ•จ public void disp(/*aaa this */)
โ€“
toString(), = ํด๋ž˜์Šค ์ด๋ฆ„
โ€“
Object, = ์ƒ์†
โ€“
super() ์ƒ์† ๋ฐ›์€ ์ƒ์œ„ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜ํ•จ this() ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ {} ์‚ฌ์ด ์ฒ˜์Œ์— ๋“ค์–ด
์™€์•ผํ•œ๋‹ค. ๋งค๊ฐœ๋ณ€์Šค๋กœ๋„ ์ ‘๊ทผ๊ฐ€๋Šฅ ๋งค๊ฐœ๋ณ€์Šค๊ฐ€ ์—†์œผ๋ฉด ์ƒ๋žต๊ฐ€๋Šฅ
private ์ ‘๊ทน์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ƒ์†์„ ๋ฐ›์•„๋„ ์‚ฌ์šฉ์„ ๋ชปํ•œ๋‹ค.
โ€ข
ํ•„๋“œ์˜ ์ƒ์†๊ณผ ์ ‘๊ทผ์ž : protected ์Šต๊ด€์ ์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
โ€ข
super์™€ this ์˜ˆ์•ฝ์–ด๋ฅผ ํ†ตํ•œ ์ ‘๊ทผ
( ์ƒ๋žต ์‹œ ์ž๋™ this ์ธ์‹ ํ•˜์œ„์— ๋™์ผํ•œ ๋ณ€์ˆ˜๊ฐ€ ์—†์„
๋•Œ๋Š” ๋‘๊ฐœ์˜ ์˜ˆ์•ฝ์–ด๋ฅผ ๋‹ค ์‚ฌ์šฉ๊ฐ€๋Šฅํ•œ๋‹ค.)
๋ฉ”์„œ๋“œ์˜ ์ƒ์†๊ณผ ๋ฉ”์„œ๋“œ ์žฌ์ •์˜
โ€“
๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ : ์ ‘๊ทผ์ž์™€ ์˜ˆ์™ธ์ „๊ฐ€
๋ฉ”์„œ๋“œ ์˜ค๋ฒ„๋กœ๋”ฉ : ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐœ์ˆ˜์™€ ์ž๋ฃŒํ˜•
class FF {
private int x;
private int y;
public void setXY(int x){
this.x = x;
this.y =100;
}
public void setXY(int x, int y){
//์˜ค๋ฒ„๋กœ๋”ฉ
๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋•Œ ๋งค๊ฐœ๋ณ€์ˆ˜์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋ฉด ์‚ฌ์šฉ ๊ฐ€๋Šฅ๊ฐ€๋‹ค
this.x = x;
this.y = y;
}
}
class HH extends FF {
public void setXY(int x){
//์ƒ์†๊ด€๊ณ„์—์„œ ์ƒ์œ„ํด๋ž˜์Šค์˜ ๋˜๊ฐ™์€ ๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ค๋ฒ„๋ผ์ด๋”ฉ
}
}
public class Exam_09 {
public static void main (String [] ar){
FF fp = new FF();
fp.setXY(100, 200);//์˜ค๋ฒ„๋กœ๋”ฉ
}
}
class GG{
public void aaa(){
System.out.println("GG");
}
}
class JJ extends GG{
public void aaa(){
System.out.println("JJ");
}
}
public class Exam_10 {
public static void main(String[]ar){
JJ jp = new JJ();
jp.aaa();
}
}
final ๋ฉ”์„œ๋“œ์˜ ์ œ์•ฝ
final class GG{//์ƒ์†์„ ๋ชปํ•˜๊ฒŒ
public final void aaa(){//์ƒ์†์ด ๊ฐ€๋Šฅํ•˜๋ฉด ์˜ค๋ฒ„๋ผ์ด๋”ฉ์„ ๋ชปํ•˜๊ฒŒ
System.out.println("GG");
}
}
class JJ extends GG{
public void aaa(){
System.out.println("JJ");
}
}
public class Exam_11 {
public static void main(String[]ar){
JJ jp = new JJ();
jp.aaa();
}
}
์ ‘๊ทผ์ œ์•ฝ์˜ ๊ทœ์น™
1.์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ๋•Œ ์ƒ์œ„ํด๋ž˜์Šค๋ณด๋‹ค ์ ‘๊ทผ์ž๊ฐ€ ๊ฐ™๊ฑฐ๋‚˜ ๋†’์•„์•ผํ•œ๋‹ค.
private < package < protected < public
2. ์˜ค๋ฒ„๋ผ์ด๋”ฉ ํ•  ๋•Œ ์ƒ์œ„ํด๋ž˜์Šค์—์„œ ์˜ˆ์˜์ „๊ฐ€๊ฐ€ ์•ˆ๋˜์–ด ์žˆ๋Š”๋ฐ ํ•˜์œ„ํด๋ž˜์Šค์—์„œ ํ•˜์ง€ ๋ชปํ•œ๋‹ค.
๋‹ค์–‘์„ฑ?
- ๋ถ€๋ชจ ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ์ž์‹ ๊ฐ์ฒด๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
class TT{
protected int x =100;
}
class RR extends TT{
int y = 300;
}
class WW extends TT{
int z= 400;
}
public class Exam_12 {
public static void main(String[]ar){
TT rp = new RR();
System.out.println("rp.x=" + rp.x);
TT wp = new WW();
System.out.println("wp.x=" + wp.x);
}
}
์˜ˆ)
class AAAA{
public String toString(){
return "AAAA";
}
}
class BBBB{
public String toString(){
return "BBBB";
}
}
public class Exam_13 {
public static void main(String[]ar){
Object[] obj = new Object[2]; //๋ถ€๋ชจ์ด๋ฆ„์œผ๋กœ ๊ด€๋ฆฌ
obj[0] = new AAAA();
obj[1] = new BBBB();
for(int i = 0; i < obj.length; ++i){
System.out.println("obj["+i+"]="+obj[i]);
}
}
โ€ข
๋ฉค๋ฒ„ ํ•„๋“œ์˜ ๋‹คํ˜•์„ฑ ํ‘œํ˜„
โ€“ ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ํ•„๋“œ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ ํ•˜์ดํด๋ž˜์Šค์˜ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ ๋ชปํ•จ
โ€“ ํ•˜์ด ํด๋ž˜์Šค ์ด๋ฆ„ ์ถœ๋ ฅ ๋“ฑ์—์„œ ์‚ฌ์šฉ ํ•œ๋‹ค.
โ€ข
๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ์˜ ๋‹คํ˜•์„ฑ ํ‘œํ˜„
โ€“ ์ž์‹ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ๋จผ์ € ์ˆ˜ํ–‰
โ€“ ์ž์‹ ๊ฐ์ฒด์— ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์—†๋‹ค๋ฉด ๋ถ€๋ชจ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ์‹คํ–‰
class FFFF{
int x = 100;
int y = 200;
public void aaa(){
System.out.println("aaa");
}
}
class HHHH extends FFFF{
int x = 300;
int z = 400;
public void aaa(){
System.out.println("aaaaaa");
}
public void xxx(){
System.out.println("xxx");
}
}
public class Exam_14 {
public static void main(String[]ar){
FFFF ff = new HHHH();
System.out.println(ff.x); //ffff.x ์ถœ๋ ฅ HHHH.z ๋Š” ์‚ฌ์šฉ ๋ชปํ•จ ํ•„๋“œ๋Š” ์ƒ์œ„ํด๋ž˜์Šค
ff.aaa(); ///HHHH.aaa(); ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค. ๋ฉ”์„œ๋„๋Š” ์ž์‹ํด๋ž˜์Šค
ff.xxx(); ////์‚ฌ์šฉ ๋ชปํ•จ
}
}
19. abstract(์ถ”์ƒ์ ) ์™€ interface<์ฒ˜์Œ์œผ๋กœ>
= ๋””์ž์ธ ๊ฐœ๋…
โ€ข
abstract ๋ฉ”์„œ๋“œ
โ€“
๋ฉ”์„œ๋“œ์˜ ๋‚ด์šฉ๋ถ€๊ฐ€ ์ •์˜ ๋˜์ง€ ์•Š์€ ํ˜•ํƒœ๋กœ ๋ชจ๋ธ ๊ฐœ๋…์˜ ๋ฉ”์„œ๋“œ
public abstract void aaa();
{}๋‚ด์šฉ์ •์˜๋ถ€๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ๋ชปํ•œ๋‹ค. ๋‹จ์ง€ ๋””์ž์ธ ํ•˜๋Š”๊ฒƒ๊ณผ ๊ฐ™๋‹ค.
โ€“
๋ฐ˜๋“œ์‹œ ์˜ค๋ฒ„๋ผ์ด๋”ฉ ๋˜์–ด์•ผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
abstract class AA{
public abstract void aaa(); //์ถ”์ƒํด๋ž˜์Šค
protected int x=100;
}
class BB extends AA{
public void aaa(){
}
}
public class Exam_01 {
public static void main(String[]ar){
//AA ap = new AA(); abstract ๋จ
BB bp = new BB();
System.out.println(bp.x);
}
}
โ€ข
abstract ํด๋ž˜์Šค
โ€“
abstract ๋ฉ”์„œ๋“œ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋กœ ๋‹คํ˜•์„ฑ ํ‘œํ˜„์œผ๋กœ ์‚ฌ์šฉ
โ€“
๊ฐ์ฒด๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ์ˆ˜ ์—†๋Š” ๊ฒƒ์„ ์ œ์™ธํ•˜๋ฉด ์ผ๋ฐ˜ ํด๋ž˜์Šค์™€ ๋™์ผ
abstract class AA{
public abstract void aaa();
protected int x=100;
}
class BB extends AA{
public void aaa(){
System.out.println("aaa");
}
}
public class Exam_01 {
public static void main(String[]ar){
//AA ap = new AA(); abstract ๋จ
BB bp = new BB();
AA Ap = new BB();
Ap.aaa();
System.out.println(bp.x);
}
}
โ€ข
interface : (์ตœ์ƒ์œ„์—๋งŒ ์‚ฌ์šฉ)
โ€“
abstract ํด๋ž˜์Šค์˜ ํ•œ ์ข…๋ฅ˜๋กœ ํฌํ•จ ๋ฉค๋ฒ„์˜ ์ œ์•ฝ์„ ๊ฐ€์ง(์ˆœ์ˆ˜ ๋””์ž์ธ ๋ชฉ์ )
โ€“
๋‹ค์ค‘ ์ƒ์†์ด ๊ฐ€๋Šฅํ•œ ์œ ์ผํ•œ ํด๋ž˜์Šค abstract ๋‹ค๋ฅธ์ 
Inner ํด๋ž˜์Šค๋งŒ {} ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค
-
๋‹ค๋ฅธ ์ข…์กฑ ์ƒ์† ๋ฐ›์„๋ ค๋ฉด
class BBBB implements aaa {}
์˜ˆ)
interface E {}
interface F extends E{}
interface G{}
interface H extends E,G{}
class J {}
class I implements E,G{}
class K extends I implements E, G{}
โ€ข
interface์˜ ํฌํ•จ ๋ฉค๋ฒ„(์ž๋™ ์ง€์ •)
โ€“
public static final ๋ฉค๋ฒ„ ํ•„๋“œ
โ€“
public abstract ๋ฉค๋ฒ„ ๋ฉ”์„œ๋“œ (๋‹ค์–‘์„ฑ)
โ€“
public static inner ํด๋ž˜์Šค
20. ์Šค๋ž˜๋“œMulti-Thread<์ฒ˜์Œ์œผ๋กœ>
Multi-Thread์™€ API
Thread์˜ ์ •์˜
โ€ข
์‹œ์ž‘์ ๊ณผ ์ง„ํ–‰ ๊ทธ๋ฆฌ๊ณ  ์ข…๋ฃŒ์ ์„ ๊ฐ€์ง€๋Š” ์ผ๋ จ๋œ ํ•˜๋‚˜์˜ ์ž‘์—… ํ๋ฆ„
โ€“
Thread ์ƒ์„ฑ ํด๋ž˜์Šค : Thread, Runnable
โ€ข
class A extends Thread{//๋‹จ์ผ์ƒ์†
public void run(){//main Method ์ด๋‹ค
System.out.println("A");
}
}
class B extends Object implements Runnable{//๋‹ค์ค‘์ƒ์†
public void run(){//main Method ์ด๋‹ค
System.out.println("B");
}
}
public class Exam_01 {
public static void main(String[]ar){
System.out.println("main start!");
A ap = new A();
//ap.run();//Method ํ˜ธ์ถœ์ด์ง€ ์Šค๋ž˜๋“œ๊ฐ€ ์•„๋‹˜
ap.start();//Thread{
B bp = new B();
Thread th = new Thread(bp);
//bp.run();//Method ํ˜ธ์ถœ
th.start();
for(int i = 1; i<100;i++){
System.out.print(i);
if(i% 10 ==0) System.out.println();
else System.out.print("\t");
}
}
}
์ฃผ์š” ๋ฉ”์†Œ๋“œ
activeCount
() ๋ช‡ ๊ฐœ์˜ Thread๊ฐ€ ์‹œํ–‰ ๋˜๋Š”์ง€ ์•Œ๋ ค์ค€๋‹ค.
int x = Thread.activeCount();
System.out.println("Thread๋Š” "+ x + "๊ฐ€ ์‹คํ–‰๋˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค..");
currentThread
() ํ˜„์žฌ ์‹คํ–‰๋˜๋Š” thread ๊ฐ์ฒด์˜ ๊ฐ’์œผ๋กœ ๋„˜๊ฒจ์˜จ๋‹ค
System.out.println("cur Thread = "+Thread.currentThread());
//๊ฒฐ๊ณผ Thread[Thread-0,5,main] 0๋ฒˆ์งธ ์Šค๋ž˜๋“œ์ด๋ฆ„, ์šฐ์„ ์ˆœ์œ„ 5(๊ธฐ๋ณธ),์–ด๋–ค์Šค๋ž˜๋“œ๋กœ ํ–ˆ๋Š”๊ฐ€)
getName
() ์ด thread์˜ ์ด๋ฆ„์„ ๋Œ๋ ค์ค๋‹ˆ๋‹ค.. Thread-0
() ์ด thread์˜ ์šฐ์„  ์ˆœ์œ„๋ฅผ ๋Œ๋ ค์ค๋‹ˆ๋‹ค. ๊ธฐ๋ณธ 5
getPriority
enumerate (Thread
[] tarray) ํ˜„์žฌ ๋™์ž‘ํ•˜๋Š” Thread ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค
Thread[] th = new Thread[Thread.activeCount()];
Thread.enumerate(th);
for(int i = 0; i < th.length; ++i){
System.out.println("i"+th[i]);
}
setPriority
(int newPriority) ์ด thread์˜ ์šฐ์„  ์ˆœ์œ„๋ฅผ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.
A ap = new A()
ap.setPriority(10); or ap.setPriority(Thread.MIN_PRIORITY);
interrupt
()
์ด thread์— ๋ผ์–ด๋“ญ๋‹ˆ๋‹ค. (์ค‘์ง€)
Thread.currentThread().interrupt();
๊ฐ์ฒด.interrupt();
interrupted
isAlive
()
currentThread
() ๊ฐ€ ๋ผ์–ด๋“ค์–ด์ง€๊ณ  ์žˆ๋Š”์ง€ ์–ด๋–ค์ง€๋ฅผ ์กฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.
() ์ด thread๊ฐ€ ์ƒ์กดํ•˜๊ณ  ์žˆ์„์ง€ ์–ด๋–จ์ง€๋ฅผ ํŒ์ •ํ•ฉ๋‹ˆ๋‹ค.
isDaemon
() ์ด thread๊ฐ€ demon thread์ผ์ง€ ์–ด๋–จ์ง€๋ฅผ ํŒ์ •ํ•ฉ๋‹ˆ๋‹ค.
ap.setDaemon(true);
isInterrupted
join
() ์ด thread๊ฐ€ ์ข…๋ฃŒํ•˜๋Š” ๊ฒƒ์„ ๋Œ€๊ธฐํ•ฉ๋‹ˆ๋‹ค.
sleep
yield
()interrupt๊ฐ€ ์ข…๋ฃŒ๋˜์—ˆ๋Š”์ง€ ๊ฒ€์‚ฌ
(long millis) ์ „์ฒด ์Šค๋ž˜๋“œ๋ฅผ ๋Œ€๊ธฐ(์ง€์—ฐ) ์‹œํ‚ด
() (์–‘๋ณด)
ํ˜„์žฌ ์‹คํ–‰์ค‘์˜ thread ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ํœด์ง€์‹œ์ผœ, ๋‹ค๋ฅธ thread๋ฅผ ์‹คํ–‰ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ฉ
๋‹ˆ๋‹ค.
์‹œ๊ฐ„ํ”„๋กœ๊ทธ๋žจ
import java.util.*;
import java.text.*;
class AAA extends Thread {
public void run(){
while(true){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss");
Date d = new Date();
String str = format.format(d);
System.out.println("ํ˜„์žฌ ์‹œ๊ฐ„ : "+ str);
try{
Thread.sleep(1000);
}catch(InterruptedException e){}
}
}
}
public class Exam_03 {
public static void main(String[] ar){
System.out.println("Main Start!");
AAA ap = new AAA();
ap.setDaemon(false);
ap.start();
try{
ap.join(5000);//์ด ๊ฐ์ฒด๋งŒ ์‚ฌ์šฉํ•ด๋ผ
Thread.sleep(5000); //ํ˜„์ œ ์Šค๋ž˜๋“œ ์‹œ๊ฐ„ ์ง€์—ฐ
}catch(InterruptedException e){}
System.out.println("Main End!");
}
}
๋™๊ธฐํ™”(์ง€์—ญ์œผ๋กœ ์‚ฌ์šฉ๊ฐ€๋Šฅ) ์ค‘์š”ํ•จ
class K extends Thread{
private int x = 100;
public void setX(int x){
this.x +=x;
}
public
synchronized int getX(){
return x;
}
public void run(){
setX(200);
System.out.println("x= "+getX());
}
}
์ง€์—ญ
synchronized (this){
setX(200);
System.out.println("x= "+getX());
}
21. ์˜ˆ์™ธ์™€ ์˜ˆ์™ธ์ฒ˜๋ฆฌ <์ฒ˜์Œ์œผ๋กœ>
Throw ์˜ˆ์™ธ๋ฅผ ๊ฐ•์ œ๋กœ ๋ฐœ์ƒ ์‹œํ‚ด
public static void main(String[] ar) throws Exception {
:
:
if(su1 > 100){
System.out.println("์˜ˆ์™ธ ๋ฐœ์ƒ : ");
Exception ex = new Exception(" >100");
throw ex;
}
:
:
๊ฒฐ๊ณผ>
์˜ˆ์™ธ ๋ฐœ์ƒ :
Exception in thread "main" java.lang.Exception:
>100
at Exam_05.main(Exam_05.java:10)
throws ์˜ˆ์™ธ์ „๊ฐ€
class O {
public void aaa() throws Exception{
throw new Exception("aaa()");
}
public void bbb()throws Exception {
aaa();
}
}
public class Exam_06 {
public static void main(String[]ar)throws Exception{
O op = new O();
op.bbb();
}
}
๊ฒฐ๊ณผ
Exception in thread "main" java.lang.Exception: aaa()
at O.aaa(Exam_06.java:3)
at O.bbb(Exam_06.java:6)
at Exam_06.main(Exam_06.java:14)
try~catch~finally ์˜ˆ์™ธ์ฒ˜๋ฆฌ
์ •์ƒ์ข…๋ฃŒ, ์˜ˆ์™ธ๋ณด๊ณ , ๊ณ„์† ์‹คํ–‰, ๋Œ€์•ˆ์  ๊ฒฐ๊ณผ
try{
System.out.print("su1 =");
su1 = Integer.parseInt(in.readLine());
System.out.print("su2 =");
su2 = Integer.parseInt(in.readLine());
}catch(NumberFormatException e){///์•”๊ธฐ NumberFormatException
System.out.println("์ˆซ์ž ํฌ๋งท์ด ์ž˜๋ชป๋˜์—ˆ๋„ค์š”");
System.exit(0);
} finally{ //๋ฐ˜๋“œ์‹œ ์‹คํ–‰ํ•  ๊ฒƒ
System.out.println("aaa์ฒ˜๋ฆฌ ์™„๋ฃŒ!");
}
์‚ฌ์šฉ์ž ์ •์˜ ์˜ˆ์™ธ
-
Exception ํด๋ž˜์Šค ์ƒ์†
22. package <์ฒ˜์Œ์œผ๋กœ>
โ€ข
package ๋ž€?
โ€“
โ€ข
package ์„ ์–ธ
โ€“
โ€ข
์ฃผ์„์„ ์ œ์™ธํ•œ ์ƒํƒœ์—์„œ ํŒŒ์ผ์˜ ์ตœ๊ณ  ์ƒ์œ„์— ์œ„์น˜ (package ๊ฒฝ๋กœ.๊ฒฝ๋กœ.๊ฒฝ๋กœโ€ฆ.)
package ์ปดํŒŒ์ผ(๋„์Šค)
โ€“
โ€ข
์œ ์‚ฌ ํด๋ž˜์Šค๋ฅผ ๋ฌถ์–ด์„œ ๊ด€๋ฆฌํ•˜๋Š” ํด๋”์˜ ๊ฐœ๋…
javac โ€“d//๋””๋ ‰ํ† ๋ฆฌ์˜ต์…˜// ์ €์žฅ๊ฒฝ๋กœ ํŒŒ์ผ๋ช….java
์‚ฌ์šฉ๋ฒ• : import ๋ฐ ์ ˆ๋Œ€๊ฒฝ๋กœ๋ช…
package ksh.util;
public class Exam_10 {
public void aaa(){
System.out.println("Exam_10");
}
}
import ksh.util.*;
public class Exam_11 {
public static void main(String[] ar){
Exam_10 ex = new Exam_10();
// ksh.util.Exam_10 ex = new ksh.util.Exam_10(); import๋ฅผ ์‚ฌ์šฉ ํ˜„์žฌํด๋”์—์„œ ์‚ฌ์šฉํ•˜์ง€๋งŒ
์ ˆ๋Œ€๊ฒฝ๋กœ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐ”๋กœ ์ฐพ์•„๋“ค์–ด๊ฐ„๋‹ค.
ex.aaa();
}
}
23. assertion <์ฒ˜์Œ์œผ๋กœ>
โ€ข
assertion ์ด๋ž€? ๊ฐœ๋ฐœ์‹œ ์‚ฌ์šฉ ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ž‘ ๋‹ค๋ฅด๋‹ค
โ€“
ํŠน์ • ๊ตฌ๋ฌธ ์‹คํ–‰ ์‹œ ๊ฒฐ๊ณผ๋ฅผ ๋‹จ์ •์ง€์–ด ํ‘œํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•(์›ํ•˜๋Š” ๊ฒฐ๊ณผ์™€ ๋‹ค๋ฅผ ๊ฒฝ์šฐ ์—๋Ÿฌ
๋ฐœ์ƒ) trace์™€๊ฐ™์€๊ฑฐ
โ€ข
ํ˜•์‹ : assert [boolean๊ฒ€์ฆ] : ์—๋Ÿฌ๊ตฌ๋ฌธ;
โ€ข
์‹คํ–‰ ์˜ต์…˜ : java โ€“ea|-da|-esa ํด๋ž˜์Šค๋ช…
โ€ข
์ง€์ • ์˜ต์…˜ :
-ea:ํด๋ž˜์Šค, -ea:โ€ฆ, -ea:<package๋ช…>โ€ฆ
import java.io.*;
public class Exam_10 {
public static void main(String[]ar)throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("x = ");
int x = Integer.parseInt(in.readLine());
assert x>5 :"if๋ฌธ ํ…Œ์ŠคํŠธ์œ„ํ•œ ๊ฑฐ๋‹ˆ๊น 5๋ณด๋‹ค ํฌ๊ฒŒ ๋„ฃ์–ด!";
if(x > 5){
System.out.println("5๋ณด๋‹ค ํฐ ์˜์—ญ์ž…๋‹ˆ๋‹ค.");
}else{
System.out.println("5๋ณด๋‹ค ์ž‘๋‹ค.");
}
}
}
24. Collection๊ณผ APIs<์ฒ˜์Œ์œผ๋กœ>
โ€ข
Collection ๊ด€๋ จ APIs : Set, Map, List ๋ฌดํ•œ๋ฐฐ์—ด
Set:์ •๋ ฌ์ด ์•ˆ๋œ๋‹ค. ์ค‘์ฒฉ๊ฐ์ฒด๋ฅผ ํ—ˆ์šฉ์•ˆ๋จ (๋ณต์ฃผ๋จธ๋‹ˆ)
import java.util.*;
abstract class ABCD {//์ถ”์ƒํด๋ž˜์Šค
public abstract void disp();
}
class AB extends ABCD { //์ถ”์ƒํด๋ž˜์Šค ์ƒ์†
public void disp(){
System.out.println("AB");
}
}
class CD extends ABCD {
public void disp(){
System.out.println("CD");
}
}
public class Exam_12 {
public static void main(String[] ar){
AB a = new AB();
CD b = new CD();
//String c = new String("C");//String ๊ฐ์ฒด
HashSet hs = new HashSet();//ํ•ด์‹œ์…‹์— ๋“ฑ๋ก
hs.add(a); //a๊ฐ์ฒด ๋“ฑ๋ก
hs.add(b); //b๊ฐ์ฒด ๋“ฑ๋ก
//hs.add(c);
Iterator it = hs.iterator();
while(it.hasNext()){ //Returns true if the iteration has more elements
=(ABCD)it.next(); (ABCD)๊ฐ์ฒด๋กœ ๋ณ€๊ฒฝํ•ด์„œ ๋‹ค์Œ ์š”์†Œ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
obj.disp();
//System.out.println("obj = "+obj);
}
}
}
ABCD obj
remove()
Removes from the underlying collection the last element returned by the iterator
Map : ์ •๋ ฌ์ด ์•ˆ๋œ๋‹ค. ์ค‘์ฒฉ๊ฐ์ฒด๋Š” ๋œ๋‹ค. (๊ฐ์ฒด์— ๋ฒˆํ˜ธ๋ฅผ ๋ฉ”๊ธฐ๋Š”๊ฒƒ๊ณผ๊ฐ™๋‹ค)
import java.util.*;
class EF{}
class GH{}
public class Exam_13 {
public static void main(String[] ar){
EF a = new EF();
GH b = new GH();
String c= new String("c");
Hashtable
ht = new Hashtable(); //Hashtable ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค.
ht.put("EF", a); //๊ฐ์ฒด์š”์†Œ๋ฅผ ๋„ฃ๋Š”๋‹ค. ์ด๋ฆ„๊ฐ’๊ณผ ์ž๋ฃŒ๋ฅผ ๋„ฃ๋Š”๋‹ค.
ht.put("GH", b);
ht.put("ST", c);
Enumeration enu = ht.elements();
//elements()์œผ๋กœ ์š”์†Œ์˜๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜ฌ๋•Œ ์‚ฌ์šฉ
while(enu.hasMoreElements()){ //๋‹ค์Œ ์š”์†Œ๊ฐ€ ์žˆ์œผ๋ฉด ์ฐธ
Object obj = enu.nextElement(); //๋‹ค์Œ ์š”์†Œ์˜๊ฐ’์„ ์ €์žฅ
System.out.println("obj =" + obj);
}
Object obj = ht.get("EF");
System.out.println("obj =" + obj);
Enumeration enu1 = ht.keys(); //ํ‚ค(์ด๋ฆ„)์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
while(enu1.hasMoreElements()){ //๋‹ค์Œ ์š”์†Œ๊ฐ€ ์žˆ์œผ๋ฉด ์ฐธ
String name = (String)enu1.nextElement(); //๋‹ค์Œ ์š”์†Œ์˜ํ‚ค๊ฐ’์„ ์ €์žฅ
Object obj1 = ht.get(name); //ํ‚ค๊ฐ’์— ํ•ด๋‹นํ•˜๋Š” ์ž๋ฃŒ๋ฅผ ์ €์žฅ
System.out.println(name +"=" + obj1);
}
}
}
List : ์ •๋ ฌ, ์ค‘์ฒฉ๊ฐ์ฒด๋‹ค ๋œ๋‹ค. (๊ธฐ์ฐจ์ฒ˜๋Ÿผ)
import java.util.*;
class IJ{}
class KL{}
public class Exam_14 {
public static void main(String[] ar){
IJ a = new IJ();
KL b = new KL();
String c= new String("c");
Vector vc = new Vector();
ArrayList al = new ArrayList();
vc.add(a); vc.add(b); vc.add(c);
al.add(a); al.add(b); al.add(c);
for(int i = 0; i < vc.size(); ++i){
Object obj = vc.elementAt(i);//ํ‚ค๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค.
System.out.println(i+ "=" + obj);
}
System.out.println();
for(int i = 0; i < al.size(); ++i){
Object obj = al.get(i);//ํ‚ค๊ฐ’์„ ๋ถˆ๋Ÿฌ์˜จ๋‹ค
System.out.println(i+ "=" + obj);
}
}
}
โ€ข
Wrapper ํด๋ž˜์Šค๋“ค
โ€ข
Class๋ผ๋Š” ํด๋ž˜์Šค
ํด๋ž˜์Šค๊ฐ€ ์žˆ๋Š” ํ™•์ธํ•  ๋•Œ ์‚ฌ์šฉ
forName() ํด๋ž˜์Šค์ด๋ฆ„์„ ๊ตฌํ•œ๋‹ค.
try{
Class cl = Class.forName("Exam_12");
System.out.println(cl.getMethods());
}catch(ClassNotFoundException e){
System.out.println("์ฐพ์„์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
}
Runtime ํด๋ž˜์Šค
ํ”„๋กœ์„ธ์Šค๋ฅผ ์‹คํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ
public class Exam_16 {
public static void main(String[] ar) throws Exception{
Runtime rt = Runtime.getRuntime();
Process pro = rt.exec("explorer.exe http://www.naver.com");
pro.destroy();
}
}
โ€ข
String ํด๋ž˜์Šค
public class Exam_17 {
public static void main(String[] ar)throws Exception{
String str = new String("Hello java!");
char a = str.charAt(6); //int ์ž๋ฆฌ์˜ ๋ฌธ์ž๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
System.out.println(a);
boolean b= str.endsWith("!");// ํ•ด๋‹น ๊ธ€์ž๋กœ ๋๋‚˜๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค.
System.out.println(b);
boolean e= str.startsWith("e");// ํ•ด๋‹น ๊ธ€์ž๋กœ ๋๋‚˜๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค.
System.out.println(e);
boolean c= str.equalsIgnoreCase("hello java!");
//๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ฌธํ•˜์ง€์•Š๊ณ  ๊ฐ™์€์ง€ ์•Œ์•„๋ณธ๋‹ค.
System.out.println(c);
byte[] d = str.getBytes("ISO8859_1");
//๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ฌธ์ž๋กœ ์ €์žฅํ•œ๋‹ค
for(int i = 0; i<d.length; i++){
System.out.print((char)d[i]+"\t");
}
System.out.println();
System.out.println(str.indexOf("o"));
//ํ•ด๋‹น ๊ธ€์ž๊ฐ€ ๋ฌธ์ž์—ด์— ๋ช‡ ๋ฒˆ์งธ ์œ„์น˜ํ•˜๋Š”๊ฐ€
System.out.println();
System.out.println(str.lastIndexOf("a"));
//ํ•ด๋‹น ๊ธ€์ž๊ฐ€ ๋’ค์žˆ๋Š”๊ฒƒ์„ ๊ฒ€์‚ฌ ๋ฌธ์ž์—ด์— ๋ช‡ ๋ฒˆ์งธ ์œ„์น˜ํ•˜๋Š”๊ฐ€
System.out.println();
System.out.println(str.length());
//๊ธ€์ž์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
System.out.println();
System.out.println(str.substring(6,10));
//6์—์„œ๋ถ€ํ„ฐ 10๊นŒ์ง€ ๋ฌธ์ž์—ด ์ž๋ผ์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
System.out.println();
System.out.println(str.toUpperCase());
//๋ชจ๋‘๋ฅผ ๋Œ€๋ฌธ์ž๋กœ toLowerCase() ๋Š” ์†Œ๋ฌธ์ž๋กœ
System.out.println(str.trim());
//์•ž๋’ค์˜ ๊ณต๋ฐฑ์„ ์—†์—”๋‹ค.
String str2 = "ABCD";
str2.substring(2,3);
System.out.println(str2);
///substring ์„ ์‚ฌ์šฉํ•ด๋„ str2๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
StringBuffer str3 = new StringBuffer("ABCD");
str3.append("E");
System.out.println(str3);
//StringBuffer ์‚ฌ์šฉํ•˜๋ฉด ์ž๊ธฐ ์ž์‹ ์ด ๋ณ€ํ•œ๋‹ค.
}
}
โ€ข
Date์™€ Calendar ํด๋ž˜์Šค
Dateํด๋ž˜์Šค
2000๋…„ ๋“ค์–ด์˜ค๋ฉด์„œ 2000๋…„ ๊นŒ์ง€ ํฌํ˜„ํ•˜์ง€ ๋ชปํ•˜์—ฌ ์‚ฌ์šฉ์„ ๊ถŒ์žฅํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
import java.util.*;
public class Exam_18 {
public static void main(String [] ar){
Date d = new Date();
System.out.println("d = "+d);
System.out.println("year = "+d.getYear());
System.out.println("month = "+d.getMonth());
System.out.println("date = " + d.getDate());
System.out.println("hour = " + d.getHours());
System.out.println("minute = "+d.getMinutes());
System.out.println("second = " + d.getSeconds());
}
}
Calendarํด๋ž˜์Šค
Calendar ca = Calendar.getInstance();
System.out.println("ca = " + ca);
System.out.println("year = " + ca.get(Calendar.YEAR) );
System.out.println("Month = " + (ca.get(Calendar.MONTH)+1));
System.out.println("date = " + ca.get(Calendar.DAY_OF_MONTH) );
System.out.println("hour = " + ca.get(Calendar.HOUR_OF_DAY) );
System.out.println("minute = " + ca.get(Calendar.MINUTE) );
System.out.println("second = " + ca.get(Calendar.SECOND) );
์ถ”์ƒํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— GregorianCalendar ์„ ์‚ฌ์šฉํ•œ๋‹ค. API ์ฐธ๊ณ 
โ€ข
SimpleDateFormat ํด๋ž˜์Šค
Date dd = new Date();
impleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String str = format.format(dd);
System.out.println("ํ˜„์ œ์‹œ๊ฐ„์€ " + str);
๊ธฐํƒ€์œ ํ‹ธ : Scanner//์ธํŠธ๋กœ ์ž…๋ ฅํ•  ๋•Œ ์‚ฌ์šฉ, Locale, Properties, Random//ํŠน์ •๋ฒ”์œ„๋‚ด์˜
์ˆ˜๋ฅผ ๋žœ๋ค์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
Timer, TimeZone
โ€ข
StringTokenizer ํด๋ž˜์Šค
import java.util.*;
public class Exam_19 {
public static void main(String [] ar){
String str = "aaa,bbb,ccc,ddd,eee,fff:ggg:hhh:iii";
StringTokenizer stk = new StringTokenizer(str, ",:", false);
//ํ•ด๋‹น ๋ฌธ์ž์—ด์ด ์–ด๋–ค ๋ฌธ์ž์— ๋Œ€ํ•ด ๋‚˜๋ˆˆ๋‹ค.
while(stk.hasMoreTokens()){
System.out.println(stk.nextToken());
}
}
}
โ€ข
For๋ฌธ์˜ ํ™•๋Œ€, Generic & Auto-boxing
import java.util.*;
public class Exam_21 {
public static void main(String[]ar){
ArrayList<Integer> al = new ArrayList();
al.add(1);
al.add(2);
al.add(3);
int sum = 0;
for (Integer i : al){
sum +=i;
}
System.out.println(sum);
}
}
25. Frame ํด๋ž˜์Šค์™€ Label<์ฒ˜์Œ์œผ๋กœ>
โ€ข
Frame ํด๋ž˜์Šค : GUI์˜ Base Form
Frame ๊ฐ์ฒด ์ƒ์„ฑ
import java.awt.*;
public class Exam_01 {
public static void main(String[] ar){
Frame f = new Frame();
Frame f1 = new Frame("์ œ๋ชฉ");
f.setVisible(true);
f1.setVisible(true);
//๋””ํดํŠธ๊ฐ€ false์ด๊ณ  true์–ด์•ผ ํ™”๋ฉด์— ๋ณด์ธ๋‹ค.
f.setSize(300, 200);
f1.setSize(400,300);
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
//ํ™”๋ฉด(ํ•ด์ƒ๋™) ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•
Dimension f_size = f.getSize();
Dimension f1_size = f1.getSize();
//ํ”„๋ ˆ์ž„ ํฌ๊ธฐ๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฉ๋ฒ•
int xpos1 = (int)(screen.getWidth() /2 -f_size.getWidth() /2);
int ypos1 = (int)(screen.getHeight() /2 -f_size.getHeight() /2);
f.setLocation(xpos1, ypos1);
//ํ”„๋ž˜์ž„์„ ์ค‘์•™์— ๋‚˜์˜ค๊ฒŒ ํ•œ๋‹ค.
int xpos2 = (int)(screen.getWidth() /2 -f1_size.getWidth() /2);
int ypos2 = (int)(screen.getHeight() /2 -f1_size.getHeight() /2);
f1.setLocation(xpos2, ypos2);
//ํ”„๋ž˜์ž„์„ ์ค‘์•™์— ๋‚˜์˜ค๊ฒŒ ํ•œ๋‹ค.
f.setResizable(false);
f1.setResizable(false);
//ํฌ๊ธฐ ์„ค์ •์ด ์•ˆ๋จ
//f.setLocation(, 200);
//f1.setLocation(300,300);
}
}
Frame ํด๋ž˜์Šค ์ƒ์†
import java.awt.*;
class Exam__sub extends Frame {
private Label lb = new Label("๋ ˆ์ด์–ด");
public Exam__sub(String title){
super(title);
super.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() /2 -frm.getWidth() /2);
int ypos = (int)(screen.getHeight() /2 -frm.getHeight() /2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){//ํ™”๋ฉด ์ดˆ๊ธฐํ™” ์ž‘์—…
this.add(lb);
}
}
public class Exam_ {
public static void main(String[] ar){
Exam__sub ex = new Exam__sub("์ œ๋ชฉ");
}
}
2. Frame ์ƒ์†๊ณ„์—ด ํด๋ž˜์Šค๋“ค(API์ฐธ๊ณ )
Component ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ
Container ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ(Componentf๋ฅผ ์ƒ์†)
Window ํด๋ž˜์Šค์˜ ๋ฉ”์„œ๋“œ (Container๋ฅผ ์ƒ์†)
26. Layout Manager <์ฒ˜์Œ์œผ๋กœ>
FlowLayout Manager
import java.awt.*;
class Exam_04_sub extends Frame {
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private Button bt6 = new Button("6");
private FlowLayout f1 = new FlowLayout(FlowLayout.LEFT); //๊ธฐ๋ณธ์ด center์ด๋‹ค
public Exam_04_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() /2 -frm.getWidth() /2);
int ypos = (int)(screen.getHeight() /2 -frm.getHeight() /2);
super.setLocation(xpos, ypos);
super.setResizable(true);
super.setVisible(true);
}
public void init(){
this.setLayout(f1);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
}
}
public class Exam_04 {
public static void main(String[] ar){
Exam_04_sub ex = new Exam_04_sub("์ œ๋ชฉ");
}
}
GridLayout Manager
import java.awt.*;
class Exam_05_sub extends Frame{
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private Button bt6 = new Button("6");
private GridLayout g1 = new GridLayout(3, 2, 10, 10);
//๋ช‡ํ–‰ ๋ช‡์—ด x์—ฌ๋ฐฑ, y์—ฌ๋ฐฑ
public Exam_05_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension scrren = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(scrren.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(scrren.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos,ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setLayout(g1);
this.add(bt1);
this.add(bt2);
this.add(bt3);
this.add(bt4);
this.add(bt5);
this.add(bt6);
}
}
public class Exam_05 {
public static void main(String[]ar){
Exam_05_sub ex = new Exam_05_sub("์ œ๋ชฉ");
}
}
BorderLayout Manager
import java.awt.*;
class Exam_06_sub extends Frame{
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private Button bt4 = new Button("4");
private Button bt5 = new Button("5");
private BorderLayout bl = new BorderLayout(10,10);
//์—ฌ๋ฐฑ์ด ๊ฐ€๋Šฅํ•˜๋‹ค
public Exam_06_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension scrren =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(scrren.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(scrren.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos,ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setLayout(bl);
this.add("North", bt1); //์œ„์น˜๊ฐ’์„ ๋ฐ˜๋“œ์‹œ ๊ธฐ๋ก
this.add("South", bt2);
this.add("West", bt3);
this.add("East", bt4);
this.add("Center", bt5);
}
}
public class Exam_06 {
public static void main(String[]ar){
Exam_06_sub ex = new Exam_06_sub("์ œ๋ชฉ");
}
}
CardLayout Manager
import java.awt.*;
class Exam_07_sub extends Frame{
private Button bt1 = new Button("1");
private Button bt2 = new Button("2");
private Button bt3 = new Button("3");
private CardLayout cl = new CardLayout();
public Exam_07_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension scrren =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(scrren.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(scrren.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos,ypos);
super.setResizable(false);
super.setVisible(true);
try{
Thread.sleep(2000);
}catch(InterruptedException e){}
cl.show(this,"bb");
try{
Thread.sleep(2000);
}catch(InterruptedException e){}
cl.show(this,"cc");
try{
Thread.sleep(2000);
}catch(InterruptedException e){}
cl.show(this,"aa");
}
public void init(){
this.setLayout(cl);
this.add("aa",bt1);
this.add("bb",bt2);
this.add("cc",bt3);//๊ฒน์ณ์„œ ๋‚˜์˜ค๊ธฐ๋•Œ๋ฌธ ๋ถ€๋ฅด๊ธฐ ์œ„ํ•œ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผํ•œ๋‹ค.
}
}
public class Exam_07 {
public static void main(String[]ar){
Exam_07_sub ex = new Exam_07_sub("์ œ๋ชฉ");
}
}
GridBagLayout Manager
import java.awt.*;
class Exam_08_sub extends Frame{
private Button bt1 = new Button("AAA");
private Button bt2 = new Button("BBB");
private Button bt3 = new Button("CCC");
private Button bt4 = new Button("EEE");
private Button bt5 = new Button("DDD");
private Button bt6 = new Button("FFF");
private Button bt7 = new Button("GGG");
private GridBagLayout gb = new GridBagLayout();
private GridBagConstraints gc = new GridBagConstraints();
public Exam_08_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension scrren =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(scrren.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(scrren.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos,ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setLayout(gb);
gc.gridx = 0;
gc.gridy = 0;
gb.setConstraints(bt1,gc);
this.add(bt1);
gc.gridx = 1;
gc.gridy = 1;
gb.setConstraints(bt2,gc);
this.add(bt2);
gc.gridx = 2;
gc.gridy = 2;
gb.setConstraints(bt3,gc);
this.add(bt3);
gc.gridx = 3;
gc.gridy = 2;
gb.setConstraints(bt5,gc);
this.add(bt5);
gc.gridx = 2;
gc.gridy = 1;
gc.gridwidth =2;
gb.setConstraints(bt4,gc);
this.add(bt4);
}
}
public class Exam_08 {
public static void main(String[]ar){
Exam_08_sub ex = new Exam_08_sub("์ œ๋ชฉ");
}
}
27. Panel ํด๋ž˜์Šค <์ฒ˜์Œ์œผ๋กœ>
java.awt.Panel ํด๋ž˜์Šค
import java.awt.*;
class Exam_03_sub extends Frame{
private Button yes_bt = new Button("ํ™•์ธ");
private Button no1_bt = new Button("์ทจ์†Œ1");
private Button no2_bt = new Button("์ทจ์†Œ2");
private Panel p = new Panel();//ํŒจ๋„์— ํŒจ๋„์„ ๋„ฃ์„ ์ˆ˜ ์žˆ๋‹ค.
private Panel p1 = new Panel();
private BorderLayout bl= new BorderLayout();
private GridLayout gl = new GridLayout(1,2);
private GridLayout gl2 = new GridLayout(2,1);
public Exam_03_sub(String title){
super(title);
this.init();
super.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setLayout(bl);
p.setLayout(gl);
p.add(yes_bt);
p1.setLayout(gl2);
p1.add(no1_bt);
p1.add(no2_bt);
p.add(p1);
this.add("South", p);//ํŒจ๋„์„ ๋„ฃ๋Š”๋‹ค.
}
}
public class Exam_03 {
public static void main(String [] ar){
Exam_03_sub ex = new Exam_03_sub("์ œ๋ชฉ");
}
}
28.์ฃผ๋ณ€ ํด๋ž˜์Šค๋“ค<์ฒ˜์Œ์œผ๋กœ>
import java.awt.*;
class Exam_04_sub extends Frame {
private Color cc= new Color(125, 78,234); //์ƒ‰์ƒ ์„ค์ •ํ•˜๊ธฐ
private Button bt = new Button("ํ•™์ธ");
private GridBagLayout gb1 = new GridBagLayout();
private Cursor cur = new Cursor(Cursor.HAND_CURSOR);//์ปค์„œ์„ค์ •ํ•˜๊ธฐ
private Cursor cur2 = new Cursor(Cursor.TEXT_CURSOR);
private Font fn = new Font("ํœด๋จผ์˜›์ฒด",Font.BOLD, 20); //ํฐํŠธ๋ฅผ ์„ค์ •ํ•œ๋‹ค.
public Exam_04_sub(String title){
super(title);
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage("aaa.gif"); //๋‹ค์ดํ‹€ ์•„์ด์ฝ˜ ๋ณ€๊ฒฝํ•˜๊ธฐ
this.setIconImage(img);
this.init();
super.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
Dimension a = new Dimension(100,200); //ํญ๊ณผ ๋„“์ด๋ฅผ ๊ด€๋ฆฌํ•œ๋‹ค.
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setBackground(cc); //๋ฐฐ๊ฒฝ์ƒ‰์ง€์ •ํ•˜๊ธฐ
this.setLayout(gb1);
bt.setCursor(cur);//๋ฒ„ํŠผ์œ„์— ์˜ฌ๋ผ๊ฐ€๋ฉด ์ปค์„œ ๋ชจ์–‘ ๋ฐ”๊พธ๊ธฐ
this.add(bt);
bt.setFont(fn); //๋ฒ„ํŠผ์— ํฐํŠธ ์ง€์ •ํ•˜๊ธฐ
bt.setBackground(Color.BLACK);//๋ฒ„ํŠผ์ƒ‰์ƒ์ง€์ •ํ•˜๊ธฐ
bt.setForeground(Color.white);
this.setCursor(cur2);//์ปค์„œ ์ง€์ €ํ•˜๊ธฐ
}
}
public class Exam_04 {
public static void main(String [] ar){
Exam_04_sub ex = new Exam_04_sub("์ œ๋ชฉ");
}
}
29.AWT ๊ด€๋ จ ์ปดํฌ๋„ŒํŠธ<์ฒ˜์Œ์œผ๋กœ>
p.setPreferredSize(new Dimension(80,10));
//ํฌ๊ธฐ ์ˆ˜์ •ํ•˜๊ธฐ
Java.awt.Label ํด๋ž˜์Šค
private Label lb = new Label("์ „ํ•˜๊ธฐ", Label.CENTER)
//๋ ˆ์ด์–ด๊ธ€์ž์˜ ์ •๋ ฌํ• ์ˆ˜์žˆ๋‹ค.;
Java.awt.Button ํด๋ž˜์Šค
private Button bt = new Button("ํ•™์ธ");
private Button [] bt = new Button[12];
Java.awt.Checkbox ํด๋ž˜์Šค
private Label fri_lb = new Label("์ข‹์•„ํ•˜๋Š” ๊ณผ์ผ์€?");
private Checkbox a_cb = new Checkbox("๋ฐ”๋‚˜๋‚˜"); //์ฒดํฌ๋ฐ•์Šค ์ƒ์„ฑ
private Checkbox b_cb = new Checkbox("๋”ธ๊ธฐ");
private Checkbox c_cb = new Checkbox("ํŒŒ์ธ์• ํ”Œ");
Java.awt.CheckboxGroup ํด๋ž˜์Šค
private Label sex_lb = new Label("๋‹น์‹ ์˜ ์„ฑ๋ณ„์€ ?");
private CheckboxGroup cg = new CheckboxGroup();
//๋ผ๋””์˜ค ๋ฒ„ํŠผ์œผ๋กœ ํ•˜๊ธฐ์œ„ํ•œ ๊ทธ๋ฃน์œผ๋กœ
private Checkbox man_cb = new Checkbox("๋‚จ์„ฑ", cg, true);
//๋ผ๋””์˜ค ๋ฒ„ํŠผ true ์„ ํƒ๋œ์ƒํƒœ
private Checkbox woman_cb = new Checkbox("์—ฌ์„ฑ", cg, false);
Java.awt Choice ํด๋ž˜์Šค
private Label blood_lb = new Label("ํ˜ˆ์•กํ˜•์€ ?");
private Choice blood_ch = new Choice();
//๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€๋Š” add๋ฅผ ์‚ฌ์šฉ ํ•˜๋ฉด๋œ๋‹ค.
blood_ch.add("Aํ˜•");
Java.awt.List ํด๋ž˜์Šค
private Label alpha_lb = new Label("์•žํŒŒ๋ฒณ๋ชฐ๋ก", Label.CENTER);
private List
alpha_li = new List(5, true);//true ์—ฌ๋Ÿฌ๊ฐœ ์„ ํƒํ• ์ˆ˜ ์žˆ๋‹ค
Java.awt.TextField & TextArea ํด๋ž˜์Šค
private TextField title_tf = new TextField();
private TextArea contents_ta = new TextArea();
Java.awt.Dialog & FileDialog ํด๋ž˜์Šค
private Dialog dlg = new Dialog(this, "๊ฒฝ๊ณ ", false);
//(๋ˆ„๊ตฌ์—๊ฒŒ(์†Œ์†), ์ œ๋ชฉ,
๋ชจ๋ธ์†์„ฑ(ํ”„๋ ˆ์ž„์œผ๋กœ ์ด๋™์ด ๊ฐ€๋Šฅํ•œ๊ฐ€ ํ”„๋ ˆ์ž„์ด ๋‹ซํ˜€์•ผ ๊ฐ€๋Šฅํ•œ๊ฐ€)
//๋ฉ”์„ธ์ง€๋ฅผ ๋›ฐ์šธ๋•Œ ์‚ฌ์šฉํ•˜๋ฉฐ ํŒ์—… ๊ฐ™์€๊ฒƒ์„ ๋งํ•œ๋‹ค.
//ํ”„๋ž˜์ž„์ฒ˜๋Ÿผ ์†์„ฑ์„ ์‚ฌ์šฉํ•œ๋‹ค. dlg.setVisible(true);
private FileDialog fdlg =
new FileDialog(this, "์ €์žฅ", FileDialog.SAVE);
//(๋ˆ„๊ตฌ์—๊ฒŒ(์†Œ์†), ์ œ๋ชฉ, ๋ชจ๋ธ์†์„ฑ(์ €์žฅํ• ๊ฒƒ์ธ์ง€, ์—ด๊ธฐ๋ฅผ ํ• ๊ฒƒ์ธ์ง€)
//๋‚ด์šฉ๋ถ€์™€ ํฌ๊ธฐ๋ฅผ ์ง€์ •ํ•  ํ•„์š”๊ฐ€์—†๋‹ค. ๊ธฐ๋ณธ์œผ๋กœ ์ง€์ •๋œ๋‹ค.
Java.awt.Menu, MenuItem ํด๋ž˜์Šค
import java.awt.*;
class Exam_15_sub extends Frame {
private
private
private
private
private
private
MenuBar mb = new MenuBar();
Menu file = new Menu("FILE");
MenuItem fnew = new MenuItem("NEW");
MenuItem fopen = new MenuItem("OPEN");
MenuItem fsave = new MenuItem("SAVE");
MenuItem fexit = new MenuItem("EXIT");
private
private
private
private
private
private
Menu edit = new Menu("EDIT");
Menu ecolor = new Menu("COLOR");
CheckboxMenuItem red_cb = new CheckboxMenuItem("RED");
CheckboxMenuItem green_cb = new CheckboxMenuItem("GREEN");
CheckboxMenuItem blue_cb = new CheckboxMenuItem("BLUE");
Menu esize = new Menu("SIZE");
private Menu help = new Menu("HELP");
private MenuItem hver = new MenuItem("VERSION");
public Exam_15_sub(String title){
//๊ธฐ๋ณธ ํผ~์ƒ๋žต
}
public void init(){
file.add(fnew);
file.addSeparator(); //๋ถ„ํ• ์„ 
file.add(fopen);
file.add(fsave);
file.addSeparator(); //๋ถ„ํ• ์„ 
file.add(fexit);
mb.add(file);
ecolor.add(red_cb);
ecolor.add(green_cb);
ecolor.add(blue_cb);
edit.add(ecolor);
edit.addSeparator(); //๋ถ„ํ• ์„ 
edit.add(esize);
mb.add(edit);
help.add(hver);
mb.add(help);
this.setMenuBar(mb);
}
}
public class Exam_15 {
public static void main(String[] ar){
Exam_15_sub ex = new Exam_15_sub("์ œ๋ชฉ");
}
}
30. Event ๊ตฌํ˜„๋ฐฉ๋ฒ•<์ฒ˜์Œ์œผ๋กœ>
import java.awt.*;
import java.awt.event.*;
class Exam_01_sub extends Frame implements ActionListener{
//ํผํด๋ž˜์Šค์™€ Listener๊ณผ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค.
public void actionPerformed(ActionEvent e){}
private Button bt = new Button("ํ™•์ธ");
private GridBagLayout gbl = new GridBagLayout();
public Exam_01_sub(String title){
super(title);
super.setSize(300,200);
this. init();
this.start();
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth()/2-frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2-frm.getHeight()/2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this.setLayout(gbl);
this.add(bt);
}
public void start(){
A ap = new A();
bt.addActionListener(ap);
B bp = new B();
bt.addMouseListener(bp);
bt.addActionListener(this);
MouseAdapter ma = new MouseAdapter(){
public void mouseClicked(MoueseEvent e){
System.exit(0);
}//์ต๋ช… ์ค‘์ฒฉ ํด๋ž˜์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
};
}
}
public class Exam_01 {
public static void main(String [] ar){
Exam_01_sub ex = new Exam_01_sub("์ œ๋ชฉ");
}
}
class A implements ActionListener{
public void actionPerformed(ActionEvent e){
//ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœ์„ ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์œ„ํ•˜์—ฌ ํ˜ธ์ถœ ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค.
//Listener ํด๋ž˜์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
//ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ๋ชจ๋‘ ์žฌ์ •ํ•ด์•ผํ•œ๋‹ค.(์ธํ„ฐํŽ˜์ด์Šค)
System.exit(0);
}
}
class B extends MouseAdapter{
public void mouseClicked(MouseEvent e){
//ํ•ด๋‹น ์ด๋ฒคํŠธ์˜ ๋ฉ”์†Œ๋“œ๋ฅผ ์ฐพ์•„ ์ ๋Š”๋‹ค.
//Adapter ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•
//ํ•ด๋‹น ๋ฉ”์†Œ๋“œ๋ฅผ ๋ชจ๋‘ ์žฌ์ •ํ• ํ•„์š”๋Š”์—†๋‹ค. (ํ•„์š”ํ•œ ๋ฉ”์†Œ๋“œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค)
System.exit(0);
}
}
31. Event ํ™œ์šฉ ๋ฒ”์œ„<์ฒ˜์Œ์œผ๋กœ>
ActionListener : ๋ฒ„ํŠผํด๋ฆญ, ๋ฉ”๋‰ด์„ ํƒ ๋“ฑ
WindowAdapter์™€ WindowListener : Frame ๊ด€๋ จ
MouseAdapter์™€ MouseListener : ๋งˆ์šฐ์Šค ํด๋ฆญ ๋“ฑ ๋งˆ์šฐ์Šค ๊ด€๋ จ
MouseMotionAdapter์™€ MouseMotionListener : ๋งˆ์šฐ์Šค ์›€์ง์ž„ ๊ด€๋ จ
import java.awt.*;
import java.awt.event.*;
class Exam_02_sub extends Frame implements MouseListener,
MouseMotionListener, ActionListener{
private MenuBar mb = new MenuBar();
private Menu file = new Menu("FILE");
private MenuItem fopen = new MenuItem("OPEN");
private MenuItem fsave = new MenuItem("SAVE");
private FileDialog fdig1 = new FileDialog(this, "๋‚ด๊บผ ์—ด๊ธฐ",
FileDialog.LOAD);
private FileDialog fdig2 = new FileDialog(this, "๋‚ด๊บผ ์ €์žฅ",
FileDialog.SAVE);
private Label lb = new Label("x = 000, y = 000");
private Button bt = new Button("ํ™•์ธ");
private BorderLayout bl = new BorderLayout();
public Exam_02_sub(String title){
super(title);
this.init();
this.start();
super.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() /2 -frm.getWidth() /2);
int ypos = (int)(screen.getHeight() /2 -frm.getHeight() /2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
file.add(fopen);
file.add(fsave);
mb.add(file);
this.setMenuBar(mb);
this.setLayout(bl);
this.add("North", lb);
this.add("South", bt);
}
public void start(){
WindowAdapter wa = new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
//Window์˜ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰ ๋จ
}
};
/*this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
//Window์˜ ๋‹ซ๊ธฐ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์‹คํ–‰ ๋จ
}
});*/
fopen.addActionListener(this);//์•ก์…˜ ์‹คํ–‰
fsave.addActionListener(this);
bt.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void actionPerformed(ActionEvent e){
if (e.getSource()==fopen){
//์–ด๋–ค๊ฒƒ์ด ๋“ค์–ด์™”๋Š”์ง€ ํ™•์ธ
fdig1.setVisible(true);
}else if(e.getSource()==fsave){
fdig2.setVisible(true);
}
}
//==================================================
public void mouseClicked(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {
//๋งˆ์šฐ์Šค๊ฐ€ ํผ๊ฐ์ฒด์— ์žˆ๋Š”๊ฐ€?
if(e.getSource()==bt){
bt.setLabel("๋ฒ„ํŠผ์œ„์— ๋งˆ์šฐ์Šค๊ฐ€ ์žˆ๋„ค์š”!");
}
}
public void mouseExited(MouseEvent e) {
//๋งˆ์šฐ์Šค๊ฐ€ ํผ๊ฐ์ฒด์—์„œ ๋น ์ €๋‚˜๊ฐ”๋А๋ƒ?
if(e.getSource()==bt){
bt.setLabel("ํ™•์ธ");
}
}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
//๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ ๋ฉ”์†Œ๋“œ===================================
//===================================================
public void mouseDragged(MouseEvent e) {
if(e.getSource()== this){
int x = e.getX();
int y = e.getY();
//๋งˆ์šฐ์Šค ์ขŒํ‘œ๋ฅผ ์ €์žฅํ•œ๋‹ค.
lb.setText("x = " + x + ", y =" + y);
}
}
public void mouseMoved(MouseEvent e) {}
//๋งˆ์šฐ์Šค๊ฐ€ ์›€์ง์ผ๋•Œ
}
//๋งˆ์šฐ์Šค ๋ชจ์…˜ ์ด๋ฒคํŠธ ๋ฉ”์†Œ๋“œ================================
public class Exam_02 {
public static void main(String[] ar){
Exam_02_sub ex = new Exam_02_sub("์ œ๋ชฉ");
}
}
KeyAdapter์™€ KeyListener : ํ‚ค ๊ด€๋ จ
FocusAdapter์™€ FocusListener : ํฌ์ปค์Šค ๊ด€๋ จ
ItemListener : List๋‚˜ Choice์—์„œ์˜ ์•„์ดํ…œ ๊ด€๋ จ
import java.awt.*;
import java.awt.event.*;
class Exam_04_sub extends Frame implements FocusListener, KeyListener,
ItemListener {
private TextField tf1 = new TextField();
private TextField tf2 = new TextField();
private Label lb1 = new Label("NONE : ", Label.RIGHT);
private Label lb2 = new Label("NONE");
private Choice ch = new Choice();
private Label lb3 = new Label("NONE");
private
private
private
private
private
GridLayout
Panel p1 =
GridLayout
Panel p2 =
GridLayout
gl = new GridLayout(4, 1);
new Panel();
gl1 = new GridLayout(1,2,5,5);
new Panel();
gl2 = new GridLayout(1,2,5,5);
public Exam_04_sub(String title){
super(title);
this.init();
this.start();
super.setSize(250,120);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = super.getSize();
int xpos = (int)(screen.getWidth() /2 -frm.getWidth() /2);
int ypos = (int)(screen.getHeight() /2 -frm.getHeight() /2);
super.setLocation(xpos, ypos);
super.setResizable(false);
super.setVisible(true);
}
public void init(){
this. setLayout(gl);
p1.setLayout(gl1);
p1.add(tf1);
p1.add(tf2);
this.add(p1);
p2.setLayout(gl2);
p2.add(lb1);
p2.add(lb2);
this.add(p2);
for(char c = 'A'; c<='Z'; c++){
String str = "" + c+ c+ c;
ch.add(str);
}
this.add(ch);
this.add(lb3);
}
public void start(){
tf1.addFocusListener(this);
tf2.addFocusListener(this);
tf1.addKeyListener(this);
tf2.addKeyListener(this);
ch.addItemListener(this);
}
public void focusGained(FocusEvent e){
//ํฌ์ปค์Šค ํ™œ์„ฑํ™”
if(e.getSource()== tf1){
lb1.setText("์ฒซ๋ฒˆ์งธ TextField์œ„์น˜ : ");
}else if(e.getSource()== tf2){
lb1.setText("๋‘๋ฒˆ์งธ TextField์œ„์น˜ : ");
}
}
public void focusLost(FocusEvent e){
//ํฌ์ปค์Šค ๋น„ํ™œ์„ฑํ™”
}
public void keyPressed(KeyEvent e) {
// ํ‚ค๋ฅผ ๋ˆ„๋ฅผ๋•Œ ์ฒ˜๋ฆฌ
}
public void keyReleased(KeyEvent e) {
// ํ‚ค๋ฅผ ๋ˆ„๋ฅด๊ณ  ๋•Œ๋ฉด ์ฒ˜๋ฆฌ
if(e.getSource()== tf1){
int cnt = tf1.getText().trim().length();
lb2.setText(cnt + "๊ฐœ");
}else if(e.getSource()== tf2){
lb1.setText("๋‘๋ฒˆ์งธ TextField์œ„์น˜ : ");
int cnt = tf2.getText().trim().length();
lb2.setText(cnt + "๊ฐœ");
}
}
public void keyTyped(KeyEvent e) {
// ํ‚คํƒ€์ž…์„ ์กฐ์‚ฌ
}
public void itemStateChanged(ItemEvent e) {
//์•„์ดํ…œ ์ด๋ฒคํŠธ
if(e.getSource()==ch){
String str = ch.getSelectedItem();
lb3.setText("์„ ํƒ๋œ ๋‚ด์šฉ = " + str);
}
}
}
public class Exam_04 {
public static void main(String[] ar){
Exam_04_sub ex = new Exam_04_sub("์ œ๋ชฉ");
}
}
32. Graphics์˜ paint ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•<์ฒ˜์Œ์œผ๋กœ>
public void update(Graphics g){
g.clearRect(0,0,200,300);
paint(g);
} //ํ™”๋ช… ์‹คํ–‰ํ•  ๋•Œ ์ดˆ๊ธฐํ™” ์ž‘์—…
public void paint(Graphics g){
int x = (int)(Math.random()*200);
int y = (int)(Math.random()*300);
g.drawLine(x,x,y,y);
//์ง์„ 
} //๊ทธ๋ž˜ํ”ฝ์Šค ์š”์†Œ๋ฅผ ์ˆ˜์ •ํ•œ๋‹ค.
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bt){
this.repaint();
}
}
//repaint ํ• ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์˜ˆ(๋ฒ„ํŠผ) ๋‹ค์‹œ์ง€์ •ํ• ๋•Œ ์˜ˆ(์ด๋ฏธ์ง€ํฌ๊ธฐ) ์žฌ์ •ํ•˜์—ฌ ๋‹ค์‹œ
๋ถˆ๋Ÿฌ์˜จ๋‹ค.
public void paint(Graphics g){
g.drawLine(50,50,100,100);
g.setColor(Color.RED);
g.drawRect(50, 50, 100, 100);
g.setColor(Color.BLUE);
g.fillRect(160, 50, 100, 100);
//์†์ƒ‰์ƒ์ด ์ฑ„์›Œ์ง„ ์‚ฌ๊ฐํ˜•
g.setColor(Color.GREEN);
int[] x= {80, 100, 50};
int[] y= {30,90,85};
g.drawPolygon(x, y, 3);
//๋‹ค๊ฐํ˜• x, y๋Š” ๋ฐฐ์—ด๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.
g.setColor(Color.CYAN);
g.setFont(new Font("๊ตด๋ฆผ์ฒด", Font.BOLD, 20));
g.drawString("์—ฌ๊ธฐ๋Š” ๊ทธ๋ž˜ํ”ฝ!", 100, 100);
//๊ธ€์ž ๊ทธ๋ž˜ํ”ฝ
Toolkit tk = Toolkit.getDefaultToolkit();
Image img = tk.getImage("1.jpg");
g.drawImage(img, 50,50, 100,100, this);
//์ด๋ฏธ์ง€
(์ด๋ฏธ์ง€, x, y, width, height, this)
}
33. Applet ์ž‘์„ฑ๋ฐฉ๋ฒ•<์ฒ˜์Œ์œผ๋กœ>
<html> ์ž‘์„ฑ
<html>
<head>
<title>๊ทธ๋ฆผํŒ</title>
</head>
<body>
<center>
<h2>์•„๋ž˜์˜ ๋‚ด์šฉ์€ ์• ํ”Œ๋ฆฟ</ht><p>
<applet codebase="./bin" code="Exam_02.class"
width="400" height="400">
</applet>
</center>
</body>
</html>
<java> ์ž‘์„ฑ
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Exam_01 extends Applet {
private static String str = "";
public void init(){
str += "init() --> ";
}
public void start(){
str += "start() --> ";
}
public void paint(Graphics g){
str +="paint() --> ";
g.drawString(str, 0 , 100);
}
public void stop(){
str += "stop() -->";
}
public void destroy(){}
}
//๋ฉ”์ธ์„ ์‚ฌ์šฉ ํ•˜์ง€์•Š๋Š”๋‹ค.
// import java.applet.*;
// Applet๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค.
//class๋Š” public๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.
//์ฃผ๊ธฐ๋ฅผ ์ƒ๊ฐํ•˜๋ผ
//awt ๋ณด๋‹ค๋Š” swing ์‚ฌ์šฉํ•˜๋Š”๊ฒƒ์ด ์ข‹๋‹ค.
//์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š”
//๊ฐ์ฒด.getDocumentBase -> html ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ
//๊ฐ์ฒด.getCodeBase ->์ž๋ฐ”์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฒƒ
34. Applet์˜Image์™€ Sound<์ฒ˜์Œ์œผ๋กœ>
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Exam_03 extends Applet implements ActionListener{
private Image play_img, stop_img, cur_img;
private AudioClip snd;
private Button play_bt = new Button("PLAY");
private Button stop_bt = new Button("STOP");
private BorderLayout bl = new BorderLayout();
private Panel p = new Panel();
private FlowLayout fl = new FlowLayout();
public void init(){
snd = this.getAudioClip(this.getCodeBase(), "aaa.mid");
play_img = this.getImage(this.getCodeBase(), "play_img.jpg");
stop_img = this.getImage(this.getCodeBase(), "stop_img.jpg");
cur_img = stop_img;
this.setLayout(bl);
p.setLayout(fl);
p.add(play_bt);
p.add(stop_bt);
this.add("South",p);
}
public void start(){
play_bt.addActionListener(this);
stop_bt.addActionListener(this);
}
public void paint(Graphics g){
g.drawImage(cur_img, 50, 50,200,100, this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==play_bt){
cur_img = play_img;
snd.loop();
this.repaint();
}else if(e.getSource()== stop_bt ){
cur_img = stop_img;
snd.stop();
this.repaint();
}
}
}
35. Swing(JFC) ์ปดํฌ๋„ŒํŠธ<์ฒ˜์Œ์œผ๋กœ>
๊ธฐ๋ณธํผ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class Exam_01_sub extends JFrame{
public Exam_01_sub(String title){
super(title);
this.init();
this.start();
this.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
this.setResizable(false);
}
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
}
public void start(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ์‚ฌ์šฉ
}
}
public class Exam_01 {
public static void main(String[] ar){
Exam_01_sub ex = new Exam_01_sub("์ œ๋ชฉ");
}
}
Border ์ปจํฌ๋„ŒํŠธ
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
class Exam_02_sub extends JFrame{
private JButton jbt1 = new
private JButton jbt2 = new
private JButton jbt3 = new
private JButton jbt4 = new
private JButton jbt5 = new
private JButton jbt6 = new
private JButton jbt7 = new
private JButton jbt8 = new
JButton("EMPTY");
JButton("LINE");
JButton("ETCHED");
JButton("BEVEL");
JButton("SOFTBEVEL");
JButton("MATTE");
JButton("TITLED");
JButton("COMPOUND");
private GridLayout gl = new GridLayout(4,2,5,5);
public Exam_02_sub(String title){
super(title);
this.init();
this.start();
this.setSize(300,300);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
this.setResizable(false);
}
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(gl);
EmptyBorder b1 = new EmptyBorder(5,3,5,3);
//ํ‰ํ‰ํ•œ ํ…Œ๋‘๋ฆฌ,
jbt1.setBorder(b1);
con.add(jbt1);
LineBorder b2 = new LineBorder(Color.RED, 3);
jbt2.setBorder(b2);
con.add(jbt2);
EtchedBorder b3 = new EtchedBorder();
//ํ™ˆ์ด ํŒŒ์ž‡๋Š” ํ…Œ๋‘๋ฆฌ
jbt3.setBorder(b3);
con.add(jbt3);
BevelBorder b4 = new BevelBorder(BevelBorder.LOWERED);
jbt4.setBorder(b4);
con.add(jbt4);
SoftBevelBorder b5 = new
SoftBevelBorder(SoftBevelBorder.RAISED);
jbt5.setBorder(b5);
con.add(jbt5);
MatteBorder b6 = new MatteBorder(5,10,15,20, Color.BLUE);
jbt6.setBorder(b6);
con.add(jbt6);
TitledBorder b7 = new TitledBorder("์ œ๋ชฉ");
jbt7.setBorder(b7);
con.add(jbt7);
SoftBevelBorder b8 = new
SoftBevelBorder(SoftBevelBorder.RAISED);
TitledBorder b9 = new TitledBorder("์ œ๋ชฉ์ž„");
CompoundBorder b10 = new CompoundBorder(b8, b9);
jbt8.setBorder(b10);
con.add(jbt8);
}
public void start(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ์‚ฌ์šฉ
}
}
public class Exam_02 {
public static void main(String[] ar){
Exam_02_sub ex = new Exam_02_sub("์ œ๋ชฉ");
}
}
JToggleButton ํด๋ž˜์Šค
import
import
import
import
import
java.awt.*;
java.awt.event.*;
javax.swing.*;
javax.swing.border.*;
javax.swing.event.*;
class Exam_03_sub extends JFrame{
private
private
private
private
ImageIcon
ImageIcon
ImageIcon
ImageIcon
ii1
ii2
ii3
ii4
=
=
=
=
new
new
new
new
ImageIcon("a.gif");
ImageIcon("b.gif");
ImageIcon("c.gif");
ImageIcon("d.dif");
//์•„์ด์ฝ˜์„ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
private
private
private
private
JToggleButton
JToggleButton
JToggleButton
JToggleButton
jtb1
jtb2
jtb3
jtb4
=
=
=
=
new
new
new
new
JToggleButton("1",ii1);
JToggleButton("2",ii2);
JToggleButton("3",ii3);
JToggleButton("4",ii4);
//์•„์ด์ฝ˜์œผ๋กœ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค\
private ButtonGroup bg = new ButtonGroup();
private GridLayout gl = new GridLayout(1,4,5,5);
public Exam_03_sub(String title){
super(title);
this.init();
this.start();
this.setSize(300,200);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
this.setResizable(false);
}
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(gl);
bg.add(jtb1);
bg.add(jtb2);
bg.add(jtb3);
bg.add(jtb4);
con.add(jtb1);
con.add(jtb2);
con.add(jtb3);
con.add(jtb4);
jtb4.setHorizontalTextPosition(SwingConstants.CENTER);
jtb4.setVerticalTextPosition(SwingConstants.BOTTOM);
//์•„์ด์ฝ˜์˜ ์œ„์น˜์—์„œ ์–ด๋””์— ์œ„์น˜ํ• ๊ฒƒ์ธ๊ฐ€
jtb1.setToolTipText("1๋ฒˆ์ž…๋‹ˆ๋‹ค.");
jtb2.setToolTipText("2๋ฒˆ์ž…๋‹ˆ๋‹ค.");
jtb3.setToolTipText("3๋ฒˆ์ž…๋‹ˆ๋‹ค.");
jtb4.setToolTipText("4๋ฒˆ์ž…๋‹ˆ๋‹ค.");
jtb1.setMnemonic('1');
jtb2.setMnemonic('2');
jtb3.setMnemonic('3');
jtb4.setMnemonic('4');
//๋‹จ์ถ•ํ‚ค๋ฅผ ์„ค์ •ํ•œ๋‹ค. Alt ์‚ฌ์šฉํ•ด์„œ ํ•œ๋‹ค.
jtb2.setSelectedIcon(ii1);
//์„ ํƒํ–ˆ์„๋•Œ์˜ ์•„์ด์ฝ˜
jtb2.setRolloverIcon(ii2);
//์˜ฌ๋ ค๋…ธ์™”์„๋•Œ์˜ ์•„์ด์ฝ˜
}
public void start(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ์‚ฌ์šฉ
}
}
public class Exam_03 {
public static void main(String[] ar){
Exam_03_sub ex = new Exam_03_sub("์ œ๋ชฉ");
}
}
JColorChooser
Color cc= JColorChooser.showDialog(this,"์ƒ‰์ƒ์„ ํƒ!", Color.BLACK);
//์นผ๋ผ ๋‹ค์ด๋กœ๊ทธ๊ฐ€ ๋‚˜ํƒ€๋‚œ๋‹ค.
//System.out.print(cc);
JFileChooser
private JFileChooser jfc = new JFileChooser(".");
int x = jfc.showOpenDialog(this);
//jfc.showSaveDialog(this);
//์—ด๊ธฐ ์ €์žฅ ๋‹ค์ด๋กœ๊ทธ๋กœ ๋‚˜ํƒ€๋‚œ๋‹ค.
//์„ ํƒํ•œ ๋ฒ„ํŠผ์˜ ์œ„์น˜์—๋•Œ๋ผ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.(int)
//System.out.println("x = " + x);
JOptionPane
//JOptionPane.showMessageDialog(this, "๊ฒฝ๊ณ ์ž…๋‹ˆ๋‹ค.!", "๊ฒฝ๊ณ !",
JOptionPane.WARNING_MESSAGE);
//๊ฒฝ๊ณ  ๋ฉ”์„ธ์ง€ ์ถœ๋ ฅ ๋‹ค์ด๋กœ๊ทธ(๋А๋‚Œํ‘œ)
int x = JOptionPane.showConfirmDialog(this, "์ •๋ง ์‚ญ์ œํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?",
"์‚ญ์ œ", JOptionPane.YES_NO_CANCEL_OPTION,
JOptionPane.QUESTION_MESSAGE);
//์งˆ๋ฌธ ๋‹ค์ด๋กœํฌ๋ฅผ ์ถœ๋ ฅ(๋ฌผ์Œํ‘œ)
//์„ ํƒํ•œ ๋ฒ„ํŠผ์˜ ์œ„์น˜์—๋•Œ๋ผ ์ˆœ์„œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.(int)
String str = JOptionPane.showInputDialog(this, "์ด๋ฆ„", "์ž…๋ ฅ!",
JOptionPane.QUESTION_MESSAGE);
System.out.print(str);
//๊ธ€์ž ์ž…๋ ฅ ๋‹ค์ด๋กœ๊ทธ๋ฅผ ์„ ํƒํ•˜๊ณ  ๋ฌธ์ž์—ด๋กœ ๋ฆฌํ„ดํ•œ๋‹ค.
JDesktopPane
JInternalFrame
private BorderLayout bl = new BorderLayout();
private JDesktopPane pane = new JDesktopPane();
private JInternalFrame jif1 = new JInternalFrame("1๋ฒˆ",
true,true,true,true);
private JInternalFrame jif2 = new JInternalFrame("2๋ฒˆ",
true,true,true,true);
private JInternalFrame jif3 = new JInternalFrame("3๋ฒˆ",
true,true,true,true);
//ํ”„๋ž˜์ž„ ์•ˆ์— ํ”„๋ž˜์ž„ ์ƒ์„ฑํ• ๋•Œ ์‚ฌ์šฉ
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(bl);
con.add("Center", pane);
pane.add(jif1);
pane.add(jif2);
pane.add(jif3);
jif1.setBounds(0,0, 200,150);
jif2.setBounds(20,20, 200,150);
jif3.setBounds(40,40, 200,150);
//์œ„์น˜์„ค์ •
}
JSplitPane
private JTree tree = new JTree();
private JTextArea jta = new JTextArea();
private JSplitPane jsp = new JSplitPane();
private BorderLayout bl = new BorderLayout();
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(bl);
jsp.setLeftComponent(tree);
jsp.setRightComponent(jta);
con.add("Center",jsp);
}
JTabbedPane
JToolbar
private BorderLayout bl = new BorderLayout();
private JToolBar jtb = new JToolBar();
private JButton jbt1 = new JButton("1๋ฒˆ");
private JButton jbt2 = new JButton("2๋ฒˆ");
private JButton jbt3 = new JButton("3๋ฒˆ");
private JButton jbt4 = new JButton("4๋ฒˆ");
private JTabbedPane jtp = new JTabbedPane();
private JPanel jp1 = new JPanel();
private JPanel jp2 = new JPanel();
private JPanel jp3 = new JPanel();
private JPanel jp4 = new JPanel();
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(bl);
jtb.add(jbt1);
jtb.add(jbt2);
jtb.addSeparator();
jtb.add(jbt3);
jtb.add(jbt4);
con.add("North", jtb);
jp1.setBackground(Color.black);
jp2.setBackground(Color.red);
jp3.setBackground(Color.green);
jp4.setBackground(Color.blue);
jtp.addTab("1๋ฒˆํŒจ๋„", jp1);
jtp.addTab("2๋ฒˆํŒจ๋„", jp2);
jtp.addTab("3๋ฒˆํŒจ๋„", jp3);
jtp.addTab("4๋ฒˆํŒจ๋„", jp4);
con.add("Center", jtp);
}
36. JApplet ์ž‘์„ฑ๋ฒ• <์ฒ˜์Œ์œผ๋กœ>
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Exam_01 extends JApplet{
private JMenuBar mb = new JMenuBar();
private JMenu file = new JMenu("FILE");
private JMenuItem fnew = new JMenuItem("NEW");
public void init(){
file.add(fnew);
mb.add(file);
this.setJMenuBar(mb);
}
public
public
public
public
void
void
void
void
start(){}
paint(Graphics g){}
stop(){}
destroy(){}
}
<html>
<head>
<title>์ œ๋ชฉ</title>
</head>
<body>
<center>
<applet code="Exam_01.class" width="300" height="200">
</applet>
</center>
<body>
</html>
37. JTree ์™€ JTable ํด๋ž˜์Šค <์ฒ˜์Œ์œผ๋กœ>
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.tree.*;
class Exam_02_sub extends JFrame{
private BorderLayout bl = new BorderLayout();
private JSplitPane jsp = new JSplitPane();
private JTree jt;
private JTable jtb;
public Exam_02_sub(String title){
super(title);
this.init();
this.start();
this.setSize(400,400);
Dimension screen =
Toolkit.getDefaultToolkit().getScreenSize();
Dimension frm = this.getSize();
int xpos = (int)(screen.getWidth()/2 - frm.getWidth()/2);
int ypos = (int)(screen.getHeight()/2 - frm.getHeight()/2);
this.setLocation(xpos, ypos);
this.setVisible(true);
this.setResizable(false);
}
public void init(){
Container con = this.getContentPane();
//ํ”„๋ž˜์ž„ ํฌ์žฅ(์ž‘์—…์˜์—ญ)
con.setLayout(bl);
DefaultMutableTreeNode
DefaultMutableTreeNode("ROOT");
DefaultMutableTreeNode
DefaultMutableTreeNode
DefaultMutableTreeNode
root = new
c = new DefaultMutableTreeNode("C:");
d = new DefaultMutableTreeNode("D:");
e = new DefaultMutableTreeNode("E:");
jt = new JTree(root);
root.add(c);
root.add(d);
root.add(e);
jsp.setLeftComponent(jt);
Vector header = new Vector();
header.add("๋ฒˆํ˜ธ");
header.add("์ด๋ฆ„");
header.add("์ฃผ๋ฏผ๋ฒˆํ˜ธ");
Vector data = new Vector();
Vector imsi = new Vector();
imsi.add(1);
imsi.add("๊ฐ•๋ช…์ฐฌ");
imsi.add("111111-1111111");
data.add(imsi);
imsi = new Vector();
imsi.add(2);
imsi.add("๊ฐ•๋ช…์ฐฌ");
imsi.add("111111-1111111");
data.add(imsi);
imsi = new Vector();
imsi.add(3);
imsi.add("๊ฐ•๋ช…์ฐฌ");
imsi.add("111111-1111111");
data.add(imsi);
imsi = new Vector();
imsi.add(4);
imsi.add("๊ฐ•๋ช…์ฐฌ");
imsi.add("111111-1111111");
data.add(imsi);
jtb = new JTable(data,header);
JScrollPane jsp1 = new JScrollPane(jtb);
jsp1.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//์Šคํฌ๋กค๋ฐ” ํ•ญ์ƒ ๋ณด์ด๊ธฐ
jsp.setRightComponent(jsp1);
con.add("Center", jsp);
}
public void start(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//์ฐฝ๋‹ซ๊ธฐ ๋ฒ„ํŠผ ์‚ฌ์šฉ
}
}
public class Exam_02 {
public static void main(String[] ar){
Exam_02_sub ex = new Exam_02_sub("์ œ๋ชฉ");
}
}
DefaultMutableTreeNode imsi
=(DefaultMutableTreeNode) e.getPath().getLastPathComponent();
//์„ ํƒํ•œ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜จ๋‹ค.
imsi.removeAllChildren();
//ํ•˜๋ถ€๋ฉค๋ฒ„๋ฅผ ๋ชจ๋‘ ์ง€์šด๋‹ค.
tree.setSelectionPath(e.getPath());
//์„ ํƒํ•œ๊ณณ์œผ๋กœ ์ด๋™ํ•œ๋‹ค.
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception ee) {
//์ธ๋„์šฐ ํ˜•์‹์˜ Look and Feel ๋กœ ํ•ด์ค€๋‹ค.
38. TCPํ†ต์‹ (InetAddress, Socket, ServerSocket) <์ฒ˜์Œ์œผ๋กœ>
InetAddress ํด๋ž˜์Šค
import java.io.*;
import java.net.*;
public class Exam_02 {
public static void main(String[] ar){
InetAddress ia = null; //์ž์‹ ์˜ IP์ฃผ์†Œ
InetAddress ia1 = null; //๋‹ค๋ฅธ ์ฃผ์†Œ์— ๋”ฐ๋ฅธ IP์ฃผ์†Œ
InetAddress[] ia2 = null; //๋‹ค๋ฅธ ์ฃผ์†Œ์— ๋”ฐ๋ฅธ ๋ชจ๋“  IP์ฃผ์†Œ
try {
ia = InetAddress.getLocalHost();
ia1 = InetAddress.getByName("www.naver.co.kr");
ia2 = InetAddress.getAllByName("www.daum.net");
}catch(UnknownHostException e){}
System.out.println("1 ="+ia.getHostAddress());
System.out.println("1 ="+ia.getHostName());
System.out.println("2 ="+ia1.getHostAddress());
System.out.println("2 ="+ia1.getHostName());
System.out.println();
for(int i = 0; i<ia2.length; ++i){
System.out.println("3 ="+ia2[i].getHostAddress());
System.out.println("3 ="+ia2[i].getHostName());
}
}
}
Socket ํด๋ž˜์Šค
import java.io.*;
import java.net.*;
public class Exam_03 {
public static void main(String[] ar){
Socket soc = null;
InetAddress ia = null;
try {
ia = InetAddress.getByName("www.daum.net");
}catch(UnknownHostException e){}
try{
soc = new Socket(ia,80);
String request
= "GET http://www.daum.net/index.html HTTP1/0\r\n\n";//์š”์ฒญ
//์ถœ๋ ฅ
OutputStreamWriter osw = new
OutputStreamWriter(soc.getOutputStream());
BufferedWriter bos = new BufferedWriter(osw,512);
PrintWriter pw = new PrintWriter(bos);
pw.println(request);
pw.flush();
//์ž…๋ ฅ
InputStreamReader isr = new
InputStreamReader(soc.getInputStream());
BufferedReader br = new BufferedReader(isr,512);
while(true){
String str = br.readLine();
if(str == null) break;
System.out.println(str);
}
pw.close();
br.close();
}catch(IOException e){}
}
}
ServerSocket ํด๋ž˜์Šค
import java.io.*;
import java.net.*;
public class Round24_Ex04 {
public static void main(String[] ar) {
// TCP์˜ ์—ด๋ ค์ ธ ์žˆ๋Š” ํฌํŠธ ํ™•์ธํ•˜๊ธฐ
ServerSocket ss = null;
for (int i = 1; i <= 65535; i++) {
try {
ss = new ServerSocket(i);
ss.close();
} catch (IOException ee) {
System.out.println(i + "๋ฒˆ TCP ํฌํŠธ๊ฐ€ ์—ด๋ ค
์žˆ์Šต๋‹ˆ๋‹ค.");
}
}
}
}
Related documents