SlideShare a Scribd company logo
1 of 17
Download to read offline
การจัดการกับอีเวนต
Event handling
วัตถุประสงค
♦ เพื่อใหนิสิตไดเรียนรูวิธีการเรียกใชงาน method ที่เกี่ยวกับ java application
♦ เพื่อใหไดนําหลักการเขียนโปรแกรมเชิงวัตถุประยุกตในการเขียนโปรแกรมแบบกราฟฟกได
บทที่
13
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
301
เนื้อหาบทเรียน
♦ Event Handling
♦ Interface ActionListener
♦ Button , Events and Others Swing basics
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
302
Event Handling
คือ การจัดการกับเหตุการณ หรืออีเวนต(event) ที่เกิดขึ้นในโปรแกรม เชน การลากเมาสผาน
ลาเบล การคลิกเมาส การดับเบิลคลิกเมาส หรือกดปุมบนคียบอรดเปนตน ในบทที่ 12 ไดเรียนรูแตการ
สรางหนาจอแตยังไมมีการจัดการกับเหตุการณตางๆที่เกิดขึ้นในโปรแกรม ซึ่งเมื่อเกิดการกระทําใดๆ
กับโปรแกรมก็ตาม โปรแกรมจะไมมีการตอบสนองใดๆ ทั้งสิ้น ในบทนี้เราจะศึกษาถึงวิธีการจัดการกับ
เหตุกาณตางๆ เหลานั้น โดยศึกษาจากตัวอยางโปรแกรมตางๆ ในบทนี้
รูปภาพแสดงเหตุการณ(event) ที่กระทําโดย component ตางๆ ถูกจัดการดวยออบเจ็คที่ชื่อ listener
Interface ActionListener
เปน interface ที่ทําการดักจับเหตุการณตางๆ ดังนั้นหากตองการใชงาน ตองทําการ implement
และ override method ตางๆ ที่อยูภายใน interface นี้
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
303
ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.BorderLayout;
import java.awt.event.ActionListener;
//ออบเจ็ค listener จะกระทําการใดที่ตอบสนองตอ event ที่เกิดขึ้น
import java.awt.event.ActionEvent;
// ออบเจ็ค event เปนออบเจ็คที่สงสัญญาณ(listener)ไปยังออบเจ็คอื่น
import java.awt.Color;
import java.awt.Container;
public class ColoredWindow implements ActionListener{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
JFrame colorFrame ;
Container contentPane;
JTextArea txt;
JButton btn;
public static void main(String args[]) {
ColoredWindow cw = new ColoredWindow();
cw.init();
}
void init(){
colorFrame = new JFrame("Color Frame");
contentPane = colorFrame.getContentPane();
colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane.setLayout(new BorderLayout());
txt = new JTextArea(5,20);
btn = new JButton("Click");
colorFrame.setSize(WIDTH,HEIGHT);
contentPane.add(txt,BorderLayout.CENTER);
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
304
contentPane.add(btn,BorderLayout.SOUTH);
btn.addActionListener(this);
colorFrame.show();
}
public void actionPerformed(ActionEvent e){// override method จาก interface ActionListener
txt.setBackground(Color.YELLOW);
//contentPane. setBackground(Color.YELLOW);
}
}
ผลลัพธที่ไดจะปรากฎหนาจอ Window สวน textArea พื้นสีขาวแลวคลิกปุม Click พื้นหลังของ
textArea จะกลายเปนสีเหลือง ทําการการปดโดยคลิกปุม ปด(x)
ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class FirstSwingDemo1 implements ActionListener{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
public static void main(String args[]){
FirstSwingDemo1 f = new FirstSwingDemo1();
f.init(); }
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
305
void init(){
JFrame firstWindow = new JFrame("First Frame");
firstWindow.setSize(WIDTH , HEIGHT);
Container cn = new Container();
cn = firstWindow.getContentPane();
JButton endButton = new JButton("Click to end program");
cn.add(endButton);
endButton.addActionListener(this);
firstWindow.setVisible(true);
firstWindow.show();
}
public void actionPerformed(ActionEvent e){
System.out.println("test test");
System.exit(0);
}
}
ผลลัพธที่ได แสดงหนาจอดังนี้ เมื่อคลิกปุม Click to end program ก็จะแสดงขอความ test test ออกบน
หนาจอ out put แลวออกจากโปรแกรม
ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ ประกอบกับการ
จัดการกับ event ดวย Action Listeners และ Action Events
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JPanel;
import java.awt.Container;
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
306
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class testPanelEvent implements ActionListener{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JFrame jfrm;
private Container contentPane;
private JButton btn1 , btn2 , btn3;
private JPanel pan , panColor ;
private JPanel pnMagenta, pnCyan , pnOrange;
public static void main(String args[]){
testPanelEvent blf = new testPanelEvent();
blf.init();
}
void init(){
jfrm = new JFrame("test grid layout");
jfrm.setSize(WIDTH,HEIGHT);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = jfrm.getContentPane();
contentPane.setLayout(new BorderLayout());
pan = new JPanel();
pnMagenta = new JPanel();
pnCyan = new JPanel();
pnOrange = new JPanel();
panColor = new JPanel();
pan.setLayout(new GridLayout(1,3));
panColor.setLayout(new GridLayout(1,3));
btn1 = new JButton("Magenta");
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
307
pan.add(btn1);
btn1.addActionListener(this);
btn2 = new JButton("Cyan");
pan.add(btn2);
btn2.addActionListener(this);
btn3 = new JButton("Orange");
pan.add(btn3);
pnMagenta.setBackground(Color.MAGENTA);
pnCyan.setBackground(Color.CYAN);
pnOrange.setBackground(Color.ORANGE);
panColor.add(pnMagenta);
panColor.add(pnCyan);
panColor.add(pnOrange);
contentPane.add(panColor , BorderLayout.CENTER);
contentPane.add(pan , BorderLayout.SOUTH);
contentPane.setVisible(true);
jfrm.show();
}
public void actionPerformed(ActionEvent e){
String buttonString = e.getActionCommand();
if(buttonString.equals("Magenta")){
System.out.println("Magenta click");
}
else if (buttonString.equals("Cyan")){
System.out.println("Cyan Click");
}
else if (buttonString.equals("Orange ")){
System.out.println("Orange Click");
}
}
}
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
308
ผลลัพธที่ไดแสดงดังภาพ เมื่อคลิก ปุม Magenta แสดงขอความ “Magenta click” บนหนาจอ
output และถาคลิกปุม Cyan แสดงขอความ “Cyan click” และถาคลิกปุม Orange จะแสดงขอความ
“Orange Click” บนหนาจอ output เชนกัน
ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ ประกอบกับการ
จัดการกับ event ดวย Action Listeners และ Action Events
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.Color;
public class testMenuBar implements ActionListener{
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
private JFrame jfrm;
private Container contentPane;
private JPanel pan , panColor ;
private JPanel pnMagenta, pnCyan , pnOrange;
private JMenu colorMenu;
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
309
private JMenuItem pinkChoice , yellowChoice ,orangeChoice ;
private JMenuBar bar;
public static void main(String args[]){
testMenuBar blf = new testMenuBar();
blf.init(); }
void init(){
jfrm = new JFrame("test grid layout");
jfrm.setSize(WIDTH,HEIGHT);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane = jfrm.getContentPane();
contentPane.setLayout(new BorderLayout());
pnMagenta = new JPanel();
pnCyan = new JPanel();
pnOrange = new JPanel();
panColor = new JPanel();
colorMenu = new JMenu("Add color");
panColor.setLayout(new GridLayout(1,3));
pinkChoice = new JMenuItem("Pink");
colorMenu.add(pinkChoice);
pinkChoice.addActionListener(this);
yellowChoice = new JMenuItem("Cyan");
colorMenu.add(yellowChoice);
yellowChoice.addActionListener(this);
orangeChoice = new JMenuItem("Orange");
colorMenu.add(orangeChoice);
orangeChoice.addActionListener(this);
bar = new JMenuBar();
bar.add(colorMenu);
jfrm.setJMenuBar(bar);
panColor.add(pnMagenta);
panColor.add(pnCyan);
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
310
panColor.add(pnOrange);
contentPane.add(panColor , BorderLayout.CENTER);
contentPane.setVisible(true);
jfrm.show();
}
public void actionPerformed(ActionEvent e){
String buttonString = e.getActionCommand();
if(buttonString.equals("Pink")){
pnMagenta.setBackground(Color.PINK);
}
else if (buttonString.equals("Cyan")){
pnCyan.setBackground(Color.CYAN);
}
else if (buttonString.equals("Orange")){
pnOrange.setBackground(Color.ORANGE);
}
}
}
ผลลัพธที่ได
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
311
ตัวอยางโปรแกรมที่ประกอบดวย Text Fields and Text Areas และ Swing basics อื่นๆ ประกอบกับการ
จัดการกับ event ดวย Action Listeners และ Action Events
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class testText implements ActionListener{
public static final int WIDTH = 300; public static final int HEIGHT = 200;
public static final int NUMBER_OF_CHAR = 20;
private JFrame jfrm;
private JPanel panel , buttonPanel;
private Container contentPane;
private JTextField fnametxt ;
private JLabel fnamelbl;
private JButton exitBtn , printBtn;
public static void main(String args[]){
testText blf = new testText();
blf.init();
}
void init(){
jfrm = new JFrame("Test Text Field");
jfrm.setSize(WIDTH,HEIGHT);
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
312
contentPane = jfrm.getContentPane();
contentPane.setLayout(new BorderLayout());
panel = new JPanel();
panel.setLayout(new GridLayout(1,2));
buttonPanel = new JPanel();
buttonPanel.setLayout(new FlowLayout());
fnametxt = new JTextField (NUMBER_OF_CHAR);
fnamelbl = new JLabel("Name");
fnametxt.addActionListener(this);
panel.add(fnamelbl);
panel.add(fnametxt);
exitBtn = new JButton("Exit");
exitBtn.addActionListener(this);
printBtn = new JButton("Print");
printBtn.addActionListener(this);
buttonPanel.add(printBtn);
buttonPanel.add(exitBtn);
contentPane.add(panel , BorderLayout.CENTER);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
contentPane.setVisible(true);
jfrm.pack();
jfrm.show();
}
public void actionPerformed(ActionEvent e){
System.out.println("Hello !!!" + fnametxt.getText());
}
}
ผลลัพธที่ได
เมื่อคลิกปุม print จะแสดงขอความ Hello!!! Surangkana เมื่อคลิกปุม Exit ออกจากโปรแกรม
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
313
แบบฝกหัด
1) จากแบบฝกหัดในบทที่ 12 ขอที่ 1 ใหทําการสรางหนาจอดังนี้
เขียนโปรแกรมทําการดักจับเหตุการณที่จะเกิดกับปุม “Magenta” คือถาคลิกปุมนี้แลวแถบดานบนจะ
ปรากฎสีมวง ถาคลิกปุม “Cyan” แลวจะปรากฎสีฟาบนพื้นที่ดานบนของปุม และถาคลิกปุม “Orange”
จะปรากฎสีสมดานบนของปุม เมื่อคลิกไอคอน X ทําการปดโปรแกรม
2 ) ใหนิสิตสรางโปรแกรมเพื่อทําการรับคา First name และ Last name เมื่อทําการคลิกปุม Print แสดง
ขอความ “Hello!! ชื่อ นามสกุล” คลิกปุม Clear ทําการลบขอความใน textbox ทั้งหมด กดปุน Exit
ออกจากโปรแกรม ดังตัวอยางตอไปนี้
3) จากตัวอยางบทที่ 12 คลาส JTextFieldArea แสดงหนาจอดังนี้
CCLLIICCKK
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
314
ใหนิสิตกําหนด action ใหกับปุมตางๆ ดังนี้
- ปุม Total ทําการ นําขอความใน Text 1 เชื่อมกับขอความใน Text 2 แสดงในชอง Total
- ปุม Clear ทําใหชองขอความทุกชองในหนาจอเปนชองวาง
- ปุม Exit เมื่อคลิกแลวทําใหปดโปรแกรม
4) สรางคลาสชื่อ FirstGUI แลวทําการเพิ่มปุมการทํางานใน JFrame เมื่อคลิกปุมดังกลาวจะปรากฎ
ขอความ “Modified First Window”. ในสวน title bar ของ window หากคลิกปุมนี้อีกครั้ง
ขอความจะเปลี่ยนเปน “Modified Again” และหากคลิกอีกครั้งจะกลับเปนขอความ “Modified
First Window” สลับกัน
5) สราง Window เพื่อทําการรับคา User name และ Password เมื่อคลิกปุม login จะปรากฎ messagebox
แสดงขอความ “Account successfully create” แลวตามดวย user และ password ที่รับคาไป แสดง
ดังรูป
6) ใหนิสิตเขียนโปรแกรมเพื่อคํานวณการรักษาฟน ประกอบดวย 3 รายการ ดังหนาจอตอไปนี้
JCheckBox
components
(unchecked)
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
315
หากไมไดกรอกชื่อหรือไมเลือกวิธีการรักษา แลวคลิกปุม calculate จะปรากฎหนาจอ เตือนดังรูป
หากคลิกปุม Ok จะเขามายังหนาจอเดิม ใหกรอกชื่อและทําการเลือกวิธีรักษา เมื่อคลิกปุม Calculate
จะแสดงผลรวมของการรักษาดังรูป
แตหากไมกรอกชื่อ Patient Name เมื่อคลิกปุม Calculate จะคํานวณ แลวแสดง message box ดังรูป
Message dialog Click OK JButton to close the
dialog
JCheckBox
components
(checked)
Close button
Dialog sized to
accommodate contents
Title bar
Icon indicates the tone
of the message
OK JButton allows user to
close the dialog
บทที่ 13 การจัดการกับอีเวนต หนาที่
เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง
อ.สุรางคนา ระวังยศ
316
7) ใหนิสิตเขียนโปรแกรมดังหนาจอตอไปนี้
- เมื่อเลือกประเทศ ที่เปนคําตอบจาก JComboBox
- สงคําตอบที่ถูกโดยคลิกปุม Submit จะแสดงขอความ Correct ใน JLabel ดานลาง
- เมื่อคลิกปุม Next Flag
JComboBox contains
answers (country names)
JLabel แสดงรูปธง
คําตอบที่ถูกเลือก
Submit JButton
สามารถกลับมาใชไดใหม
JComboBox ของ
dropdownlistbox
เมื่อตอบถูกแสดงขอความ Correct!
JTextField ถูก clear

