Survey
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project
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 ํฌํธ๊ฐ ์ด๋ ค
์์ต๋๋ค.");
}
}
}
}