Bài giảng Nhập môn java - Chương 6: Lập trình sự kiện
Số trang: 70
Loại file: ppt
Dung lượng: 1.40 MB
Lượt xem: 11
Lượt tải: 0
Xem trước 7 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Bài giảng Nhập môn Java - Chương 6 trình bày những nội dung liên quan đến lập trình sự kiện như: Các ví dụ mở đầu, mô hình xử lý sự kiện, các component nâng cao, xử lý sự kiện chuột, xử lý sự kiện bàn phím. Mời các bạn cùng tham khảo để nắm bắt các nội dung chi tiết.
Nội dung trích xuất từ tài liệu:
Bài giảng Nhập môn java - Chương 6: Lập trình sự kiệnCHƯƠNG 6LẬP TRÌNH SỰ KIỆNNỘI DUNG TRÌNH BÀY • Các ví dụ mở đầu • Mô hình xử lý sự kiện • Các component nâng cao • Xử lý sự kiện chuột • Xử lý sự kiện bàn phím 2 PHẦN 1CÁC VÍ DỤ MỞ ĐẦUVÍ DỤ 1Xây dựng một chương trình như sau:• Khi nhấn vào button Red hoặc button Green hoặc button Blue thì nền của cửa sổ chương trình thay đổi màu tương ứng, đồng thời label bên dưới các button cũng có câu thông báo màu tương ứng. 4VÍ DỤ 1 (file MyFirstAwt.java)import java.awt.*;import java.awt.event.*;public class MyFirstAwt extends Frame{ Label status; Button button1 = new Button(Red); Button button2 = new Button(Green); Button button3 = new Button(Blue); MyFirstAwt() { this.setTitle(My First Awt); //super(My First Awt); this.setLayout(new FlowLayout()); this.add(button1); this.add(button2); this.add(button3); status = new Label(); status.setText(Press any button, please!); this.add(status); 5//xem tiếp ở slide tiếp theoVÍ DỤ 1 (file MyFirstAwt.java) - tt button1.addActionListener(new MyListener(status,this)); button2.addActionListener(new MyListener(status,this)); button3.addActionListener(new MyListener(status,this)); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt){System.exit(0);} }); } public static void main(String[] args) { MyFirstAwt mfa = new MyFirstAwt(); mfa.resize(300,200); mfa.show(); } } 6VÍ DỤ 1 (file MyListener.java)import java.awt.*;import java.awt.event.*;public class MyListener implements ActionListener{ Label status; Component compo; MyListener(Label status1, Component compo1) { this.status = status1; this.compo = compo1; }//xem tiếp ở slide tiếp theo 7VÍ DỤ 1 (file MyListener.java) - tt public void actionPerformed(ActionEvent evt) { if(evt.getSource() instanceof Button) { Button temp = (Button)evt.getSource(); status.setText(You have selected: + temp.getLabel()); if(temp.getLabel().equalsIgnoreCase(Red)) { compo.setBackground(new Color(255,0,0)); } if(temp.getLabel().equalsIgnoreCase(Green)) { compo.setBackground(new Color(0,255,0)); } if(temp.getLabel().equalsIgnoreCase(Blue)) { compo.setBackground(new Color(0,0,255)); } } } } 8VÍ DỤ 2Xây dựng một chương trình như sau:• Khi nhấn vào button Yes hoặc button No hoặc button Maybe thì xuất hiện câu thông báo tương ứng. 9VÍ DỤ 2 (file ButtonDemo.java)import java.awt.*; import java.awt.event.*;public class ButtonDemo extends Frame implements ActionListener{ String messege = ; Button yes,no,maybe; Label label = new Label(); ButtonDemo(String msg) { setTitle(msg); //super(My First Awt); setLayout(new FlowLayout()); yes = new Button(Yes); no = new Button(No); maybe = new Button(Maybe); add(yes); add(no); add(maybe); add(label); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } //xem tiếp ở slide tiếp theo 10VÍ DỤ 2 (file ButtonDemo.java)-tt public void actionPerformed(ActionEvent evt) { String str = evt.getActionCommand(); if(str.equals(Yes)) { label.setText(You pressed Yes button); } if(str.equals(No)) { label.setText(You pressed No button); } if(str.equals(Maybe)) { label.setText(You pressed Maybe button); } } public static void main(String[] args) { ButtonDemo btdm = new ButtonDemo(My Button Demo); btdm.s ...
Nội dung trích xuất từ tài liệu:
Bài giảng Nhập môn java - Chương 6: Lập trình sự kiệnCHƯƠNG 6LẬP TRÌNH SỰ KIỆNNỘI DUNG TRÌNH BÀY • Các ví dụ mở đầu • Mô hình xử lý sự kiện • Các component nâng cao • Xử lý sự kiện chuột • Xử lý sự kiện bàn phím 2 PHẦN 1CÁC VÍ DỤ MỞ ĐẦUVÍ DỤ 1Xây dựng một chương trình như sau:• Khi nhấn vào button Red hoặc button Green hoặc button Blue thì nền của cửa sổ chương trình thay đổi màu tương ứng, đồng thời label bên dưới các button cũng có câu thông báo màu tương ứng. 4VÍ DỤ 1 (file MyFirstAwt.java)import java.awt.*;import java.awt.event.*;public class MyFirstAwt extends Frame{ Label status; Button button1 = new Button(Red); Button button2 = new Button(Green); Button button3 = new Button(Blue); MyFirstAwt() { this.setTitle(My First Awt); //super(My First Awt); this.setLayout(new FlowLayout()); this.add(button1); this.add(button2); this.add(button3); status = new Label(); status.setText(Press any button, please!); this.add(status); 5//xem tiếp ở slide tiếp theoVÍ DỤ 1 (file MyFirstAwt.java) - tt button1.addActionListener(new MyListener(status,this)); button2.addActionListener(new MyListener(status,this)); button3.addActionListener(new MyListener(status,this)); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent evt){System.exit(0);} }); } public static void main(String[] args) { MyFirstAwt mfa = new MyFirstAwt(); mfa.resize(300,200); mfa.show(); } } 6VÍ DỤ 1 (file MyListener.java)import java.awt.*;import java.awt.event.*;public class MyListener implements ActionListener{ Label status; Component compo; MyListener(Label status1, Component compo1) { this.status = status1; this.compo = compo1; }//xem tiếp ở slide tiếp theo 7VÍ DỤ 1 (file MyListener.java) - tt public void actionPerformed(ActionEvent evt) { if(evt.getSource() instanceof Button) { Button temp = (Button)evt.getSource(); status.setText(You have selected: + temp.getLabel()); if(temp.getLabel().equalsIgnoreCase(Red)) { compo.setBackground(new Color(255,0,0)); } if(temp.getLabel().equalsIgnoreCase(Green)) { compo.setBackground(new Color(0,255,0)); } if(temp.getLabel().equalsIgnoreCase(Blue)) { compo.setBackground(new Color(0,0,255)); } } } } 8VÍ DỤ 2Xây dựng một chương trình như sau:• Khi nhấn vào button Yes hoặc button No hoặc button Maybe thì xuất hiện câu thông báo tương ứng. 9VÍ DỤ 2 (file ButtonDemo.java)import java.awt.*; import java.awt.event.*;public class ButtonDemo extends Frame implements ActionListener{ String messege = ; Button yes,no,maybe; Label label = new Label(); ButtonDemo(String msg) { setTitle(msg); //super(My First Awt); setLayout(new FlowLayout()); yes = new Button(Yes); no = new Button(No); maybe = new Button(Maybe); add(yes); add(no); add(maybe); add(label); yes.addActionListener(this); no.addActionListener(this); maybe.addActionListener(this); } //xem tiếp ở slide tiếp theo 10VÍ DỤ 2 (file ButtonDemo.java)-tt public void actionPerformed(ActionEvent evt) { String str = evt.getActionCommand(); if(str.equals(Yes)) { label.setText(You pressed Yes button); } if(str.equals(No)) { label.setText(You pressed No button); } if(str.equals(Maybe)) { label.setText(You pressed Maybe button); } } public static void main(String[] args) { ButtonDemo btdm = new ButtonDemo(My Button Demo); btdm.s ...
Tìm kiếm tài liệu theo từ khóa liên quan:
Lập trình java Nhập môn java Ngôn ngữ java Lập trình sự kiện Mô hình xử lý sự kiện Xử lý sự kiện chuột Xử lý sự kiện bàn phímTài liệu liên quan:
-
Bài toán phân luồng giao thông và ứng dụng
11 trang 180 1 0 -
Excel add in development in c and c phần 9
0 trang 110 0 0 -
Program C Ansi Programming Embedded Systems in C and C++ phần 4
12 trang 98 0 0 -
Lập trình Java cơ bản : GUI nâng cao part 3
6 trang 85 0 0 -
265 trang 82 0 0
-
81 trang 68 0 0
-
Bài giảng Nhập môn Java: Bài 12 – Võ Tấn Dũng
12 trang 60 0 0 -
Nghiên cứu hệ thống báo cháy ứng dụng cảm biến nhiệt hồng ngoại và camera
4 trang 58 0 0 -
Giáo trình Lập trình mạng - ThS. Văn Thiên Hoàng
201 trang 56 0 0 -
7 trang 51 0 0
-
67 trang 46 0 0
-
Mô tả công việc lập trình viên Java
1 trang 36 0 0 -
Code Division Multiple Access (CDMA) phần 10
19 trang 35 0 0 -
153 trang 33 0 0
-
Bài giảng Lập trình Java cơ bản: Chương 3 - GV. Võ Hoàng Phương Dung
55 trang 33 0 0 -
Lập trình Java: Chương 4: Tính kế thừa và đa hình
27 trang 32 0 0 -
Giáo trình Lập trình viên công nghệ Java (Module 3) - Trung tâm tin học ĐH KHTN
176 trang 31 0 0 -
Bài giảng Lập trình Java căn bản: Chương 2 - ThS. Võ Đức Cẩm Hải
24 trang 30 0 0 -
100 trang 30 0 0
-
59 trang 30 0 0