More Related Content

More from Theeravaj Tum

Javacentrix com chap11-2
Javacentrix com chap11-2Javacentrix com chap11-2
Javacentrix com chap11-2Theeravaj Tum
 
Javacentrix com chap11-1
Javacentrix com chap11-1Javacentrix com chap11-1
Javacentrix com chap11-1Theeravaj Tum
 
Javacentrix com chap10-0
Javacentrix com chap10-0Javacentrix com chap10-0
Javacentrix com chap10-0Theeravaj Tum
 
Javacentrix com chap09-0
Javacentrix com chap09-0Javacentrix com chap09-0
Javacentrix com chap09-0Theeravaj Tum
 
Javacentrix com chap08-0
Javacentrix com chap08-0Javacentrix com chap08-0
Javacentrix com chap08-0Theeravaj Tum
 
Javacentrix com chap07-0
Javacentrix com chap07-0Javacentrix com chap07-0
Javacentrix com chap07-0Theeravaj Tum
 
Javacentrix com chap06-0
Javacentrix com chap06-0Javacentrix com chap06-0
Javacentrix com chap06-0Theeravaj Tum
 
Javacentrix com chap04-0
Javacentrix com chap04-0Javacentrix com chap04-0
Javacentrix com chap04-0Theeravaj Tum
 
