본문 바로가기

JAVA

#8 중첩클래스, TUI, AWT, 데이터저장

<중첩클래스> 포함,Nested

- 클래스내에 또 다른 클래스를 구성하는 것.

※ 주의 : 메소드 내에는 다른 메소드를 정의 할 수 없음!!


종류)

정적 중첩클래스 - static이 선언된 내부클래스

외부의 자원을 사용할 때 static붙는 인스턴스만 접근가능.

비정적 중첩클래스 - static이 선언되지 않은 내부클래스

inner클래스!!

class A{ //외부클래스, Outer클래스, Top-level클래스

//필드

//메소드

//1차 자원 정의

int i=11;

void hello(){

print(); (X)

B b = new B();

b.print(); (O)

}

class B{ //내부클래스, Inner클래스

//2차 자원 정의

void callTest(){

hello(); (O)

}

void print(){

}

}//B class

}//A class

저장: A.java ----> 컴파일 결과 ---> A.class A$B.class


<NestedTest>

/* public|abstract|final */ class NestingClass{//외부클래스

int i=11;

void hello() {

System.out.println("안녕~!!");

NestedClass nested = new NestedClass();

nested.print();

}

/*static*/ class NestedClass{//내부클래스(inner클래스)

int j=22;

void callTest() {

System.out.println("i="+i);

hello();

}

void print() {

System.out.println("프린트~!!");

}

}

}//NestingClass

public class NestedTest {

public static void main(String[] args) {

NestingClass nesting = new NestingClass();

nesting.hello();

//내부 클래스의 print()메소드를 main()에서 직접 호출하고 싶다

NestingClass.NestedClass nested = new NestingClass().new NestedClass();

nested.print();

}

}//NestedTest


* TUI (Text User Interface)

-프로그램 실행을 Text에 의존해서 실행.

ex) 메뉴에서 1을 누르면 추가, 2를 누르면 검색, 3을 누르면 종료.

* AWT (Abstract Window Toolkit)

- GUI(Graphic User Interface)환경을 구축하기위한 class들의 묶음.

- GUI와 관련된 class들의 묶음. java.awt.*;

1. Component : Menu, Button, Label, Choice, Checkbox, List

TextField, TextArea, Scrollbar.....

2. Container : Component의 객체를 생성한 후에 그 객체를 배치한다.

Object

Component

Container

Panel Window

Applet Frame


default: FlowLayout BorderLayout

Panel : display(x) , 한 영역에 두개 이상의 컴포넌트를 붙일때 사용.

컴포넌트 속성을 지정하기위해 사용.

Frame : display(o)

3. Event 처리: Component에 기능을 실제로 부여하는 것.

* Container의 배치방법(LayoutManager)

----> 컴포넌트를 컨테이너에 어떻게 위치시킬지 방법을 정의.

Frame f = new Frame();

Button b = new Button();

FlowLayout fl = new FlowLayout();

f.setLayout(fl);//레이아웃(배치) 설정

f.add(b);

- FlowLayout(가운데 정렬)

: 가운데를 기준으로 해서 Component들이 배치.

(Frame크기에 따라 배치가 변경)

- BorderLayout(방위정렬)

: 방위에 따라 사용자가 임의 배치 가능 (동,서,남,북, 중앙)

: 상대적으로 큰 영역데 강조할 컴포넌트를 배치.

- GridLayout(같은 크기 정렬, 행열표현 정렬)

: Container의 크기를 같은 크기로 나누어 Component를 붙인다.

- CardLayout(같은 위치 정렬)

: 같은 위치에 여러개의 Component를 배치할 때 사용.


<BorderTest>

import java.awt.BorderLayout;

import java.awt.Button;

import java.awt.Frame;

public class BorderTest extends Frame{//방위정렬

//객체(컴포넌트,컨테이너)선언

Button bt1,bt2,bt3,bt4,bt5;

//Frame f;//null

public BorderTest(String title) {

//super(); 생략

super(title);

//객체생성

bt1 = new Button("1");

bt2 = new Button("2");

bt3 = new Button("3");

bt4 = new Button("4");

bt5 = new Button("5");

setTitle("타이틀변경~!!");

//객체에 속성설정(컨테이너에 대한 레이아웃, 컴포넌트 붙이기)

//this.setLayout(new BorderLayout());

setLayout(new BorderLayout());

//BorderLayout(방위정렬)의 경우 컴포넌트를 붙일 위치를 지정!!

add("North",bt1);//f.add(String 붙일위치, 붙일 컴포넌트)

add("East",bt2);

add("South",bt3);

add("West",bt4);

add("Center",bt5);

//마무리(컨테이너 크기설정, 보이기 설정)

setSize(300, 300);

setVisible(true);

}//생성자


public static void main(String[] args) {

new BorderTest("Border테스트!!"); //자식은 부모다 ==> BorderTest는 Frame

new Frame("타이틀!!");

}

}

