PowerPoint 프레젠테이션

Download Report

Transcript PowerPoint 프레젠테이션

Java Programming Language (5)
- AWT programming 1 -
Company
Logo
@
Dong In Shin
DCS lab
Seoul National University
AWT programming





java.awt package
AWT component class
Component의 화면 배치
Container & Panel component
Layout management



FlowLayout, BorderLayout, GridLayout,
GridBagLayout, CardLayout
Label, Button, Canvas component
Window , Frame component
[email protected]
AWT programming





AWT event Processing Model
Adapter class
AWT event class의 종류
Event handler의 등록
여러 가지 components


TextField, TextArea, Checkbox, Choice, List,
Scrollbar, ScrollPane…
AWT 문서

http://java.sun.com/products/jdk/awt/
[email protected]
AWT component의 종류
[email protected]
AWT component의 종류

기본 control


사용자 입력의 다른 방법


screen에 graphic을 나타낼 수 있게 한다.
Label


Scrollbar, TextArea
Canvas


Button, Checkbox, Choice, List, MenuItem, TextFileld
선택할 수 없는 문자 라인을 출력하는 class
Container



다른 component를 담을 수 있는 UI
Container 클래스의 하위 클래스
세 종류의 container가 존재

Window 하위 class Dialog, FileDialog, Frame

Panel

ScrollPane
[email protected]
AWT component의 종류
[email protected]
AWT classes

Layout manger : component의 배치와 크기를 조절

크기와 형태를 지정해 놓은 클래스들





Color : 색의 지정과 조작
Image




Image
Toolkit : 플랫폼에 의존하는 AWT에 대하여 플랫폼 독립적인 인터페이스 제공
MediaTracker : 이미지 로딩의 상태를 추적
Font



Dimension : 사각형의 크기
Insets : 컨테이너의 주변부와 컨테이너 내부와의 관계
Point, Rectangle, Polygon
Font : 여러 가지 글자 폰트와 그에 관한 정보
FontMetrics : 특정한 폰트에 대한 상세한 정보를 얻게 해 준다.
Graphics
[email protected]
Component의 일반적인 사용 규칙

Container에 component 추가


Container의 Layout에 따라 여러 가지 add method 사용
Component의 일반적인 기능들

기본적인 painting method


Event handling






여러 종류의 event 와 keyboard focus를 제공
외형 제어


Paint(Graphics g), update(Graphics g), repaint..
Font : setFont(), getFont()
color : setForeground(), getForeground( ), setBackground( ),
getBackground( )
Visible : setVisible()
Component의 활성화 , 무력화 : setEnabled(boolean)
Image handling
Cursor : setCursor(), getCursor()
[email protected]
Button 컴포넌트
import java.awt.*;
class LabelButtonTest {
public static void main(String[] args) {
Frame f = new Frame();
f.setLayout(new GridLayout() );
Label label1 = new Label();
Label label2 = new Label();
Button button = new Button();
f.add(label1);
f.add(label2);
f.add(button);
label1.setText("첫번째 레이블");
label2.setText("두번째 레이블");
label2.setAlignment(Label.CENTER);
button.setLabel("보턴");
System.out.println(button.getActionCommand() );
f.setSize(400, 100);
f.setVisible(true);
}
}
[email protected]
Checkbox 컴포넌트

Checkbox


ChckboxGroup


두개의 상태에서 선택
여러 개의 체크박스 중 배타적으로 하나만 선택되어 지도록 하기 위해서 체크박스를 체
크박스 그룹에 더한다.
ItemListener 와 관련
[email protected]
Checkbox 컴포넌트
import java.awt.*;
import java.applet.Applet;
public class CheckboxDemo extends Applet {
public void init() {
Panel p1, p2;
Checkbox cb1, cb2, cb3;
Checkbox cb4, cb5, cb6;
CheckboxGroup cbg;
cb1 = new Checkbox();
cb1.setLabel("Checkbox 1");
cb2 = new Checkbox("Checkbox 2");
cb3 = new Checkbox("Checkbox 3");
cb3.setState(true);
p1 = new Panel();
p1.setLayout(new FlowLayout());
p1.add(cb1);
p1.add(cb2);
p1.add(cb3);
[email protected]
cbg = new CheckboxGroup();
cb4 = new Checkbox("Checkbox 4", cbg,
false);
cb5 = new Checkbox("Checkbox 5", cbg,
false);
cb6 = new Checkbox("Checkbox 6", cbg,
false);
p2 = new Panel();
p2.setLayout(new FlowLayout());
p2.add(cb4);
p2.add(cb5);
p2.add(cb6);
setLayout(new GridLayout(0, 2));
add(p1);
add(p2);
validate();
}
}
Frame component


Frame 클래스는 applet이나 application을 위한 window
를 제공한다.
method