Javacentrix com chap03-0
Javacentrix com chap03-0Javacentrix com chap03-0
Javacentrix com chap03-0Theeravaj Tum
 
Javacentrix com chap02-0
Javacentrix com chap02-0Javacentrix com chap02-0
Javacentrix com chap02-0Theeravaj Tum
 
Javacentrix com chap01-0
Javacentrix com chap01-0Javacentrix com chap01-0
Javacentrix com chap01-0Theeravaj Tum
 
Javacentrix com chap05-0
Javacentrix com chap05-0Javacentrix com chap05-0
Javacentrix com chap05-0Theeravaj Tum
 
บทที่ 11 การดักจับข
บทที่ 11 การดักจับขบทที่ 11 การดักจับข
บทที่ 11 การดักจับขTheeravaj Tum
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริTheeravaj Tum
 
บทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปบทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปTheeravaj Tum
 
บทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกบทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกTheeravaj Tum
 
บทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจบทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจTheeravaj Tum
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์Theeravaj Tum
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาสTheeravaj Tum
 
บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์Theeravaj Tum
 

More from Theeravaj Tum (20)

Javacentrix com chap11-2
Javacentrix com chap11-2Javacentrix com chap11-2
Javacentrix com chap11-2
 
Javacentrix com chap11-1
Javacentrix com chap11-1Javacentrix com chap11-1
Javacentrix com chap11-1
 