<FlowTest>

import java.awt.Button;

import java.awt.FlowLayout;

import java.awt.Frame;

public class FlowTest {//가운데 정렬 테스트

//필요한 객체(컴포넌트, 컨테이너) 선언

Button bt1, bt2, bt3;

// Button bt2;

// Button bt3;

Frame f;

public FlowTest() {//생성자 : 초기화 관련

//객체 생성

//new Button(String label)

bt1 = new Button("행복버튼1");

bt2 = new Button("행복버튼2");

bt3 = new Button("행복버튼3");

//new Frame(String title)

f = new Frame("Flow테스트");

//속성지정(설정)

//컨테이너의 레이아웃 설정(Flow,Grid,Border,Card)

//FlowLayout fl = new FlowLayout();

f.setLayout(new FlowLayout());//fl);

f.add(bt1); //컨테이너.add(붙일 컴포넌트);

f.add(bt2); //컨테이너.add(붙일 컴포넌트);

f.add(bt3); //컨테이너.add(붙일 컴포넌트);

//마무리 메소드 2개(프레임창의 크기, 프레임창의 보이기 설정)

//f.setSize(int width,int height);

f.setSize(300,300);

f.setVisible(true);

}//생성자

public static void main(String[] args) {

//생성자 호출

//FlowTest ft = new FlowTest();

new FlowTest();

}//main

}

<GridTest>

import java.awt.Button;

import java.awt.Frame;

import java.awt.GridLayout;

public class GridTest extends Frame{

//Grid: 격자,행열 ===> 2행 3열에 버튼 붙이기

Button b1,b2,b3,b4,b5,b6;

public GridTest() {

setTitle("Grid테스트");

b1 = new Button("1");

b2 = new Button("2");

b3 = new Button("3");

b4 = new Button("4");

b5 = new Button("5");

b6 = new Button("6");

//setLayout(new GridLayout());

//setLayout(new GridLayout(2,3)); //2행 3열 배치

//setLayout(new GridLayout(int rows,int cols, int hgap, int vgap)); //2행 3열 배

//rows:행의 수, cols:열의 수, hgap:수평으로 놓여진 컴포넌트 사이 간격,

vgap:수직 컴포넌트 사이 간격

setLayout(new GridLayout(2,3, 10, 0)); //2행 3열 배치

add(b1);

add(b2);

add(b3);

add(b4);

add(b5);

add(b6);

setSize(300, 300);

setVisible(true);

}//생성자

public static void main(String[] args) {

new GridTest();

}

}

<DataStore>

import java.util.Vector;

public class DataStoreSearchTest {

public static void main(String[] args) {

//데이터저장(주기): set, 데이터조회(얻기): get

//변수에 데이터 저장

String name="나길동";

System.out.println(name);//name은 "나길동"

System.out.println("나길동 길이==>"+ name.length());

//배열에 데이터 저장

//String [] names= {"너길동"};

String [] names = new String[3];//names[0]번지~names[2]번지 생성

names[0] = "너길동";

System.out.println(names[0]);

System.out.println("너길동 길이==>"+ names[0].length());

//벡터에 데이터 저장

Vector<String> v = new Vector<>();

v.add("우리길동");

System.out.println(v.get(0));//벡터에 저장된 첫번째 데이터 얻어오기

System.out.println("우리길동 길이==>"+ v.get(0).length());

//★ name == names[인덱스] == 벡터명.get(인덱스) == String데이터!!

// == 빈즈명.getName()

//빈즈에 데이터 저장

Person p = new Person();//new Person("동에번쩍");

//p.name="동에번쩍";

//System.out.println(p.name);

p.setName("서에번쩍");

System.out.println(p.getName());

System.out.println("서에번쩍 길이==>"+ p.getName().length());

}//main

}