1 / 65

제 7 장 AWT(Abstract Window Toolkit)

제 7 장 AWT(Abstract Window Toolkit). Component 2D. AWT. GUI 프로그램을 작성 하기 위한 클래스들의 묶음 Write Once , Run Everywhere 버튼 , 체크 박스 등을 Component 라고 한다 . Component 를 담을 수 있는 것을 Container 라고 한다 . Component 클래스는 추상 클래스이기 때문에 instance 를 만들어 사용 할 수 없다. Component. Button. Canvas. Checkbox.

peta
Download Presentation

제 7 장 AWT(Abstract Window Toolkit)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 제 7 장 AWT(Abstract Window Toolkit) Component 2D

  2. AWT GUI 프로그램을 작성 하기 위한 클래스들의 묶음 Write Once , Run Everywhere 버튼,체크 박스 등을 Component 라고 한다. Component를 담을 수 있는 것을 Container 라고 한다. Component 클래스는 추상 클래스이기 때문에 instance를 만들어 사용 할 수 없다.

  3. Component Button Canvas Checkbox ScrollPane Choice Panel Applet Container Window Frame Label Dialog List FileDialog Scrollbar TextArea TextComponent TextField AWT

  4. Frame • 윈도우 Component를 담는 기본 윈도우 • 생성 • Frame() • Frame( String title ) • setSize(int width,int height) 메서드로 크기 조정 • setVisible(true) 메서드로 화면에 표시

  5. Frame import java.awt.*; public class Ex001Frame { public static void main( String args[] ) { Frame f = new Frame("첫 번째 Frame"); f.setSize(300,200); f.setVisible(true); } }

  6. Label • 문자열을 출력 • 생성 • Label() • Label( String text ) • Label( String text , int alignment ) • Label.LEFT , Label.RIGHT , Label.CENTER - alignment 값 • setText(String text) –문자열 설정 • getText() –문자열 내용 반환 • add() – Frame 에 Label을 부착

  7. Label import java.awt.*; public class Ex002Label extends Frame { public Ex002Label() { super("Label test"); Label label1 = new Label("하하하"); add( label1 ); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new Ex002Label(); } }

  8. Quiz 일정 시간이 지나면 내용이 규칙적으로 바뀌는 프로그램을 작성 하시오

  9. Quiz Scroll하는 문자열을 출력하는 프로그램을 작성 하시오. 가나다라마바사아 나다라마바사아가 다라마바사아가나

  10. Button • 생성 • Button( String title ) • getLabel() –버튼의 타이틀을 구함 • setLabel() –버튼의 타이틀을 변경

  11. Button import java.awt.*; public class Ex003Button extends Frame { public Ex003Button() { super("Button test"); Button button1 = new Button("하하하"); add( button1 ); setSize(300,200); setVisible(true); } public static void main( String args[] ) { new Ex003Button(); } }

  12. Checkbox • 생성 • Checkbox() • Checkbox(String text) • Checkbox(String text , boolean state) • Checkbox(String text , CheckboxGroup group , boolean state) • getLabel() – Checkbox의 타이틀을 구함 • setLabel() – Checkbox의 타이틀을 변경 • getState() – Checkbox의 on/off 상태를 구함 • setState() – Checkbox의 on/off 상태를 설정

  13. Checkbox import java.awt.*; public class Ex004Checkbox extends Frame { public Ex004Checkbox() { super("Checkbox test"); setLayout(new FlowLayout()); Checkbox check1 = new Checkbox("하하하1"); Checkbox check2 = new Checkbox("하하하2"); Checkbox check3 = new Checkbox("하하하3"); add( check1 ); add( check2 ); add( check3 ); setSize(300,200); setVisible(true); } .............................

  14. Quiz Checkbox를 선택하면 선택된 모든 Checkbox의 문자를 Label에 출력하는 프로그램을 작성 하시오

  15. Radio Button Checkbox Class 와 CheckboxGroup Class 와 같이 사용한다. import java.awt.*; public class Ex005RadioButton extends Frame { public Ex005RadioButton() { super("Radio Button test"); setLayout(new FlowLayout()); CheckboxGroup cbg = new CheckboxGroup(); Checkbox check1 = new Checkbox("하하하1",cbg,true); Checkbox check2 = new Checkbox("하하하2",cbg,false); Checkbox check3 = new Checkbox("하하하3",cbg,false); add( check1 ); add( check2 ); add( check3 ); .....................

  16. Quiz CheckboxGroup를 포함한 Checkbox를 선택하면 선택된 Checkbox의 문자를 Label에 출력하는 프로그램을 작성 하시오

  17. Choice Button • 생성 • Choice() • add(String) –항목을 추가 • getItemCount() –항목 개수를 구함 • getItem(int) –해당 항목을 구함 • remove(int) –해당 항목을 삭제 • getSelectedItem() –선택된 항목을 구함 • insert(String,int) –특정 위치에 항목을 추가 • 항목의 인덱스는 ‘0’부터 시작

  18. Choice Button import java.awt.*; public class Ex006Choice extends Frame { public Ex006Choice() { super("Choice Button test"); setLayout(new FlowLayout()); Choice choice1 = new Choice(); choice1.add("하하하1"); choice1.add("하하하2"); choice1.add("하하하3"); add( choice1 ); setSize(300,200); setVisible(true); ....................

  19. Quiz Choice Button에서 선택된 Item을 Label에 출력하는 프로그램을 작성 하시오

  20. List • 여러 개의 항목들 중에서 하나 또는 여러 개를 선택 할 수 있다. • 생성 • List() • List(int rows) • List(int rows, boolean multipleMode) • add(String) –항목을 추가 • add(String,int) –특정 위치에 항목을 추가 • getItemCount() –항목 개수를 구함 • getItem(int) –해당 항목을 구함 • remove(int) –해당 항목을 삭제 • getSelectedItem() –선택된 항목을 구함 • getSelectedItems() –선택된 항목을 구함 • 항목의 인덱스는 ‘0’부터 시작

  21. List import java.awt.*; public class Ex007List extends Frame { public Ex007List() { super("List test"); setLayout(new FlowLayout()); List List1 = new List(3,false); List1.add("하하하1"); List1.add("하하하2"); List1.add("하하하3"); List1.add("하하하4"); List1.add("하하하5"); add( List1 ); ...................

  22. TextArea • 여러 줄에 걸쳐 문장을 작성 할 수 있다. • 생성 • TextArea() • TextArea(String text) • TextArea(int rows, int columns) • TextArea(String text , int rows, int columns) • append(String) –문자열을 추가 • setText(String) –문자열을 설정 • getText() –내용을 구함 • insert(String,int) –해당 위치에 문자열을 추가

  23. TextArea import java.awt.*; public class Ex008TextArea extends Frame { public Ex008TextArea() { super("TextArea test"); setLayout(new FlowLayout()); TextArea text1 = new TextArea(5,30); add( text1 ); ..........................

  24. TextField • 한 줄의 문장을 작성 할 수 있다. • 생성 • TextField() • TextField(int columns) • TextField(String text) • TextField(String text, int columns) • setText(String) –문자열을 설정 • getText() –내용을 구함 • setEchoChar(char) –해당 문자로 입력 값을 치환 • setEditable(bolean) –입력 가능 ,불 가능 설정

  25. TextField import java.awt.*; public class Ex009TextField extends Frame { public Ex009TextField() { super("TextField test"); setLayout(new FlowLayout()); TextField text1 = new TextField("하하하",30); add( text1 ); ...........................

  26. Scrollbar • 가로 또는 세로의 스크롤바 • 생성 • Scrollbar() • Scrollbar(Scrollbar.VERTICAL) • Scrollbar(Scrollbar.HORIZONTAL) • Scrollbar(intorientation , intvalue, intvisible, intminimum, intmaximum ) • getValue() –스크롤바의 현재 값 반환 • Scrollbar(Scrollbar.HORIZONTAL,0, 60, 0, 300);

  27. Scrollbar import java.awt.*; public class Ex010Scrollbar extends Frame { public Ex010Scrollbar() { super("Scrollbar test"); setLayout(new FlowLayout()); Scrollbar scroll1 = new Scrollbar(Scrollbar.HORIZONTAL); Scrollbar scroll2 = new Scrollbar(Scrollbar.HORIZONTAL,100, 60, 0, 300); add( scroll1 ); add( scroll2 ); ...........................

  28. Canvas • 그림을 그리는데 사용 된다. • 생성 • Canvas() • setSize(int width,int height) –크기 조정 • setBackground(Color ) –바탕 색 설정

  29. Canvas import java.awt.*; public class Ex011Canvas extends Frame { public Ex011Canvas() { super("Canvas test"); setLayout(new FlowLayout()); Canvas can1 = new Canvas(); can1.setSize(200,100); can1.setBackground(Color.red); add( can1 ); ......................

  30. Panel • 다른 Container를 포함 할 수 있는 Container이다. • 생성 • Panel() • Panel(LayoutManager layout)

  31. Panel import java.awt.*; public class Ex012Panel extends Frame { public Ex012Panel() { super("Panel test"); setLayout(null); Panel pan1 = new Panel(); pan1.setSize(200,100); pan1.setBackground(Color.red); pan1.setLocation(50,50); add( pan1 ); ......................

  32. Panel import java.awt.*; public class Ex013Panel extends Frame { public Ex013Panel() { super("Panel test"); setLayout(null); Panel pan1 = new Panel(); pan1.setSize(200,100); pan1.setBackground(Color.red); pan1.setLocation(50,50); add( pan1 ); Button button1 = new Button("하하하"); Button button2 = new Button("호호호"); pan1.add( button1 ); pan1.add( button2 ); ......................

  33. ScrollPane • 스크롤을 기본적으로 가지는 Component • 하나의 Component만 포함 할 수 있다 ( Panel을 포함 한 후 다른 Component를 포함 시킨다 ) • 생성 • ScrollPane() • ScrollPane(intscrollbarDisplayPolicy )

  34. ScrollPane import java.awt.*; public class Ex014ScrollPane extends Frame { public Ex014ScrollPane() { super("ScrollPan test"); setLayout(null); ScrollPane sPane1 = new ScrollPane(); sPane1.setSize(200,100); sPane1.setBackground(Color.red); sPane1.setLocation(50,50); Panel pan1 = new Panel(); TextArea text1 = new TextArea(300,500); pan1.add( text1 ); sPane1.add( pan1 ); add( sPane1 ); ......................

  35. Popup Menu • 평소에 안보이다가 마우스를 누르면 나타나는 메뉴 • 생성 • PopupMenu() • PopupMenu(String label) • add() – Popup Menu에 메뉴를 추가 • show() – Popup Menu를 출력

  36. Popup Menu import java.awt.*; public class Ex015PopupMenu extends Frame { public Ex015PopupMenu() { super("Popup Menu test"); setLayout(null); Canvas can1 = new Canvas(); can1.setSize(200,100); can1.setBackground(Color.red); PopupMenu pMenu1 = new PopupMenu("Popup Menu"); pMenu1.add(new MenuItem("하하하1")); pMenu1.add(new MenuItem("하하하2")); pMenu1.add(new MenuItem("하하하3"));

  37. Popup Menu Menu menu1 = new Menu("호호호"); menu1.add(new MenuItem("호호호1")); menu1.add(new MenuItem("호호호2")); menu1.add(new MenuItem("호호호3")); pMenu1.add( menu1 ); can1.add( pMenu1 ); add( can1 ); setSize(300,200); setVisible(true); pMenu1.show(can1,50,50); } .....................

  38. 배치 컴퍼넌트는 컨테이너라는 컴퍼넌트에 붙일 수 있다. 컨테이너는 Frame,Panel,Window,Applet 등이 있다. 컴퍼넌트를 붙일 때 크기와 위치는 레이아웃 매니져(LayoutManager ) Class가 결정 한다. 프로그래머가 지정한 위치와 크기는 LayoutManager에 의하여 무시 된다. 하나의 Container는 하나의 LayoutManager를 가질 수 있다. FlowLayout , BorderLayout , GridLayout , CardLayout , GridBagLayout 이 있다.

  39. FlowLayout Panel,Applet의 기본 Layout 이다. Component들을 가로 방향(왼쪽에서 오른쪽)으로 늘어 놓는다. setLayout(new FlowLayout()) – Component 간격 가로/세로 5픽셀 setLayout(new FlowLayout(FlowLayout.LEFT)) – Component의 정렬 위치는 Container의 왼쪽 ( LEFT,RIGHT,CENTER가 있으며 기본 값은 CENTER이다) setLayout(new FlowLayout(FlowLayout.RGIHT,5,10) – Component의 가로 간격 5픽셀 , 세로 간격 10픽셀

  40. FlowLayout import java.awt.*; public class Ex016FlowLayout extends Frame { public Ex016FlowLayout() { super("FlowLayout test"); setLayout(new FlowLayout()); Button button1 = new Button("하하하하하하1"); Button button2 = new Button("하하하하하하2"); Button button3 = new Button("하하하하하하3"); .......................... add( button1 ); add( button2 ); add( button3 ); ...........................

  41. BorderLayout Frame의 기본 Layout 이다. North,South,West,East,Center 중 하나의 위치를 가진다. setLayout(new BorderLayout()) – Component 간격 0픽셀 setLayout(new BorderLayout(5,10)) – Component의 간격은 가로 5, 세로 10 픽셀 add(“North”, new TextField(“Name”,30) – TextField Component를 북쪽에 위치 시킨다.

  42. BorderLayout import java.awt.*; public class Ex017BorderLayout extends Frame { public Ex017BorderLayout() { super("BorderLayout test"); setLayout(new BorderLayout()); Button button1 = new Button("111"); ................................ add( "East" , button1 ); add( "West" , button2 ); add( "South" , button3 ); add( "North" , button4 ); add( BorderLayout.CENTER , button5 ); ...........................

  43. GridLayout 행과 열의 수에 맞게 배치 setLayout(new GridLayout()) – 1행 1열 setLayout(new GridLayout(3,4)) – 3행 4열 setLayout(new GridLayout(3,4,5,10)) – 3행 4열 가로 5 픽셀 , 세로 10 픽셀

  44. GridLayout import java.awt.*; public class Ex018GridLayout extends Frame { public Ex018GridLayout() { super("GridLayout test"); setLayout(new GridLayout(2,3)); Button button1 = new Button("111"); ........................... add( button1 ); add( button2 ); add( button3 ); add( button4 ); add( button5 ); ...........................

  45. CardLayout • 여러 개의 카드를 포개어 놓은 Layout • CardLayout 생성 순서 • 생성자를 이용하여 생성 • CardLayout card = new CardLayout(); • LayoutManager에 등록 • setLayout( card ); • 카드 역할을 하는 Panel등록 • Panel p1 = new Panel(); • Panel p2 = new Panel(); • Panel을 Container 에 등록 • add(“First” , p1 ); • 원하는 카드를 출력 • card.show(this,”First”);

  46. CardLayout import java.awt.*; public class Ex019CardLayout extends Frame { public Ex019CardLayout() { super("CardLayout test"); CardLayout card = new CardLayout(); setLayout(card); Panel p1 = new Panel(); ........................ p1.setBackground( Color.red ); ......................... add( "First" , p1 ); ......................... card.show(this,"Third"); ...........................

  47. GridBagLayout • Component 의 위치를 자유롭게 만든다. • 생성자를 이용해서 GridBagLayout을 생성 • GridBagLayout gb = new GridBagLayout(); • 그리드백 constraint를 만든다. • GridBagConstraints constraint = new GridBagConstraints(); • Layout Manager 에 등록 • setLayout( gb ); • 콘스트레인트에 변수 값을 할 당 • constraint.weightx = 1.0; • Component의 Constraint를 설정 한다. • gb.setConstraints(b1,constraint); • Component 추가 • add(b1);

  48. GridBagLayout import java.awt.*; public class Ex020GridBagLayout extends Frame { public Ex020GridBagLayout() { super("GridBagLayout test"); GridBagLayout gb = new GridBagLayout(); GridBagConstraints constraint = new GridBagConstraints(); setLayout(gb); constraint.fill = GridBagConstraints.BOTH; constraint.weightx = 1.0; Button b1 = new Button("Button1"); gb.setConstraints(b1,constraint); add(b1); ...........................

  49. 레이아웃 사용하지 않기 위치와 크기를 자유롭게 결정하여 Component를 출력하고자 할 때 사용 setLayout( null ) 로 정의 한다. setSize , setLocation 메서드로 Component마다 크기와 위치를 지정 해 주어야 한다. 위치를 지정하지 않으면 좌측 상단으로 지정 된다. 같은 위치를 지정하면 먼저 지정된 Component가 우선 한다.

  50. Component 크기 변경 Component의 크기는 getPreferredSize() 메서드에 의하여 가장 적절한 크기로 결정 된다. getPreferredSize() 메서드를 Override하여 Component의 크기를 결정한다. 프레임 내부의 Component의 간격을 조절하기 위해서는 getInsets() 메서드를 Override한다.

More Related