Javacentrix com chap10-0
Javacentrix com chap10-0Javacentrix com chap10-0
Javacentrix com chap10-0
 
Javacentrix com chap09-0
Javacentrix com chap09-0Javacentrix com chap09-0
Javacentrix com chap09-0
 
Javacentrix com chap08-0
Javacentrix com chap08-0Javacentrix com chap08-0
Javacentrix com chap08-0
 
Javacentrix com chap07-0
Javacentrix com chap07-0Javacentrix com chap07-0
Javacentrix com chap07-0
 
Javacentrix com chap06-0
Javacentrix com chap06-0Javacentrix com chap06-0
Javacentrix com chap06-0
 
Javacentrix com chap04-0
Javacentrix com chap04-0Javacentrix com chap04-0
Javacentrix com chap04-0
 
Javacentrix com chap03-0
Javacentrix com chap03-0Javacentrix com chap03-0
Javacentrix com chap03-0
 
Javacentrix com chap02-0
Javacentrix com chap02-0Javacentrix com chap02-0
Javacentrix com chap02-0
 
Javacentrix com chap01-0
Javacentrix com chap01-0Javacentrix com chap01-0
Javacentrix com chap01-0
 
Javacentrix com chap05-0
Javacentrix com chap05-0Javacentrix com chap05-0
Javacentrix com chap05-0
 
