Professional Documents
Culture Documents
Chap 08
Chap 08
เนื้อหาในบทนี้จะกลาวถึงพื้นฐานในการสรางสวนที่ใชในการติดตอระหวางผูใชดวยแพ็คเกจ AWT
รูปแสดงสวนประกอบแพ็คเกจ AWT
AWTEvent
Component
Object Color
Font
Graphics
phanlapha@yahoo.com 1/33
Graphic User Interface & Event Handler Modern Programming Languages
รูปแสดงสวนประกอบคลาส Component
Button
Object
Canvas
Checkbox Dialog
Component
Window
Choice
Frame
Container
Label
List
TextArea
Scrollbar
TextComponent TextField
8.1.1.1 Container
Container เปนคลาสที่ใชวางออบเจกตของคอมโพเนนตกราฟฟก โดยที่คลาส Container อยูใน
แพ็คเกจ AWT ประกอบไปดวย Frame, Dialog, Panel และ Applet เปนตน
เมธอดที่สําคัญคือ
• public void add(Component c)
• public void add(Component c, int index)
โดยที่
• c เปนออบเจกตของคอมโพเนนตกราฟฟกที่ตองการวาง
• index เปนตําแหนงที่ใชในการจัดวางกราฟฟก ตามรูปแบบการจัดวางตําแหนงคอมโพเน
นตกราฟฟก (LayoutManager)
phanlapha@yahoo.com 2/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
phanlapha@yahoo.com 3/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 4/33
Graphic User Interface & Event Handler Modern Programming Languages
Panel เปนคลาสประเภท Container เชนเดียวกับคลาส Frame แตกตางกันคือ Panel ไมมี title bar
และ resizable
• Panel มี Subclass คือ Applet เพื่อใชในการเขียนโปรแกรม Applet ชวยใหสามารถวางออบเจกต
graphic component ได
• จาวา Appication ไมสามารถเริ่มตนดวยการสรางออบเจกต Panel ไดโดยตรง ตองสรางออบเจกต
Frame กอน
• Panel สามารถวางลงไปในออบเจกต Frame สามารถสราง GUI ที่มีการจัดวาง Layout ที่ซับซอน
ได
• คอนสตรัคเตอร ของคลาส Panel ที่สําคัญมีดังนี้
o public Panel()
o public Panel(LayoutManget layout)
phanlapha@yahoo.com 5/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
Panel
phanlapha@yahoo.com 6/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 7/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
phanlapha@yahoo.com 8/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
phanlapha@yahoo.com 9/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
phanlapha@yahoo.com 10/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
TextField เปนคลาสที่ใชในการสรางออบเจกตเพื่อใหผูใชสามารถพิมพขอความไดทีละบรรทัด
• คอนสตรัคเตอร ของคลาส TextField มีดังนี้
o public TextField ()
o public TextField (int column)
o public TextField (String str)
o public TextField (String str, int column)
โดยที่
• column เปนจํานวนคอลัมนที่ตองการแสดง
• str เปนขอความเริ่มตนที่ตองการแสดง
• เมธอดที่สําคัญ มีดังนี้
o public String getText()
เปนเมธอดที่สงขอความของออบเจกต TextField กลับมา
o public void setText(String str)
เปนเมธอดที่ใชกําหนดหรือเปลี่ยนแปลงขอความของออบเจกต TextField
o public void setEditable(boolean b)
เปนเมธอดที่ใชกําหนดสถานะให TextField อานไดอยางเดียว ไมสามารถแกไขได
o public boolean isEditable()
เปนเมธอดที่ใชตรวจสอบสถานะ TextField วาอานไดอยางเดียว (false) หรือ
สามารถแกไขได (true)
phanlapha@yahoo.com 11/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
TextArea เปนคลาสที่ใชในการสรางออบเจกตเพื่อใหผูใชสามารถพิมพขอความไดทีละหลายบรรทัด
• คอนสตรัคเตอร ของคลาส TextArea มีดังนี้
o public TextArea()
o public TextArea(int row, int col)
o public TextArea(String str)
o public TextArea(String str, int row, int col)
o public TextArea(String str , int row, int col, int scrollBar)
โดยที่
• row และ col เปนจํานวนแถวและคอลัมนที่ตองการแสดงใน TextArea
• str เปนขอความเริ่มตนที่ตองการแสดง
• scrollBar กําหนดการมี scrollBar ของ TextArea
SCROLLBARS_BOTH (0)
SCROLLBARS_VERTICAL_ONLY (1)
SCROLLBARS_HORIZONTAL_ONLY (2)
SCROLLBARS_NONE (3)
• เมธอดที่สําคัญสวนใหญ มีเหมือนกับ TextFiled เพราะตางก็เปน คลาสลูกที่สืบทอดคุณสมบัติ
TextComponent
phanlapha@yahoo.com 12/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
• FlowLayout
• BorderLayout
• GridLayout
• CardLayout
• GridBagLayout
phanlapha@yahoo.com 13/33
Graphic User Interface & Event Handler Modern Programming Languages
ตัวอยาง
8.1.2.1FlowLayout
เปนการจัดวางคอมโพเนนตกราฟฟกไวตําแนงบนสุดของออบเจกต Container และเรียงตําแหนงจากซายไป
ขวา ถาขนาดความกวางแตละแถวของออบเจกต Container ไมพอ จะนําออบเจกต graphic component ที่
เหลือวางในตําแหนงของแถวถัดไป
ผลลัพธที่ได
phanlapha@yahoo.com 14/33
Graphic User Interface & Event Handler Modern Programming Languages
8.1.2.2BorderLayout
กําหนดการจัดวางตําแหนงคอมโพเนนตกราฟฟก ไวทั้งหมด 5 ตําแนง คือ CENTER, NORTH,
SOUTH, EAST และ WEST การระบุตําแหนงทําไดโดยเรียกใชเมธอด add() เชน
หากไมระบุตําแหนงจะถูกวางไวตรงกลาง
ตัวอยาง การจัดวางคอมโพเนนตกราฟฟกแบบ BorderLayout
ผลลัพธที่ได
phanlapha@yahoo.com 15/33
Graphic User Interface & Event Handler Modern Programming Languages
8.1.2.3GridLayout
เปนการจัดวางคอมโพเนนตกราฟฟก ตามขนาดของแถวและคอลัมนที่กําหนด การระบุตําแหนงทําไดสราง
ออบเจกต GridLayout โดยมีรูปแบบของ คอนสตรัคเตอร ที่สําคัญดังนี้
public GridLayout()
public GridLayout(int row, int col)
ผลลัพธที่ได
phanlapha@yahoo.com 16/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 17/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
Event
Event Source Event Handler
phanlapha@yahoo.com 18/33
Graphic User Interface & Event Handler Modern Programming Languages
รูปแสดงสวนประกอบคลาส AWTEvent
ActionEvent ContainerEvent
AdjustmentEvent FocusEvent
KeyEvent
AWTEvent ComponentEvent InputEvent
ItemEvent MouseEvent
WindowEvent
TextEvent
phanlapha@yahoo.com 19/33
Graphic User Interface & Event Handler Modern Programming Languages
dragged เปนเหตุการณที่เกิดขึ้นเมื่อมีการกดปุมเมาสคางพรอมกับการเลื่อนเมาส
entered เปนเหตุการณที่เกิดขึ้นเมื่อมีการเลื่อนเมาสเขาไปในขอบเขตของออบเจกต
ของคลาสที่เปนสวนประกอบคอมโพเนนตกราฟฟกใด ๆ
exited เปนเหตุการณที่เกิดขึ้นเมื่อมีตําแหนงของเมาสอยูนอกขอบเขตของออบเจกต
ของคลาสที่เปนสวนประกอบคอมโพเนนตกราฟฟกใด ๆ
pressed เปนเหตุการณที่เกิดขึ้นเมื่อมีการกดปุมบนเมาส
released เปนเหตุการณที่เกิดขึ้นเมื่อมีการปลอยปุมที่กดบนเมาส
เมธอดที่สําคัญมีดังนี้
int getX()
เปนเมธอดที่จะสงตําแหนงพิกัดของเมาสบนแกน X คืนมา มีชนิดขอมูลเปน int
int getY()
เปนเมธอดที่จะสงตําแหนงพิกัดของเมาสบนแกน Y คืนมา มีชนิดขอมูลเปน int
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
EventListener
KeyListener
MouseListener
MouseMotionListener
TextListener
WindowListener
phanlapha@yahoo.com 20/33
Graphic User Interface & Event Handler Modern Programming Languages
8.2.3 การจัดการกับเหตุการณ
• ออบเจกตของ component เปนออบเจกตประเภท Event Source เชน Button เปน Event Source
ของ ActionEvent
• ออบเจกตของ component รับฟงเหตุการณใด ตอง implements อินเตอรเฟสประเภท Listener ให
สอดคลองกัน เชน Button ตอง implements อินเตอรเฟส ActionListener
• Event Source ใดที่ตองการจัดการกับเหตุการณที่เกิดขึ้น และตองระบุใหรูวา Event Source รอรับฟง
เหตุการณที่จะเกิดขึ้น มีรูปแบบดังนี้
eventSource.add[ประเภท Listener]Listener(listener);
โดยที่
• eventSource เปนชื่อออบเจกตที่ทําใหเกิดเหตุการณ
• add[ประเภท Listener]Listener เปนเมธอดที่ลงทะเบียนรอรับฟงเหตุการณนั้น ๆ โดย
ที่จะตองเลือกใหสอดคลองกับอินเตอรเฟสประเภท Listener
• listener เปนออบเจกตของคลาส [ประเภท Listener]Listener ซึ่งสามารถรับฟง
เหตุการณที่ตองการจัดการได
อินเตอรเฟส เมธอดที่รับฟงเหตุการณ
ActionListener addActionListener()
KeyListener addKeyListener()
ItemListener addItemListener()
MouseListener addMouseListener()
MouseMotionListener addMouseMotionListener()
WindowListener addWindowActionListener()
phanlapha@yahoo.com 21/33
Graphic User Interface & Event Handler Modern Programming Languages
ตัวอยาง การจัดการเหตุการณเมื่อเกิดเหตุการณกับปุม(Button)และชอยส(Choice)
phanlapha@yahoo.com 22/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
select choice Æ
phanlapha@yahoo.com 23/33
Graphic User Interface & Event Handler Modern Programming Languages
ตัวอยาง การจัดการเหตุการณหลายเหตุการณ
phanlapha@yahoo.com 24/33
Graphic User Interface & Event Handler Modern Programming Languages
ตัวอยาง การจัดการเหตุการณหลายๆแบบ
ในโปรแกรม EventAdapter พิจารณาการจัดการเหตุการณเมื่อเกิดการปดหนางตาง แบงเปน 3 แบบ
โดยทําการพิจารณาไปทีละแบบ ดังนี้
• แบบที่ 1
• ทําการสรางคลาสชื่อ CodeToHandleWinClose ซึ่งเปนคลาสที่ใชจัดการเหตุการณเมื่อปด
หนาตาง โดยทําการ implements อินเตอรเฟส WindowListener
• ในบรรทั ด ที่ 27 สรางออบเจกตที่ทํา หนา ที่จัด การกับ เหตุก ารณที่เกิดขึ้น จาก
WindowListener ชื่อออบเจกต m
• ในบรรทัดที่ 28 แสดงคําสั่งใหออบเจกตของ Frame รอรับฟงเหตุการณประเภท
WindowListener ดังนี้ addWindowListener(m);
• แบบที่ 2
• ทําการสรางคลาสชื่อ Detect ซึ่งเปนคลาสที่ใชจัดการเหตุการณเมื่อปดหนาตาง โดยทําการสืบ
ทอดคุณสมบัติจากคลาส WindowAdapter
• ในบรรทัดที่ 20 แสดงคําสั่งใหออบเจกตของ Frame รอรับฟงเหตุการณประเภท
WindowListener ดังนี้ addWindowListener(new Detect()); โดยที่ new
Detect() เปนการสรางออบเจกตที่ทําหนาที่จัดการกับเหตุการณที่เกิดขึ้นจาก
WindowListener
• แบบที่ 3
• ระหวางบรรทัดที่ 22-26 แสดงคําสั่งใหออบเจกตของ Frame รอรับฟงเหตุการณประเภท
WindowListener ดังนี้
• addWindowListener([การสรางคลาสประเภท Event Hadler]);
phanlapha@yahoo.com 25/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 26/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 27/33
Graphic User Interface & Event Handler Modern Programming Languages
ผลลัพธที่ได
phanlapha@yahoo.com 28/33
Graphic User Interface & Event Handler Modern Programming Languages
แบบฝกหัด
1. สรางโปรแกรม VatCalculation.java เพื่อใชคํานวณราคาสินคารวม VAT
• เมื่อรันโปรแกรมไดผลดังรูป
phanlapha@yahoo.com 29/33
Graphic User Interface & Event Handler Modern Programming Languages
• เมื่อกดปุม จบการทํางานโปรแกรม
phanlapha@yahoo.com 30/33
Graphic User Interface & Event Handler Modern Programming Languages
phanlapha@yahoo.com 31/33
Graphic User Interface & Event Handler Modern Programming Languages
มีเงื่อนไขดังนี้
1. ใหรับคาน้ําหนักเปนกิโลกรัม
2. ใหรับคาสวนสูงเปนเซ็นติเมตร
2. แสดงผลคามวลของรางกายกําหนดใหแสดงจุดทศนิยม 2 ตําแหนง
3. คามวลมากกวาหรือเทากับ 24 อวนเกินไป แสดงขอความ You are overweight.
4. คามวลมากวา 20 แตนอยกวา 24 ปกติ แสดงขอความ You are normal.
5. คามวลนอยกวาหนือเทากับ 20 ผอมเกินไป แสดงขอความ You are skinny.
ยกตัวอยางในการออกแบบ
- TextField ที่ใชในการรับคาสวนสูงและน้ําหนักควรรับคาไดเปนตัวเลข
- คาน้ําหนักและสวนสูงตองไมเปนศูนย
phanlapha@yahoo.com 32/33
Graphic User Interface & Event Handler Modern Programming Languages
java.awt.event
Class ActionEvent
Method Summary
String getActionCommand()
Returns the command string associated with this action.
Methods inherited from class java.util.EventObject
getSource
getSource
public Object getSource()The object on which the Event initially occurred.
Returns:
The object on which the Event initially occurred.
Class KeyEvent
Method Summary
char getKeyChar()
Returns the character associated with the key in this event.
int getKeyCode()
Returns the integer keyCode associated with the key in this event.
Methods inherited from class java.util.EventObject
getSource
Class ItemEvent
Method Summary
Object getItem()
Returns the item affected by the event.
Methods inherited from class java.util.EventObject
getSource
Class MouseEvent
Method Summary
Point getPoint()
Returns the x,y position of the event relative to the source component.
int getX()
Returns the horizontal x position of the event relative to the source component.
int getY()
Returns the vertical y position of the event relative to the source component.
Methods inherited from class java.util.EventObject
getSource
phanlapha@yahoo.com 33/33