String getTitle(), void setTitle(String)
Image getIconImage(), void setIconImage(Image)
MenuBar getMenuBar(), void setMenuBar(MenuBar)
remove(MenuComponent)
boolena isResizable(), setResizable(boolean)
[email protected]
Frame Example
import java.awt.*;
import java.io.File;
class FrameTest {
public static void main(String[] args) {
Frame f = new Frame();
f.setTitle("Frame Test");
f.setSize(100,100);
f.setResizable(false);
Image icon = Toolkit.getDefaultToolkit().getImage(
".." + File.separator + ".." +
File.separator
+ "pencil.gif");
f.setIconImage(icon);
f.setVisible(true);
}
}
[email protected]
List component

목적(다음 경우에 적당)



선택 아이템이 항상 보이게 할 때
선택할 아이템이 매우 많을 때  스크롤 기능
한번에 여러 개의 아이템을 선택할 수 있게 할 때

ItemListner

method





List(int)
String getItem(int)
String[ ] getItems( )
int getItemCout( )
int getRows( )
[email protected]
List component









Int getSelectedIndex( ), String getSelectedItem( )
int[ ] getSelectedIndexes( ), String[ ]
getSelectedItems( ) ,Object[ ] getSelectedObjects( )
void select(int), void deselect( )
boolena isIndexSelected(int), boolean
isItemSelected(String)
void addItem(String, int)
void replaceItem(String, int)
void delItem(it), void remove(int), void remove(String),
void removeAll( )
boolean isMultipleMode( ), setMultipleMode(boolean)
void makeVisible(int), int getVisibleIndex( )
[email protected]
List Example
import java.awt.*;
import java.awt.event.*;
class ListTest extends Frame {
List places = new List(4); {
places.add("seoul");
places.add("Inchoen");
places.add("Taejon");
places.add("Taeku");
places.add("Pusan");
}
TextField selectedPlace = new TextField(8); {
selectedPlace.setEditable(false); }
List courses = new List(4, true); {
courses.add("Java");
courses.add("Unix Administration");
courses.add("Windows Nt administration");
courses.add("Network");
courses.add("CGI Programming");
courses.add("Database");
}
[email protected]
List Example
List selectedCourses = new List(4, true); {
places.addItemListener(new ItemListener( ) {
public void itemStateChanged(ItemEvent ev) {
selectedPlace.setText(places.getSelectedItem());
}
});
courses.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent ev) {
String[] selectedItems =
courses.getSelectedItems();
selectedCourses.removeAll();
for(int i = 0 ; i < selectedItems.length; ++i)
selectedCourses.add(selectedItems[i]);
}
});
setTitle("ListTest");
setLayout(new FlowLayout(FlowLayout.LEFT));
Panel p1 = new Panel();
p1.add(new Label("Place for lesson: "));
p1.add(places);
add(p1);
[email protected]
List Example
Panel p2 = new Panel();
p2.add(new Label("Selected Place: "));
p2.add(selectedPlace);
add(p2);
Panel p3 = new Panel();
p3.add(new Label("Courses: "));
p3.add(courses);
add(p3);
Panel p4 = new Panel();
p4.add(new Label("Selected Courses:
"));
p4.add(selectedCourses);
add(p4);
}
public static void main(String[] args) {
Frame f = new ListTest();
f.setSize(300, 300);
f.setVisible(true);
}
}
[email protected]
List Example
[email protected]
Panel



범용의 container
Component 를 보유하거나 특수한 기능- 보드를 그리거나 이벤
트 핸들링을 사용할 때 - 을 수행하는 하위 클래스를 정의할 때 사
용
applet의 상위 클래스
[email protected]
문자 편집

TextField


한 줄의 문자 편집기
Method




getColumns( )
setEchoChar(char c)
getEchoChar( ), echoCharIsSet( )
TextArea


여러 줄의 문자열 편집
Method



getRows( ), getColumns( )
insert(String, int)
void replaceRange(String, int, int)
[email protected]
Example
import java.awt.*;
import java.awt.event.*;
public class TextAreaTest extends Frame {
TextArea view = new TextArea();
TextField input = new TextField(); {
add(view, "Center");
add(input, "South");
input.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ev) {
view.append(input.getText() + '\n');
input.setText("");
}
});
}
public static void main(String[] args) {
TextAreaTest f = new TextAreaTest();
f.setSize(500, 300);
f.setVisible(true);
}
}
[email protected]
배치 관리자

목적


배치 관리자는 다음을 사용하여 Container 내의 각 child
component를 배치한다.




Container 내부의 component의 font size가 변하거나 실행 환경이
달라져도 올바르게 자동적으로 배치 한다.
배치 관리자의 배치 전략
Container의 크기
각 child component의 배치 정보
배치 전략에 따른 LayoutManger interface를 구현하는
class