บทที่ 11 การดักจับข
บทที่ 11 การดักจับขบทที่ 11 การดักจับข
บทที่ 11 การดักจับข
 
บทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริบทที่ 10 ตัวแปรสตริ
บทที่ 10 ตัวแปรสตริ
 
บทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูปบทที่ 9 การพ้องรูป
บทที่ 9 การพ้องรูป
 
บทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติกบทที่ 8 คุณสมบัติก
บทที่ 8 คุณสมบัติก
 
บทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจบทที่ 7 แพ็คเกจ
บทที่ 7 แพ็คเกจ
 
บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์บทที่ 6 อาร์เรย์
บทที่ 6 อาร์เรย์
 
บทที่ 5 คลาส
บทที่ 5 คลาสบทที่ 5 คลาส
บทที่ 5 คลาส
 
บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์บทที่ 4 แอทริบิวท์
บทที่ 4 แอทริบิวท์
 

บทที่ 13 การดักจับเ

  • 1. การจัดการกับอีเวนต Event handling วัตถุประสงค ♦ เพื่อใหนิสิตไดเรียนรูวิธีการเรียกใชงาน method ที่เกี่ยวกับ java application ♦ เพื่อใหไดนําหลักการเขียนโปรแกรมเชิงวัตถุประยุกตในการเขียนโปรแกรมแบบกราฟฟกได บทที่ 13
  • 2. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 301 เนื้อหาบทเรียน ♦ Event Handling ♦ Interface ActionListener ♦ Button , Events and Others Swing basics
  • 3. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 302 Event Handling คือ การจัดการกับเหตุการณ หรืออีเวนต(event) ที่เกิดขึ้นในโปรแกรม เชน การลากเมาสผาน ลาเบล การคลิกเมาส การดับเบิลคลิกเมาส หรือกดปุมบนคียบอรดเปนตน ในบทที่ 12 ไดเรียนรูแตการ สรางหนาจอแตยังไมมีการจัดการกับเหตุการณตางๆที่เกิดขึ้นในโปรแกรม ซึ่งเมื่อเกิดการกระทําใดๆ กับโปรแกรมก็ตาม โปรแกรมจะไมมีการตอบสนองใดๆ ทั้งสิ้น ในบทนี้เราจะศึกษาถึงวิธีการจัดการกับ เหตุกาณตางๆ เหลานั้น โดยศึกษาจากตัวอยางโปรแกรมตางๆ ในบทนี้ รูปภาพแสดงเหตุการณ(event) ที่กระทําโดย component ตางๆ ถูกจัดการดวยออบเจ็คที่ชื่อ listener Interface ActionListener เปน interface ที่ทําการดักจับเหตุการณตางๆ ดังนั้นหากตองการใชงาน ตองทําการ implement และ override method ตางๆ ที่อยูภายใน interface นี้
  • 4. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 303 ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ import javax.swing.JFrame; import javax.swing.JTextArea; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.event.ActionListener; //ออบเจ็ค listener จะกระทําการใดที่ตอบสนองตอ event ที่เกิดขึ้น import java.awt.event.ActionEvent; // ออบเจ็ค event เปนออบเจ็คที่สงสัญญาณ(listener)ไปยังออบเจ็คอื่น import java.awt.Color; import java.awt.Container; public class ColoredWindow implements ActionListener{ public static final int WIDTH = 300; public static final int HEIGHT = 200; JFrame colorFrame ; Container contentPane; JTextArea txt; JButton btn; public static void main(String args[]) { ColoredWindow cw = new ColoredWindow(); cw.init(); } void init(){ colorFrame = new JFrame("Color Frame"); contentPane = colorFrame.getContentPane(); colorFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane.setLayout(new BorderLayout()); txt = new JTextArea(5,20); btn = new JButton("Click"); colorFrame.setSize(WIDTH,HEIGHT); contentPane.add(txt,BorderLayout.CENTER);
  • 5. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 304 contentPane.add(btn,BorderLayout.SOUTH); btn.addActionListener(this); colorFrame.show(); } public void actionPerformed(ActionEvent e){// override method จาก interface ActionListener txt.setBackground(Color.YELLOW); //contentPane. setBackground(Color.YELLOW); } } ผลลัพธที่ไดจะปรากฎหนาจอ Window สวน textArea พื้นสีขาวแลวคลิกปุม Click พื้นหลังของ textArea จะกลายเปนสีเหลือง ทําการการปดโดยคลิกปุม ปด(x) ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ import javax.swing.JFrame; import javax.swing.JButton; import java.awt.Container; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class FirstSwingDemo1 implements ActionListener{ public static final int WIDTH = 300; public static final int HEIGHT = 200; public static void main(String args[]){ FirstSwingDemo1 f = new FirstSwingDemo1(); f.init(); }
  • 6. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 305 void init(){ JFrame firstWindow = new JFrame("First Frame"); firstWindow.setSize(WIDTH , HEIGHT); Container cn = new Container(); cn = firstWindow.getContentPane(); JButton endButton = new JButton("Click to end program"); cn.add(endButton); endButton.addActionListener(this); firstWindow.setVisible(true); firstWindow.show(); } public void actionPerformed(ActionEvent e){ System.out.println("test test"); System.exit(0); } } ผลลัพธที่ได แสดงหนาจอดังนี้ เมื่อคลิกปุม Click to end program ก็จะแสดงขอความ test test ออกบน หนาจอ out put แลวออกจากโปรแกรม ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ ประกอบกับการ จัดการกับ event ดวย Action Listeners และ Action Events import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import java.awt.Container;
  • 7. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 306 import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class testPanelEvent implements ActionListener{ public static final int WIDTH = 300; public static final int HEIGHT = 200; private JFrame jfrm; private Container contentPane; private JButton btn1 , btn2 , btn3; private JPanel pan , panColor ; private JPanel pnMagenta, pnCyan , pnOrange; public static void main(String args[]){ testPanelEvent blf = new testPanelEvent(); blf.init(); } void init(){ jfrm = new JFrame("test grid layout"); jfrm.setSize(WIDTH,HEIGHT); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = jfrm.getContentPane(); contentPane.setLayout(new BorderLayout()); pan = new JPanel(); pnMagenta = new JPanel(); pnCyan = new JPanel(); pnOrange = new JPanel(); panColor = new JPanel(); pan.setLayout(new GridLayout(1,3)); panColor.setLayout(new GridLayout(1,3)); btn1 = new JButton("Magenta");
  • 8. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 307 pan.add(btn1); btn1.addActionListener(this); btn2 = new JButton("Cyan"); pan.add(btn2); btn2.addActionListener(this); btn3 = new JButton("Orange"); pan.add(btn3); pnMagenta.setBackground(Color.MAGENTA); pnCyan.setBackground(Color.CYAN); pnOrange.setBackground(Color.ORANGE); panColor.add(pnMagenta); panColor.add(pnCyan); panColor.add(pnOrange); contentPane.add(panColor , BorderLayout.CENTER); contentPane.add(pan , BorderLayout.SOUTH); contentPane.setVisible(true); jfrm.show(); } public void actionPerformed(ActionEvent e){ String buttonString = e.getActionCommand(); if(buttonString.equals("Magenta")){ System.out.println("Magenta click"); } else if (buttonString.equals("Cyan")){ System.out.println("Cyan Click"); } else if (buttonString.equals("Orange ")){ System.out.println("Orange Click"); } } }
  • 9. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 308 ผลลัพธที่ไดแสดงดังภาพ เมื่อคลิก ปุม Magenta แสดงขอความ “Magenta click” บนหนาจอ output และถาคลิกปุม Cyan แสดงขอความ “Cyan click” และถาคลิกปุม Orange จะแสดงขอความ “Orange Click” บนหนาจอ output เชนกัน ตัวอยางโปรแกรมที่ประกอบดวย Button , Events , TextArea และ Swing basics อื่นๆ ประกอบกับการ จัดการกับ event ดวย Action Listeners และ Action Events import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JMenu; import javax.swing.JMenuItem; import javax.swing.JMenuBar; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Container; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.Color; public class testMenuBar implements ActionListener{ public static final int WIDTH = 300; public static final int HEIGHT = 200; private JFrame jfrm; private Container contentPane; private JPanel pan , panColor ; private JPanel pnMagenta, pnCyan , pnOrange; private JMenu colorMenu;
  • 10. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 309 private JMenuItem pinkChoice , yellowChoice ,orangeChoice ; private JMenuBar bar; public static void main(String args[]){ testMenuBar blf = new testMenuBar(); blf.init(); } void init(){ jfrm = new JFrame("test grid layout"); jfrm.setSize(WIDTH,HEIGHT); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); contentPane = jfrm.getContentPane(); contentPane.setLayout(new BorderLayout()); pnMagenta = new JPanel(); pnCyan = new JPanel(); pnOrange = new JPanel(); panColor = new JPanel(); colorMenu = new JMenu("Add color"); panColor.setLayout(new GridLayout(1,3)); pinkChoice = new JMenuItem("Pink"); colorMenu.add(pinkChoice); pinkChoice.addActionListener(this); yellowChoice = new JMenuItem("Cyan"); colorMenu.add(yellowChoice); yellowChoice.addActionListener(this); orangeChoice = new JMenuItem("Orange"); colorMenu.add(orangeChoice); orangeChoice.addActionListener(this); bar = new JMenuBar(); bar.add(colorMenu); jfrm.setJMenuBar(bar); panColor.add(pnMagenta); panColor.add(pnCyan);
  • 11. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 310 panColor.add(pnOrange); contentPane.add(panColor , BorderLayout.CENTER); contentPane.setVisible(true); jfrm.show(); } public void actionPerformed(ActionEvent e){ String buttonString = e.getActionCommand(); if(buttonString.equals("Pink")){ pnMagenta.setBackground(Color.PINK); } else if (buttonString.equals("Cyan")){ pnCyan.setBackground(Color.CYAN); } else if (buttonString.equals("Orange")){ pnOrange.setBackground(Color.ORANGE); } } } ผลลัพธที่ได
  • 12. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 311 ตัวอยางโปรแกรมที่ประกอบดวย Text Fields and Text Areas และ Swing basics อื่นๆ ประกอบกับการ จัดการกับ event ดวย Action Listeners และ Action Events import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.JLabel; import javax.swing.JButton; import java.awt.Container; import java.awt.GridLayout; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Color; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class testText implements ActionListener{ public static final int WIDTH = 300; public static final int HEIGHT = 200; public static final int NUMBER_OF_CHAR = 20; private JFrame jfrm; private JPanel panel , buttonPanel; private Container contentPane; private JTextField fnametxt ; private JLabel fnamelbl; private JButton exitBtn , printBtn; public static void main(String args[]){ testText blf = new testText(); blf.init(); } void init(){ jfrm = new JFrame("Test Text Field"); jfrm.setSize(WIDTH,HEIGHT); jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  • 13. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 312 contentPane = jfrm.getContentPane(); contentPane.setLayout(new BorderLayout()); panel = new JPanel(); panel.setLayout(new GridLayout(1,2)); buttonPanel = new JPanel(); buttonPanel.setLayout(new FlowLayout()); fnametxt = new JTextField (NUMBER_OF_CHAR); fnamelbl = new JLabel("Name"); fnametxt.addActionListener(this); panel.add(fnamelbl); panel.add(fnametxt); exitBtn = new JButton("Exit"); exitBtn.addActionListener(this); printBtn = new JButton("Print"); printBtn.addActionListener(this); buttonPanel.add(printBtn); buttonPanel.add(exitBtn); contentPane.add(panel , BorderLayout.CENTER); contentPane.add(buttonPanel, BorderLayout.SOUTH); contentPane.setVisible(true); jfrm.pack(); jfrm.show(); } public void actionPerformed(ActionEvent e){ System.out.println("Hello !!!" + fnametxt.getText()); } } ผลลัพธที่ได เมื่อคลิกปุม print จะแสดงขอความ Hello!!! Surangkana เมื่อคลิกปุม Exit ออกจากโปรแกรม
  • 14. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 313 แบบฝกหัด 1) จากแบบฝกหัดในบทที่ 12 ขอที่ 1 ใหทําการสรางหนาจอดังนี้ เขียนโปรแกรมทําการดักจับเหตุการณที่จะเกิดกับปุม “Magenta” คือถาคลิกปุมนี้แลวแถบดานบนจะ ปรากฎสีมวง ถาคลิกปุม “Cyan” แลวจะปรากฎสีฟาบนพื้นที่ดานบนของปุม และถาคลิกปุม “Orange” จะปรากฎสีสมดานบนของปุม เมื่อคลิกไอคอน X ทําการปดโปรแกรม 2 ) ใหนิสิตสรางโปรแกรมเพื่อทําการรับคา First name และ Last name เมื่อทําการคลิกปุม Print แสดง ขอความ “Hello!! ชื่อ นามสกุล” คลิกปุม Clear ทําการลบขอความใน textbox ทั้งหมด กดปุน Exit ออกจากโปรแกรม ดังตัวอยางตอไปนี้ 3) จากตัวอยางบทที่ 12 คลาส JTextFieldArea แสดงหนาจอดังนี้ CCLLIICCKK
  • 15. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 314 ใหนิสิตกําหนด action ใหกับปุมตางๆ ดังนี้ - ปุม Total ทําการ นําขอความใน Text 1 เชื่อมกับขอความใน Text 2 แสดงในชอง Total - ปุม Clear ทําใหชองขอความทุกชองในหนาจอเปนชองวาง - ปุม Exit เมื่อคลิกแลวทําใหปดโปรแกรม 4) สรางคลาสชื่อ FirstGUI แลวทําการเพิ่มปุมการทํางานใน JFrame เมื่อคลิกปุมดังกลาวจะปรากฎ ขอความ “Modified First Window”. ในสวน title bar ของ window หากคลิกปุมนี้อีกครั้ง ขอความจะเปลี่ยนเปน “Modified Again” และหากคลิกอีกครั้งจะกลับเปนขอความ “Modified First Window” สลับกัน 5) สราง Window เพื่อทําการรับคา User name และ Password เมื่อคลิกปุม login จะปรากฎ messagebox แสดงขอความ “Account successfully create” แลวตามดวย user และ password ที่รับคาไป แสดง ดังรูป 6) ใหนิสิตเขียนโปรแกรมเพื่อคํานวณการรักษาฟน ประกอบดวย 3 รายการ ดังหนาจอตอไปนี้ JCheckBox components (unchecked)
  • 16. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 315 หากไมไดกรอกชื่อหรือไมเลือกวิธีการรักษา แลวคลิกปุม calculate จะปรากฎหนาจอ เตือนดังรูป หากคลิกปุม Ok จะเขามายังหนาจอเดิม ใหกรอกชื่อและทําการเลือกวิธีรักษา เมื่อคลิกปุม Calculate จะแสดงผลรวมของการรักษาดังรูป แตหากไมกรอกชื่อ Patient Name เมื่อคลิกปุม Calculate จะคํานวณ แลวแสดง message box ดังรูป Message dialog Click OK JButton to close the dialog JCheckBox components (checked) Close button Dialog sized to accommodate contents Title bar Icon indicates the tone of the message OK JButton allows user to close the dialog
  • 17. บทที่ 13 การจัดการกับอีเวนต หนาที่ เอกสารประกอบการสอน 305272 การเขียนโปรแกรมคอมพิวเตอรขั้นสูง อ.สุรางคนา ระวังยศ 316 7) ใหนิสิตเขียนโปรแกรมดังหนาจอตอไปนี้ - เมื่อเลือกประเทศ ที่เปนคําตอบจาก JComboBox - สงคําตอบที่ถูกโดยคลิกปุม Submit จะแสดงขอความ Correct ใน JLabel ดานลาง - เมื่อคลิกปุม Next Flag JComboBox contains answers (country names) JLabel แสดงรูปธง คําตอบที่ถูกเลือก Submit JButton สามารถกลับมาใชไดใหม JComboBox ของ dropdownlistbox เมื่อตอบถูกแสดงขอความ Correct! JTextField ถูก clear