FlowLayout : 왼쪽에서 오른쪽으로 배치
BorderLayout : 동,서,남,북, 중앙에 배치
GridLayout : compont를 동일한 크기로 배치
[email protected]
FlowLayout




component를 왼쪽에서 오른쪽으로 배치(공간이 부족하면 그
다음열에 배치)
각 component를 최적의 크기로 배치한다.
목적: 보통, Panel에 보턴을 추가하는데 사용한다.
Panel의 디폴트 배치 관리자이다.
import java.awt.*;
class FlowLayoutTest extends Frame {
public FlowLayoutTest()
{
setLayout(new FlowLayout());
add(new Button("보턴 1"));
add(new Button("보턴 2"));
add(new MyButton("새로운 보턴"));
Panel p = new Panel();
p.add(new Button("패널 보턴 1"));
[email protected]
FlowLayout
p.add(new Button("패널 보턴 2"));
add(p);
}
public static void main(String[] args) {
Frame f1 = new FlowLayoutTest();
f1.setTitle("FlowLayout: 디폴트");
f1.setBounds(0, 0, 200, 300);
f1.setVisible(true);
Frame f2 = new FlowLayoutTest();
((FlowLayout) f2.getLayout()).setAlignment(FlowLayout.LEFT);
f2.setTitle("FlowLayout: 왼쪽 정렬");
f2.setBounds(200, 0, 200, 300);
f2.setVisible(true);
}
}
class MyButton extends Button {
public MyButton(String text) {
super(text);
}
public Dimension getPrefferedSize() {
return new Dimension(super.getPreferredSize().width + 20,
super.getPreferredSize().height + 20 );
}
}
[email protected]
BorderLayout



동,서,남,북,중앙에 배치
chlid component가 현재의 Container를 채우기 그 크기가
자동적으로 늘거나 줄어든다.
component가 5개 미만이면, 나머지 component가 여백을
채운다.
[email protected]
BorderLayout Example
import java.awt.*;
class BorderLayoutTest
{
public static void main(String[] args)
{
Frame f = new Frame();
f.add(new Button("Center Button"), "Center");
f.add(new Button("West Button"), "West");
f.add(new Button("East Button"), "East");
f.add(new Button("North Button"), "North");
f.add(new Button("South Button"), "South");
f.setSize(500, 500); f.setVisible(true);
}
}
[email protected]
GridLayout

컴포넌트를 동일한 크기로 배치
[email protected]
GridLayout Example
import java.awt.*;
class GridLayoutTest extends Frame
{
public GridLayoutTest(String title, LayoutManager mgr, int x, int y)
{
setTitle("GridLayout test: " + title);
setLayout(mgr);
add(new Button("보턴 1")); add(new Button("보턴 2"));
add(new Button("보턴 3")); add(new Button("보턴 4"));
add(new Button("보턴 5")); add(new Button("보턴 6"));
add(new Button("보턴 7"));
setBounds(x, y, 300, 300); setVisible(true);
}
public static void main(String[] args)
{
new GridLayoutTest("디폴트", new GridLayout(), 0, 0);
new GridLayoutTest("2, 0", new GridLayout(2,0), 300, 0);
new GridLayoutTest("0, 3", new GridLayout(0,2), 0, 300);
new GridLayoutTest("3, 3", new GridLayout(3,3), 300, 300);
}
}
[email protected]
GridBagLayout

component를 다른 크기로 배치

GridBagConstraints





int gridx , gridy  relative ( 0, 1, 2, 3… )
int anchor : CENTER, EAST, WEST, NORTH, SOUTH…
int fill : NONE, HORIZONTAL, VERTICAL, BOTH
double weightx = 0 , weighty = 0
add(Components, GridBagConstraints)를 사용하는 것
이 일반적
[email protected]
GridBagLayout Example
import java.awt.*;
class GridBagLayoutTest
{
public static void main(String[] args)
{
Frame f = new Frame(“GridBagLayout test”);
GridBagLayout gbl=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
f.setLayout(gb1);
f.add(new Label(“JDK 1.1.5:”),gbc);
f.add(new TextField(“http://java.sun.com/products/jdk/1.1/”), gbc);
gbc.gridy=1;
f.add(new Label(“News:”), gbc);
f.add(new TextField(“comp.lang.java.programmer”),gbc);
[email protected]
GridBagLayout Example
gbc.gridy = 2;
gbc.anchor=GridBagConstraints.EAST;
f.add(new Label(“News: “), gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
f.add(new TextField(“comp.lang.java.announce “), gbc );
gbc.gridx = 2 ; gbc.gridy = 3 ;
gbc.fill = GridBagConstraints.BOTH;
gbx.weightx = 1 ;
f.add(new Button(“test”), gbc ) ;
f.setSize( 400, 300 ) ;
f.setVisible(true);
}
}
[email protected]
Next

Event 관련 class



Event_handler
Event_listener
Codes..
[email protected]