SlideShare a Scribd company logo
1 of 71
Download to read offline
Bài 12
Đồ họa và xử lý
sự kiện
Trịnh Thành Trung
trungtt@soict.hust.edu.vn
Nội dung
1. Giao diện đồ họa người
sử dụng
2. AWT
3. Xử lý sự kiện
4. Swing
Giao diện đồ họa
người sử dụng
Graphical User Interface (GUI)
1
4
Giao diện đồ họa người dùng
• Giao diện đồ họa người sử dụng (Graphical user
interface – GUI)
• Giúp tạo ra các ứng dụng có giao diện đồ họa với
nhiều các điều khiển như: Button, Textbox, Label,
Checkbox, List, Tree...
5
Ví dụ
• Giao diện trình duyệt web
Button
Title bar
Menu bar
Combo box
Menus
Scroll bar
6
• Java cung cấp hai thư viện đồ họa
− AWT
− Swing
• AWT
− Được cung cấp trong Java 1.0
• Swing
− Nâng cấp các thành phần giao diện của AWT
− Được tích hợp trong Java 1.2
Lập trình GUI trong Java
7
• Một số loại giao diện khác
− Eclipse's Standard Widget Toolkit (SWT)
− Google Web Toolkit (GWT)
− Các thư viện đồ họa như Java bindings for OpenGL
(JOGL) hay Java3D.
Lập trình GUI trong Java
AWT
Advanced Widget Toolkit
2
9
• AWT – Advanced Widget Toolkit
• Các lớp AWT được Java cung cấp trong 12 gói
− Các gói java.awt và java.awt.event được sử dụng chủ yếu
− Độc lập nền và độc lập thiết bị
• Các lớp cơ bản
− Các thành phần GUI (vd. Button, TextField, Label…)
− Các lớp GUI Container (vd. Frame, Panel, Dialog,
ScrollPane…)
− Layout managers (vd. FlowLayout, BorderLayout,
GridLayout…)
− Các lớp đồ họa (vd. Graphics, Color, Font…)
AWT
10
• Các lớp xử lý sự kiện
− Các lớp Event (vd. ActionEvent, MouseEvent, KeyEvent
and WindowEvent…)
− Các giao diện Event Listener (vd. ActionListener,
MouseListener, KeyListener, WindowListener…)
− Các lớp Event Listener Adapter (vd. MouseAdapter,
KeyAdapter, WindowAdapter…)
AWT (tiếp)
11
• 2 loại thành phần chính
− Component: các thực thể GUI cơ bản (Button, Label,
TextField.)
− Container (Frame, Panel and Applet): Chứa các thực thể
GUI. Một containter cũng có thể chứa các container khác.
Các thành phần của AWT
12
• Top-level container: Frame, Dialog và Applet.
• Frame: Cung cấp cửa sổ chính cho ứng dụng,
chứa:
− title bar (chứa biểu tượng, tiêu đề, các nút minimize,
maximize & close)
− menu bar
− vùng hiển thị nội dung
Top-level AWT container
13
• Dialog: còn gọi là "pop-up window“, chứa:
− title-bar
− vùng hiển thị nội dung
• Applet: Ứng dụng Java có thể chạy trên trình
duyệt
Top-level AWT container
14
• Secondary Containers: Panel, ScrollPane
− Đặt bên trong top-level container hoặc các secondary
container khác
• Panel
− Một vùng hình chữ nhật nằm bên trong container
− Sử dụng để áp dụng một layout cho các thành phần bên
trong
• ScrollPane: tạo ra một vùng có thể trượt dọc hoặc
trượt ngang các thành phần bên trong
Secondary AWT container
15
Cây phân cấp kế
thừa của các lớp
trong AWT
Cây phân cấp kế thừa AWT
16
• Button, TextField, Label, Checkbox,
CheckboxGroup (radio buttons), List, and Choice
Các thành phần của AWT
17
• java.awt.Label: Hiển thị một nhãn văn bản
• Phương thức khởi tạo
− // Construct a Label with the given text String, of the text
alignment
− public Label(String strLabel, int alignment);
− public Label(String strLabel); // Construct a Label with the
given text
− public Label(); // Construct an initially empty Label
• Phương thức
− public String getText();
− public void setText(String strLabel);
− public int getAlignment();
− public void setAlignment(int alignment);
Label
18
• Các bước để tạo một component và add vào
container:
− Khai báo và khởi tạo thành phần đó
− Xác định container sẽ chứa thành phần này
+ Sử dụng phương thức add
+ VD: aContainer.add(aComponent)
• Ví dụ
Label lblInput;
lblInput = new Label("Enter ID");
this.add(lblInput);
lblInput.setText("Enter password");
lblInput.getText();
Add component vào container
19
• java.awt.Button: Kích hoạt một sự kiện khi nhấp chuột
• Phương thức khởi tạo
− public Button(String buttonLabel);
− public Button(String buttonLabel);
• Các phương thức
− public String getLabel();
− public void setLabel(String buttonLabel);
− public void setEnable(boolean enable);
• Ví dụ
Button btnColor = new Button("Red");
this.add(btnColor);
...
btnColor.setLabel("green");
btnColor.getLabel();
Button
20
• java.awt.TextField: Ô văn bản để người dùng có
thể nhập liệu trong một dòng (TextArea: nhiều
dòng)
• Phương thức khởi tạo
− public TextField(String strInitialText, int columns);
− public TextField(String strInitialText);
− public TextField(int columns);
• Các phương thức
− public String getText();
− public void setText(String strText);
− public void setEditable(boolean editable);
TextField
21
• Layout: Sắp xếp các thành phần trong container
• Các Layout manager trong AWT: (trong gói
java.awt)
− FlowLayout
− GridLayout
− BorderLayout
− GridBagLayout
− BoxLayout
− CardLayout
Quản lý bố cục
22
• Gọi đến phương thức setLayout() của container
public void setLayout(LayoutManager mgr)
• Các bước để thiết lập Layout trong Container
− Khởi tạo đối tượng Layout tương ứng, vd. new FlowLayout()
− Gọi đến phương thức setLayout với tham số là đối tượng
vừa tạo
− Gọi phương thức add của container theo thứ tự tương ứng
• Ví dụ
Panel p = new Panel();
p.setLayout(new FlowLayout());
p.add(new JLabel("One"));
p.add(new JLabel("Two"));
p.add(new JLabel("Three"));
Thiết lập Layout Manager
24
• Với các container sử dụng FlowLayout:
− Các thành phần được sắp xếp lần lượt từ trái sang phải
− Khi đầy một dòng -> tạo dòng mới
• Phương thức khởi tạo
− public FlowLayout();
− public FlowLayout(int align);
− public FlowLayout(int align, int hgap, int vgap);
Align:
+ FlowLayout.LEFT (or LEADING)
+ FlowLayout.RIGHT (or TRAILING)
+ FlowLayout.CENTER
hgap, vgap: khoảng cách dọc/ngang giữa các thành phần.
− mặc định: hgap=5, vgap=5, align=CENTER
FlowLayout
25
import java.awt.*;
import java.awt.event.*;
public class AWTFlowLayout extends Frame {
public AWTFlowLayout () {
setLayout(new FlowLayout());
add(new Button("Button 1"));
add(new Button("This is Button 2"));
add(new Button("3"));
add(new Button("Another Button 4"));
add(new Button("Button 5"));
add(new Button("One More Button 6"));
setTitle("FlowLayout"); // "this" Frame sets title
setSize(280, 150); // "this" Frame sets initial size
setVisible(true); // "this" Frame shows
}
public static void main(String[] args) {
new AWTFlowLayout(); // Let the constructor do the job
}
}
Ví dụ FlowLayout
26
• Với các container sử dụng FlowLayout:
− Các thành phần được sắp xếp theo hàng và cột
• Phương thức khởi tạo
− public GridLayout(int rows, int columns);
− public GridLayout(int rows, int columns, int hgap, int vgap);
• mặc định: rows=1, cols=0, hgap=0, vgap=0
GridLayout
27
BorderLayout
• Với BorderLayout, container
được chia làm 5 phần: EAST,
WEST, SOUTH, NORTH,
CENTER
• Phương thức khởi tạo
• public BorderLayout();
• public BorderLayout(int
hgap, int vgap);
• mặc định hgap=0, vgap=0
• Khi thêm vào một thành phần
• aContainer.add(acomponent,
aZone)
• aZone:
• BorderLayout.NORTH (or
PAGE_START)
• BorderLayout.SOUTH (or
PAGE_END)
• BorderLayout.WEST (or
LINE_START)
• BorderLayout.EAST (or LINE_END)
• BorderLayout.CENTER
• aContainer.add(aComponent):
thêm thành phần vào CENTER
• Không bắt buộc phải thêm đủ
thành phần vào cả 5 vùng
Xử lý sự kiện
Các giao diện listener và các cài đặt
3
29
• Xử lý sự kiện theo mô hình "hướng sự kiện" (event-
driven)
− Khi một sự kiện xảy ra -> gọi đến mã xử lý tương ứng
• Các đối tượng: source, listener, event
− source là đối tượng sinh ra các sự kiện (event) (ví dụ: khi
người dùng tác động vào)
− event thông điệp được gửi đến các đối tượng listener đã
được đăng ký
− Các phương thức xử lý sự kiện tương ứng của listener sẽ
được gọi để xử lý các sự kiện
• Các listener phải được đăng ký với các source để
quản lý các sự kiện nhất định có thể xảy ra tại các
source.
Tổng quan
30
Xử lý sự kiện
• AWT cung cấp các lớp xử lý sự kiện trong
java.awt.event
Chiến lược xử lý sự kiện
Các giao diện listener Phương thức đăng ký
ActionListener addActionListener
AdjustmentListener addAdjustmentListener
ComponentListener addComponentListener
ContainerListener addContainerListener
FocusListener addFocusListener
ItemListener addItemListener
KeyListener addKeyListener
MouseListener addMouseListener
MouseMotionListener addMouseMotionListener
TextListener addTextListener
WindowListener addWindowListener
32
• ActionListener
− Xử lý các nút và một số ít các hành động khác
+ actionPerformed(ActionEvent event)
• AdjustmentListener
− Áp dụng khi cuộn (scrolling)
+ adjustmentValueChanged(AdjustmentEvent event)
• ComponentListener
− Xử lý các sự kiện dịch chuyển/thay đổi kích thước/ẩn các đối
tượng GUI
+ componentResized(ComponentEvent event)
+ componentMoved (ComponentEvent event)
+ componentShown(ComponentEvent event)
+ componentHidden(ComponentEvent event)
Một số listener cơ bản
33
• ContainerListener
− Được kích hoạt khi cửa sổ thêm/gỡ bỏ các đối tượng GUI
+ componentAdded(ContainerEvent event)
+ componentRemoved(ContainerEvent event)
• FocusListener
− Phát hiện khi nào các đối tượng có/mất focus
+ focusGained(FocusEvent event)
+ focusLost(FocusEvent event)
Một số listener cơ bản
34
• ItemListener
−Xử lý các sự kiện chọn trong các list, checkbox,...
+ itemStateChanged(ItemEvent event)
• KeyListener
−Phát hiện ra các sự kiện liên quan đến bàn phím
+ keyPressed(KeyEvent event) // any key
pressed down
+ keyReleased(KeyEvent event) // any key
released
+ keyTyped(KeyEvent event) // key for printable
char released
Một số listener cơ bản
35
• MouseListener
−Áp dụng cho các sự kiện chuột cơ bản
+ mouseEntered(MouseEvent event)
+ mouseExited(MouseEvent event)
+ mousePressed(MouseEvent event)
+ mouseReleased(MouseEvent event)
+ mouseClicked(MouseEvent event) -- Nhả chuột khi
không kéo
• Áp dụng khi nhả chuột mà không di chuyển từ khi nhấn
chuột
• MouseMotionListener
−Xử lý các sự kiện di chuyển chuột
+ mouseMoved(MouseEvent event)
+ mouseDragged(MouseEvent event)
Một số listener cơ bản
36
• TextListener
− Áp dụng cho các textfield và text area
+ textValueChanged(TextEvent event)
• WindowListener
− Xử lý các sự kiện mức cao của cửa sổ
+ windowOpened, windowClosing, windowClosed,
windowIconified, windowDeiconified,
windowActivated, windowDeactivated
• windowClosing đặc biệt rất hữu dụng
Một số listener cơ bản
37
• Xây dựng lớp listener tương ứng với sự kiện
− Thực thi giao diện XxxListener
• Cài đặt các phương thức tương ứng với sự kiện
của giao diện này
• Đăng ký các listener với nguồn
− public void addXxxListener(XxxListener l);
− public void removeXxxListener(XxxListener l);
Chiến lược xử lý sự kiện
• Giao diện MouseListener
interface MouseListener {
// Called back upon mouse-button pressed
public void mousePressed(MouseEvent evt);
// Called back upon mouse-button released
public void mouseReleased(MouseEvent evt);
// Called back upon mouse-button clicked
(pressed and released)
public void mouseClicked(MouseEvent evt);
// Called back when mouse pointer entered the
component
public void mouseEntered(MouseEvent evt);
// Called back when mouse pointer exited the
component
public void mouseExited(MouseEvent evt);
}
Ví dụ
Xây dựng lớp Listener
class MyMouseListener implements MouseListener {
public void mousePressed(MouseEvent event) {
System.out.println
("Mouse-button pressed at (" + event.getX()
+ "," + event.getY() + ").");
}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
}
import java.awt.*;
public class ButtonEventExample extends Frame {
public ButtonEventExample () {
Button b = new Button("Button");
add(b);
b.addMouseListener(new MyMouseListener());
setTitle(“Button Event Example");
setSize(280, 150);
setVisible(true);
}
public static void main(String[] args) {
new ButtonEventExample();
}
}
Đăng ký với button
41
• Nhược điểm của việc sử dụng giao diện
XxxListener
− Phải cài đặt tất cả các phương thức của giao diện
− Nếu chỉ cần xử lý 1 sự kiện -> tạo ra rất nhiều phương
thức rỗng
• AWT cung cấp các lớp adapter cho các listener
có nhiều hơn 1 phương thức
• Để sử dụng các lớp này, ta viết các lớp kế thừa từ
các lớp Adapter thay vì thực thi các giao diện
Adapter
Các giao diện listener Các lớp Adapter Phương thức đăng ký
ActionListener addActionListener
AdjustmentListener addAdjustmentListener
ComponentListener ComponentAdapter addComponentListener
ContainerListener ContainerAdapter addContainerListener
FocusListener FocusAdapter addFocusListener
ItemListener addItemListener
KeyListener KeyAdapter addKeyListener
MouseListener MouseAdapter addMouseListener
MouseMotionListener MouseMotionAdapter addMouseMotionListener
TextListener addTextListener
WindowListener WindowAdapter addWindowListener
Các Adapter tương ứng
Ví dụ
class MyMouseListener extends MouseAdapter {
public void mousePressed(MouseEvent event) {
System.out.println("Mouse-button pressed at (" +
event.getX() + "," + event.getY() + ").");
}
}
public class FrameEventExample extends Frame {
public FrameEventExample () {
addMouseListener(new MyMouseListener());
setTitle(“Button Event Example");
setSize(640, 480);
setVisible(true);
}
}
44
• Nếu ClickListener muốn vẽ một hình tròn tại vị trí
chuột được nhấn?
− Tại sao không thể gọi phương thức getGraphics để lấy về
đối tượng Graphics để vẽ
• Cách 1
− Gọi event.getSource để lấy về một tham chiếu tới cửa sổ
hoặc thành phần GUI tạo ra sự kiện
− Ép kết quả trả về theo ý muốn
− Gọi các phương thức trên tham chiếu đó
Lấy đối tượng từ source
public class CircleListener extends MouseAdapter
{
private int radius = 10;
public void mousePressed(MouseEvent event) {
Frame app = (Frame)event.getSource();
Graphics g = app.getGraphics();
g.fillOval(event.getX()-radius,
event.getY()-radius,
2*radius, 2*radius);
}
}
Cách 1: Sử dụng getSource
Cách 1: Sử dụng getSource
public class FrameEventExample extends Frame {
private int radius = 10;
public FrameEventExample() {
addMouseListener(new CircleListener());
setSize(640, 480);
setVisible(true);
}
Cách 2: Thực thi giao diện listener
• Giúp frame hiện tại đóng vai trò như một
listener
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class FrameEventExample extends Frame
implements MouseListener {
private int radius = 10;
public FrameEventExample() {
addMouseListener(this);
}
Cách 2: Thực thi giao diện listener
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void mouseReleased(MouseEvent event) {}
public void mouseClicked(MouseEvent event) {}
public void mousePressed(MouseEvent event) {
Graphics g = getGraphics();
g.fillOval(event.getX()-radius,
event.getY()-radius,
2*radius, 2*radius);
}
}
Cách 3: Sử dụng inner class
• Viết lớp listener bên trong lớp frame
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class FrameEventExample extends Frame {
public FrameEventExample() {
addMouseListener(new CircleListener());
}
private class CircleListener extends MouseAdapter {
…
}
}
Cách 4: Sử dụng anonymous
inner class
public class FrameEventExample extends Frame {
public FrameEventExample() {
addMouseListener
(new MouseAdapter() {
private int radius = 25;
public void mousePressed(MouseEvent event) {
Graphics g = getGraphics();
g.fillOval(event.getX()-radius,
event.getY()-radius,
2*radius, 2*radius);
}
});
}
}
Ví dụ: Simple White Board
import java.awt.*;
import java.awt.event.*;
public class SimpleWhiteboard extends Frame {
protected int lastX=0, lastY=0;
public SimpleWhiteboard() {
setBackground(Color.white);
setForeground(Color.blue);
addMouseListener(new PositionRecorder());
addMouseMotionListener(new LineDrawer());
setSize(640, 480);
setVisible(true);
}
Ví dụ: Simple White Board (tiếp)
protected void record(int x, int y) {
lastX = x; lastY = y;
}
private class PositionRecorder extends MouseAdapter {
public void mouseEntered(MouseEvent event) {
requestFocus();
record(event.getX(), event.getY());
}
public void mousePressed(MouseEvent event) {
record(event.getX(), event.getY());
}
}
Ví dụ: Simple White Board (tiếp)
private class LineDrawer extends MouseMotionAdapter {
public void mouseDragged(MouseEvent event) {
int x = event.getX();
int y = event.getY();
Graphics g = getGraphics();
g.drawLine(lastX, lastY, x, y);
record(x, y);
}
}
}
Ví dụ: Simple White Board (tiếp)
public class WhiteBoardDemo {
public static void main(String args[]) {
new SimpleWhiteboard();
}
}
Swing
javax.swing
4
56
• Cách đặt tên
− Tất cả các thành phần trong swing đều có tên bắt đầu với
chữ hoa J và tuân theo khuôn dạng JXxxx. Ví dụ: JFrame,
JPanel, JApplet, JDialog, JButton,...
• Các thành phần “nhẹ”? (lightweight)
− Hầu hết các thành phần swing đều “nhẹ”, được tạo ra
bằng cách vẽ trong cửa sổ cơ sở
• Look and Feel mới (mặc định)
− Có thể thay đổi Look and Feel tự nhiên (native look)
• Không nên trộn cả swing và awt trong một cửa
sổ.
Swing vs. AWT
57
GUI component hierarchy
58
Windows Look and Feel
59
Motif Look and Feel
60
Java Look and Feel
61
Thay đổi Look and Feel
• Gọi phương thức setLookAndFeel
public class WindowUtilities {
public static void setNativeLookAndFeel() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch(Exception e) {
System.out.println(“Co loi khi thay doi LAF: “+e);
}
}
...
62
• Các lớp container
− JApplet, JFrame
• Các thành phần Swing tương đương với các thành
phần AWT
− JLabel, JButton, JPanel, JSlider
• Các thành phần Swing mới
− JColorChooser, JInternalFrame, JOptionPane,
JToolBar, JEditorPane
• Các thành phần đơn giản khác
− JCheckBox, JRadioButton, JTextField, JTextArea,
JFileChooser
Các thành phần Swing
63
• Content pane
− A JApplet contains a content pane in which to add
components. Changing other properties like the layout
manager, background color, etc., also applies to the content
pane. Access the content pane through getContentPane.
• Layout manager
− The default layout manager is BorderLayout (as with Frame
and JFrame), not FlowLayout (as with Applet). BorderLayout
is really layout manager of content pane.
• Look and feel
− The default look and feel is Java (Metal), so you have to
explicitly switch the look and feel if you want the native look.
JApplet
import java.awt.*;
import javax.swing.*;
public class JAppletExample extends JApplet {
public void init() {
WindowUtilities.setNativeLookAndFeel();
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
content.add(new JButton("Button 1"));
content.add(new JButton("Button 2"));
content.add(new JButton("Button 3"));
}
}
JApplet
65
• JLabel
− New features: HTML content images, borders
• JButton
− New features: icons, alignment, mnemonics
• JPanel
− New feature: borders
• JSlider
− New features: tick marks and labels
Các thành phần tương tự AWT
66
• Main new feature: icons
− Create an ImageIcon by passing the ImageIcon
constructor a String representing a GIF or JPG file
(animated GIFs!).
− Pass the ImageIcon to the JButton constructor.
• Other features
− HTML content as with JLabel
− Alignment: location of image with respect to text
− Mnemonics: keyboard accelerators that let you use Alt-
someChar to trigger the button.
JButton
import java.awt.*;
import javax.swing.*;
public class JButtons extends JFrame {
public static void main(String[] args) {
new JButtons();
}
public JButtons() {
super("Using JButton");
WindowUtilities.setNativeLookAndFeel();
addWindowListener(new ExitListener());
Container content = getContentPane();
content.setBackground(Color.white);
content.setLayout(new FlowLayout());
Ví dụ JButton
JButton button1 = new JButton("Java");
content.add(button1);
ImageIcon cup = new ImageIcon("images/cup.gif");
JButton button2 = new JButton(cup);
content.add(button2);
JButton button3 = new JButton("Java", cup);
content.add(button3);
JButton button4 = new JButton("Java", cup);
button4.setHorizontalTextPosition(SwingConstants.LEFT);
content.add(button4);
pack();
setVisible(true);
}
}
Ví dụ JButton
69
• Very rich class with many options for different
types of dialog boxes.
• Five main static methods
− JOptionPane.showMessageDialog
+ Icon, message, OK button
− JOptionPane.showConfirmDialog
+ Icon, message, and buttons:
OK, OK/Cancel, Yes/No, or Yes/No/Cancel
− JOptionPane.showInputDialog (2 versions)
+ Icon, message, textfield or combo box, buttons
− JOptionPane.showOptionDialog
+ Icon, message, array of buttons or other components
JOptionPane
70
JOptionPane Message Dialogs
71
Các thành phần khác
• JCheckBox
• Note uppercase B
(vs. Checkbox in AWT)
• JRadioButton
• Use a ButtonGroup to
link radio buttons
• JTextField
• Just like AWT TextField except that it does
not act as a password field (use
JPasswordField for that)
• JTextArea
• Place in JScrollPane if
you want scrolling
• JFileChooser
Thank you!
Any questions?

More Related Content

What's hot

Oop unit 05 một số kỹ thuật java nâng cao
Oop unit 05 một số kỹ thuật java nâng caoOop unit 05 một số kỹ thuật java nâng cao
Oop unit 05 một số kỹ thuật java nâng caoTráng Hà Viết
 
Oop unit 03 xây dựng lớp
Oop unit 03 xây dựng lớpOop unit 03 xây dựng lớp
Oop unit 03 xây dựng lớpTráng Hà Viết
 
Bài 2: Biến và toán tử - Giáo trình FPT
Bài 2: Biến và toán tử - Giáo trình FPTBài 2: Biến và toán tử - Giáo trình FPT
Bài 2: Biến và toán tử - Giáo trình FPTMasterCode.vn
 
Bai04 tao vasudungdoituong
Bai04 tao vasudungdoituongBai04 tao vasudungdoituong
Bai04 tao vasudungdoituongNhuận Lê Văn
 
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...MasterCode.vn
 
Bai09 ngoai levaxulyngoaile
Bai09 ngoai levaxulyngoaileBai09 ngoai levaxulyngoaile
Bai09 ngoai levaxulyngoaileNhuận Lê Văn
 
Android Nâng cao-Bài 8-JSON & XML Parsing
Android Nâng cao-Bài 8-JSON & XML ParsingAndroid Nâng cao-Bài 8-JSON & XML Parsing
Android Nâng cao-Bài 8-JSON & XML ParsingPhuoc Nguyen
 
Oop unit 01 tổng quan lập trình hướng đối tượng
Oop unit 01 tổng quan lập trình hướng đối tượngOop unit 01 tổng quan lập trình hướng đối tượng
Oop unit 01 tổng quan lập trình hướng đối tượngTráng Hà Viết
 
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPT
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPTBài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPT
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPTMasterCode.vn
 
Chuong9 lop vadoituong
Chuong9 lop vadoituongChuong9 lop vadoituong
Chuong9 lop vadoituongMinh Ngoc Tran
 
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPT
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPTBài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPT
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPTMasterCode.vn
 

What's hot (20)

Oop unit 05 một số kỹ thuật java nâng cao
Oop unit 05 một số kỹ thuật java nâng caoOop unit 05 một số kỹ thuật java nâng cao
Oop unit 05 một số kỹ thuật java nâng cao
 
Oop unit 10 ngoại lệ
Oop unit 10 ngoại lệOop unit 10 ngoại lệ
Oop unit 10 ngoại lệ
 
Bai08 10 java_fx
Bai08 10 java_fxBai08 10 java_fx
Bai08 10 java_fx
 
Oop unit 03 xây dựng lớp
Oop unit 03 xây dựng lớpOop unit 03 xây dựng lớp
Oop unit 03 xây dựng lớp
 
Bài 2: Biến và toán tử - Giáo trình FPT
Bài 2: Biến và toán tử - Giáo trình FPTBài 2: Biến và toán tử - Giáo trình FPT
Bài 2: Biến và toán tử - Giáo trình FPT
 
Bai05 ket tapvakethua
Bai05 ket tapvakethuaBai05 ket tapvakethua
Bai05 ket tapvakethua
 
Bai04 tao vasudungdoituong
Bai04 tao vasudungdoituongBai04 tao vasudungdoituong
Bai04 tao vasudungdoituong
 
Bai08 lap trinhtongquat
Bai08 lap trinhtongquatBai08 lap trinhtongquat
Bai08 lap trinhtongquat
 
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...
Bài 2: Lập trình hướng đối tượng & Collection - Lập trình winform - Giáo trìn...
 
Linq net
Linq net Linq net
Linq net
 
Bai07 da hinh
Bai07 da hinhBai07 da hinh
Bai07 da hinh
 
Bai03 xay dunglop
Bai03 xay dunglopBai03 xay dunglop
Bai03 xay dunglop
 
Bai09 ngoai levaxulyngoaile
Bai09 ngoai levaxulyngoaileBai09 ngoai levaxulyngoaile
Bai09 ngoai levaxulyngoaile
 
Android Nâng cao-Bài 8-JSON & XML Parsing
Android Nâng cao-Bài 8-JSON & XML ParsingAndroid Nâng cao-Bài 8-JSON & XML Parsing
Android Nâng cao-Bài 8-JSON & XML Parsing
 
Bai11 ooad bieu_dolop
Bai11 ooad bieu_dolopBai11 ooad bieu_dolop
Bai11 ooad bieu_dolop
 
Oop unit 01 tổng quan lập trình hướng đối tượng
Oop unit 01 tổng quan lập trình hướng đối tượngOop unit 01 tổng quan lập trình hướng đối tượng
Oop unit 01 tổng quan lập trình hướng đối tượng
 
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPT
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPTBài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPT
Bài 2: Lập trình hướng đối tượng (OOP) - Giáo trình FPT
 
Bai02 java introduction
Bai02 java introductionBai02 java introduction
Bai02 java introduction
 
Chuong9 lop vadoituong
Chuong9 lop vadoituongChuong9 lop vadoituong
Chuong9 lop vadoituong
 
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPT
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPTBài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPT
Bài 3: Cấu trúc điều khiển, hàm và xử lý sự kiện - Giáo trình FPT
 

Viewers also liked

Presentation by abhijith suresh
Presentation by abhijith sureshPresentation by abhijith suresh
Presentation by abhijith sureshabhijith suresh
 
Diluted, but is thought leadership dead, dying or dynamic
Diluted, but is thought leadership dead, dying or dynamicDiluted, but is thought leadership dead, dying or dynamic
Diluted, but is thought leadership dead, dying or dynamicFidoly Rangel
 
curriculum vitae
curriculum vitaecurriculum vitae
curriculum vitaeSunil Singh
 
2016_FP Portfolio_JHumphries
2016_FP Portfolio_JHumphries2016_FP Portfolio_JHumphries
2016_FP Portfolio_JHumphriesJoseph Humphries
 
Final copy Annual Report 2013-14 including cover
Final copy Annual Report 2013-14 including coverFinal copy Annual Report 2013-14 including cover
Final copy Annual Report 2013-14 including coverKatie Hoskins
 
applications-and-current-challenges-of-supercomputing-across-multiple-domains...
applications-and-current-challenges-of-supercomputing-across-multiple-domains...applications-and-current-challenges-of-supercomputing-across-multiple-domains...
applications-and-current-challenges-of-supercomputing-across-multiple-domains...Neha Gupta
 
How to stand against cyberbullying
How to stand against cyberbullyingHow to stand against cyberbullying
How to stand against cyberbullyingtechexpert2345
 
рOland 2016
рOland 2016рOland 2016
рOland 2016x-mss
 
Gene 作品集
Gene 作品集Gene 作品集
Gene 作品集Gene Lai
 
Aura arboleda 8 - 1
Aura arboleda 8 - 1 Aura arboleda 8 - 1
Aura arboleda 8 - 1 Aura Arboleda
 

Viewers also liked (11)

Presentation by abhijith suresh
Presentation by abhijith sureshPresentation by abhijith suresh
Presentation by abhijith suresh
 
Diluted, but is thought leadership dead, dying or dynamic
Diluted, but is thought leadership dead, dying or dynamicDiluted, but is thought leadership dead, dying or dynamic
Diluted, but is thought leadership dead, dying or dynamic
 
curriculum vitae
curriculum vitaecurriculum vitae
curriculum vitae
 
2016_FP Portfolio_JHumphries
2016_FP Portfolio_JHumphries2016_FP Portfolio_JHumphries
2016_FP Portfolio_JHumphries
 
Final copy Annual Report 2013-14 including cover
Final copy Annual Report 2013-14 including coverFinal copy Annual Report 2013-14 including cover
Final copy Annual Report 2013-14 including cover
 
Blogspot bh
Blogspot bhBlogspot bh
Blogspot bh
 
applications-and-current-challenges-of-supercomputing-across-multiple-domains...
applications-and-current-challenges-of-supercomputing-across-multiple-domains...applications-and-current-challenges-of-supercomputing-across-multiple-domains...
applications-and-current-challenges-of-supercomputing-across-multiple-domains...
 
How to stand against cyberbullying
How to stand against cyberbullyingHow to stand against cyberbullying
How to stand against cyberbullying
 
рOland 2016
рOland 2016рOland 2016
рOland 2016
 
Gene 作品集
Gene 作品集Gene 作品集
Gene 作品集
 
Aura arboleda 8 - 1
Aura arboleda 8 - 1 Aura arboleda 8 - 1
Aura arboleda 8 - 1
 

Similar to Oop unit 12 đồ họa và xử lý sự kiện

Javagui 1226937311176297-9
Javagui 1226937311176297-9Javagui 1226937311176297-9
Javagui 1226937311176297-9Tai LeTan
 
Lập trình Java GUI
Lập trình Java GUILập trình Java GUI
Lập trình Java GUIHa Bogay
 
Giao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpGiao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpngohanty13
 
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...MasterCode.vn
 
Lập trình Android cơ bản bằng tiếng Việt
Lập trình Android cơ bản bằng tiếng ViệtLập trình Android cơ bản bằng tiếng Việt
Lập trình Android cơ bản bằng tiếng Việtlaptrinhandroid
 
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...MasterCode.vn
 
bai1-gioithieu
bai1-gioithieubai1-gioithieu
bai1-gioithieuMio Class
 
IT120-2. Bắt đầu với Hello World
IT120-2. Bắt đầu với Hello WorldIT120-2. Bắt đầu với Hello World
IT120-2. Bắt đầu với Hello WorldMultiUni
 
Bai4 ltgiaodien 9165
Bai4 ltgiaodien 9165Bai4 ltgiaodien 9165
Bai4 ltgiaodien 9165Nguyen Xuan
 
Core java 5
Core java 5Core java 5
Core java 5. .
 
Bài 7: Toast – Dialog, ListView & Binding
Bài 7: Toast – Dialog, ListView & BindingBài 7: Toast – Dialog, ListView & Binding
Bài 7: Toast – Dialog, ListView & Bindinghoccungdoanhnghiep
 
Hdth03 ltudql02-user control
Hdth03 ltudql02-user controlHdth03 ltudql02-user control
Hdth03 ltudql02-user controlDũng Đinh
 
Bai tap java_script-html-2016
Bai tap java_script-html-2016Bai tap java_script-html-2016
Bai tap java_script-html-2016viethoang89
 
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Techacademy Software
 

Similar to Oop unit 12 đồ họa và xử lý sự kiện (20)

Javagui 1226937311176297-9
Javagui 1226937311176297-9Javagui 1226937311176297-9
Javagui 1226937311176297-9
 
Lập trình Java GUI
Lập trình Java GUILập trình Java GUI
Lập trình Java GUI
 
Giao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharpGiao trinh asp.ne_tvoi_csharp
Giao trinh asp.ne_tvoi_csharp
 
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...
Bài 2: Hướng dẫn làm việc với các điều khiển - Giáo trình FPT - Có ví dụ kèm ...
 
Lập trình Android cơ bản bằng tiếng Việt
Lập trình Android cơ bản bằng tiếng ViệtLập trình Android cơ bản bằng tiếng Việt
Lập trình Android cơ bản bằng tiếng Việt
 
Asp control
Asp controlAsp control
Asp control
 
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
Bài 3: Lập trình giao diện điều khiển & Xử lý sự kiện - Lập trình winform - G...
 
bai1-gioithieu
bai1-gioithieubai1-gioithieu
bai1-gioithieu
 
IT120-2. Bắt đầu với Hello World
IT120-2. Bắt đầu với Hello WorldIT120-2. Bắt đầu với Hello World
IT120-2. Bắt đầu với Hello World
 
Bai4 ltgiaodien 9165
Bai4 ltgiaodien 9165Bai4 ltgiaodien 9165
Bai4 ltgiaodien 9165
 
Core java 5
Core java 5Core java 5
Core java 5
 
Window Form
Window FormWindow Form
Window Form
 
Bai giangvb.net
Bai giangvb.netBai giangvb.net
Bai giangvb.net
 
Bài 7: Toast – Dialog, ListView & Binding
Bài 7: Toast – Dialog, ListView & BindingBài 7: Toast – Dialog, ListView & Binding
Bài 7: Toast – Dialog, ListView & Binding
 
Hdth03 ltudql02-user control
Hdth03 ltudql02-user controlHdth03 ltudql02-user control
Hdth03 ltudql02-user control
 
Mau slide
Mau slideMau slide
Mau slide
 
Bai tap java_script-html-2016
Bai tap java_script-html-2016Bai tap java_script-html-2016
Bai tap java_script-html-2016
 
Chuong 1
Chuong 1Chuong 1
Chuong 1
 
Asp.net003
Asp.net003Asp.net003
Asp.net003
 
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
Slide bài giảng lập trình Android DTU - Phần 2 (Bắt đầu với Helloworld)
 

More from Tráng Hà Viết

Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh tài liệu, ebook
Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh   tài liệu, ebookTài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh   tài liệu, ebook
Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh tài liệu, ebookTráng Hà Viết
 
Artificial intelligence ai l1-gioi thieu
Artificial intelligence ai l1-gioi thieuArtificial intelligence ai l1-gioi thieu
Artificial intelligence ai l1-gioi thieuTráng Hà Viết
 
Artificial intelligence ai l2-tac tu
Artificial intelligence ai l2-tac tuArtificial intelligence ai l2-tac tu
Artificial intelligence ai l2-tac tuTráng Hà Viết
 
Artificial intelligence ai gioi thieu-mon_hoc_it4040
Artificial intelligence ai gioi thieu-mon_hoc_it4040Artificial intelligence ai gioi thieu-mon_hoc_it4040
Artificial intelligence ai gioi thieu-mon_hoc_it4040Tráng Hà Viết
 
Artificial intelligence ai l5-thoa man-rang_buoc
Artificial intelligence ai l5-thoa man-rang_buocArtificial intelligence ai l5-thoa man-rang_buoc
Artificial intelligence ai l5-thoa man-rang_buocTráng Hà Viết
 
Artificial intelligence ai l6-logic va-suy_dien
Artificial intelligence ai l6-logic va-suy_dienArtificial intelligence ai l6-logic va-suy_dien
Artificial intelligence ai l6-logic va-suy_dienTráng Hà Viết
 
Artificial intelligence ai l4-tim kiem-heuristic
Artificial intelligence ai l4-tim kiem-heuristicArtificial intelligence ai l4-tim kiem-heuristic
Artificial intelligence ai l4-tim kiem-heuristicTráng Hà Viết
 
Artificial intelligence ai l7-bieu dien-tri_thuc
Artificial intelligence ai l7-bieu dien-tri_thucArtificial intelligence ai l7-bieu dien-tri_thuc
Artificial intelligence ai l7-bieu dien-tri_thucTráng Hà Viết
 
Artificial intelligence ai l9-hoc may
Artificial intelligence ai l9-hoc mayArtificial intelligence ai l9-hoc may
Artificial intelligence ai l9-hoc mayTráng Hà Viết
 
Artificial intelligence ai l3-tim kiem-co_ban
Artificial intelligence ai l3-tim kiem-co_banArtificial intelligence ai l3-tim kiem-co_ban
Artificial intelligence ai l3-tim kiem-co_banTráng Hà Viết
 
Công cụ mã nguồn mở BlueFish
Công cụ mã nguồn mở BlueFishCông cụ mã nguồn mở BlueFish
Công cụ mã nguồn mở BlueFishTráng Hà Viết
 

More from Tráng Hà Viết (11)

Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh tài liệu, ebook
Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh   tài liệu, ebookTài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh   tài liệu, ebook
Tài liệu môn trí tuệ nhân tạo đh bách khoa tp hồ chí minh tài liệu, ebook
 
Artificial intelligence ai l1-gioi thieu
Artificial intelligence ai l1-gioi thieuArtificial intelligence ai l1-gioi thieu
Artificial intelligence ai l1-gioi thieu
 
Artificial intelligence ai l2-tac tu
Artificial intelligence ai l2-tac tuArtificial intelligence ai l2-tac tu
Artificial intelligence ai l2-tac tu
 
Artificial intelligence ai gioi thieu-mon_hoc_it4040
Artificial intelligence ai gioi thieu-mon_hoc_it4040Artificial intelligence ai gioi thieu-mon_hoc_it4040
Artificial intelligence ai gioi thieu-mon_hoc_it4040
 
Artificial intelligence ai l5-thoa man-rang_buoc
Artificial intelligence ai l5-thoa man-rang_buocArtificial intelligence ai l5-thoa man-rang_buoc
Artificial intelligence ai l5-thoa man-rang_buoc
 
Artificial intelligence ai l6-logic va-suy_dien
Artificial intelligence ai l6-logic va-suy_dienArtificial intelligence ai l6-logic va-suy_dien
Artificial intelligence ai l6-logic va-suy_dien
 
Artificial intelligence ai l4-tim kiem-heuristic
Artificial intelligence ai l4-tim kiem-heuristicArtificial intelligence ai l4-tim kiem-heuristic
Artificial intelligence ai l4-tim kiem-heuristic
 
Artificial intelligence ai l7-bieu dien-tri_thuc
Artificial intelligence ai l7-bieu dien-tri_thucArtificial intelligence ai l7-bieu dien-tri_thuc
Artificial intelligence ai l7-bieu dien-tri_thuc
 
Artificial intelligence ai l9-hoc may
Artificial intelligence ai l9-hoc mayArtificial intelligence ai l9-hoc may
Artificial intelligence ai l9-hoc may
 
Artificial intelligence ai l3-tim kiem-co_ban
Artificial intelligence ai l3-tim kiem-co_banArtificial intelligence ai l3-tim kiem-co_ban
Artificial intelligence ai l3-tim kiem-co_ban
 
Công cụ mã nguồn mở BlueFish
Công cụ mã nguồn mở BlueFishCông cụ mã nguồn mở BlueFish
Công cụ mã nguồn mở BlueFish
 

Recently uploaded

ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...Nguyen Thanh Tu Collection
 
Hệ phương trình tuyến tính và các ứng dụng trong kinh tế
Hệ phương trình tuyến tính và các ứng dụng trong kinh tếHệ phương trình tuyến tính và các ứng dụng trong kinh tế
Hệ phương trình tuyến tính và các ứng dụng trong kinh tếngTonH1
 
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...hoangtuansinh1
 
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXH
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXHTư tưởng Hồ Chí Minh về độc lập dân tộc và CNXH
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXHThaoPhuong154017
 
Sơ đồ tư duy môn sinh học bậc THPT.pdf
Sơ đồ tư duy môn sinh học bậc THPT.pdfSơ đồ tư duy môn sinh học bậc THPT.pdf
Sơ đồ tư duy môn sinh học bậc THPT.pdftohoanggiabao81
 
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoabài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa2353020138
 
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào môBryan Williams
 
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhh
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhhôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhh
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhhvanhathvc
 
Ma trận - định thức và các ứng dụng trong kinh tế
Ma trận - định thức và các ứng dụng trong kinh tếMa trận - định thức và các ứng dụng trong kinh tế
Ma trận - định thức và các ứng dụng trong kinh tếngTonH1
 
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...Nguyen Thanh Tu Collection
 
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...Nguyen Thanh Tu Collection
 
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptx
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptxChàm - Bệnh án (da liễu - bvdlct ctump) .pptx
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptxendkay31
 
Trích dẫn trắc nghiệm tư tưởng HCM5.docx
Trích dẫn trắc nghiệm tư tưởng HCM5.docxTrích dẫn trắc nghiệm tư tưởng HCM5.docx
Trích dẫn trắc nghiệm tư tưởng HCM5.docxnhungdt08102004
 
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh lí
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh líKiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh lí
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh líDr K-OGN
 
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...Nguyen Thanh Tu Collection
 
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )lamdapoet123
 
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...Nguyen Thanh Tu Collection
 
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...Nguyen Thanh Tu Collection
 
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdf
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdfChuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdf
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdfhoangtuansinh1
 
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...ThunTrn734461
 

Recently uploaded (20)

ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
ĐỀ THAM KHẢO THEO HƯỚNG MINH HỌA 2025 KIỂM TRA CUỐI HỌC KÌ 2 NĂM HỌC 2023-202...
 
Hệ phương trình tuyến tính và các ứng dụng trong kinh tế
Hệ phương trình tuyến tính và các ứng dụng trong kinh tếHệ phương trình tuyến tính và các ứng dụng trong kinh tế
Hệ phương trình tuyến tính và các ứng dụng trong kinh tế
 
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...
Thong bao 337-DHPY (24.4.2024) thi sat hach Ngoai ngu dap ung Chuan dau ra do...
 
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXH
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXHTư tưởng Hồ Chí Minh về độc lập dân tộc và CNXH
Tư tưởng Hồ Chí Minh về độc lập dân tộc và CNXH
 
Sơ đồ tư duy môn sinh học bậc THPT.pdf
Sơ đồ tư duy môn sinh học bậc THPT.pdfSơ đồ tư duy môn sinh học bậc THPT.pdf
Sơ đồ tư duy môn sinh học bậc THPT.pdf
 
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoabài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
bài 5.1.docx Sinh học di truyền đại cương năm nhất của học sinh y đa khoa
 
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô
[GIẢI PHẪU BỆNH] Tổn thương cơ bản của tb bào mô
 
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhh
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhhôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhh
ôn tập lịch sử hhhhhhhhhhhhhhhhhhhhhhhhhh
 
Ma trận - định thức và các ứng dụng trong kinh tế
Ma trận - định thức và các ứng dụng trong kinh tếMa trận - định thức và các ứng dụng trong kinh tế
Ma trận - định thức và các ứng dụng trong kinh tế
 
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...
BỘ ĐỀ KIỂM TRA CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO CẤU TRÚC ĐỀ MIN...
 
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
BỘ ĐỀ PHÁT TRIỂN THEO CẤU TRÚC ĐỀ MINH HỌA BGD NGÀY 22-3-2024 KỲ THI TỐT NGHI...
 
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptx
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptxChàm - Bệnh án (da liễu - bvdlct ctump) .pptx
Chàm - Bệnh án (da liễu - bvdlct ctump) .pptx
 
Trích dẫn trắc nghiệm tư tưởng HCM5.docx
Trích dẫn trắc nghiệm tư tưởng HCM5.docxTrích dẫn trắc nghiệm tư tưởng HCM5.docx
Trích dẫn trắc nghiệm tư tưởng HCM5.docx
 
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh lí
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh líKiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh lí
Kiểm tra chạy trạm lí thuyết giữa kì giải phẫu sinh lí
 
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...
Sáng kiến “Sử dụng ứng dụng Quizizz nhằm nâng cao chất lượng ôn thi tốt nghiệ...
 
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )
Bài giảng về vật liệu ceramic ( sứ vệ sinh, gạch ốp lát )
 
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...
Sáng kiến Dạy học theo định hướng STEM một số chủ đề phần “vật sống”, Khoa họ...
 
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...
10 ĐỀ KIỂM TRA + 6 ĐỀ ÔN TẬP CUỐI KÌ 2 VẬT LÝ 11 - KẾT NỐI TRI THỨC - THEO C...
 
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdf
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdfChuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdf
Chuong trinh dao tao Su pham Khoa hoc tu nhien, ma nganh - 7140247.pdf
 
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...
QUẢN LÝ HOẠT ĐỘNG GIÁO DỤC KỸ NĂNG SỐNG CHO HỌC SINH CÁC TRƯỜNG TRUNG HỌC CƠ ...
 

Oop unit 12 đồ họa và xử lý sự kiện

  • 1. Bài 12 Đồ họa và xử lý sự kiện Trịnh Thành Trung trungtt@soict.hust.edu.vn
  • 2. Nội dung 1. Giao diện đồ họa người sử dụng 2. AWT 3. Xử lý sự kiện 4. Swing
  • 3. Giao diện đồ họa người sử dụng Graphical User Interface (GUI) 1
  • 4. 4 Giao diện đồ họa người dùng • Giao diện đồ họa người sử dụng (Graphical user interface – GUI) • Giúp tạo ra các ứng dụng có giao diện đồ họa với nhiều các điều khiển như: Button, Textbox, Label, Checkbox, List, Tree...
  • 5. 5 Ví dụ • Giao diện trình duyệt web Button Title bar Menu bar Combo box Menus Scroll bar
  • 6. 6 • Java cung cấp hai thư viện đồ họa − AWT − Swing • AWT − Được cung cấp trong Java 1.0 • Swing − Nâng cấp các thành phần giao diện của AWT − Được tích hợp trong Java 1.2 Lập trình GUI trong Java
  • 7. 7 • Một số loại giao diện khác − Eclipse's Standard Widget Toolkit (SWT) − Google Web Toolkit (GWT) − Các thư viện đồ họa như Java bindings for OpenGL (JOGL) hay Java3D. Lập trình GUI trong Java
  • 9. 9 • AWT – Advanced Widget Toolkit • Các lớp AWT được Java cung cấp trong 12 gói − Các gói java.awt và java.awt.event được sử dụng chủ yếu − Độc lập nền và độc lập thiết bị • Các lớp cơ bản − Các thành phần GUI (vd. Button, TextField, Label…) − Các lớp GUI Container (vd. Frame, Panel, Dialog, ScrollPane…) − Layout managers (vd. FlowLayout, BorderLayout, GridLayout…) − Các lớp đồ họa (vd. Graphics, Color, Font…) AWT
  • 10. 10 • Các lớp xử lý sự kiện − Các lớp Event (vd. ActionEvent, MouseEvent, KeyEvent and WindowEvent…) − Các giao diện Event Listener (vd. ActionListener, MouseListener, KeyListener, WindowListener…) − Các lớp Event Listener Adapter (vd. MouseAdapter, KeyAdapter, WindowAdapter…) AWT (tiếp)
  • 11. 11 • 2 loại thành phần chính − Component: các thực thể GUI cơ bản (Button, Label, TextField.) − Container (Frame, Panel and Applet): Chứa các thực thể GUI. Một containter cũng có thể chứa các container khác. Các thành phần của AWT
  • 12. 12 • Top-level container: Frame, Dialog và Applet. • Frame: Cung cấp cửa sổ chính cho ứng dụng, chứa: − title bar (chứa biểu tượng, tiêu đề, các nút minimize, maximize & close) − menu bar − vùng hiển thị nội dung Top-level AWT container
  • 13. 13 • Dialog: còn gọi là "pop-up window“, chứa: − title-bar − vùng hiển thị nội dung • Applet: Ứng dụng Java có thể chạy trên trình duyệt Top-level AWT container
  • 14. 14 • Secondary Containers: Panel, ScrollPane − Đặt bên trong top-level container hoặc các secondary container khác • Panel − Một vùng hình chữ nhật nằm bên trong container − Sử dụng để áp dụng một layout cho các thành phần bên trong • ScrollPane: tạo ra một vùng có thể trượt dọc hoặc trượt ngang các thành phần bên trong Secondary AWT container
  • 15. 15 Cây phân cấp kế thừa của các lớp trong AWT Cây phân cấp kế thừa AWT
  • 16. 16 • Button, TextField, Label, Checkbox, CheckboxGroup (radio buttons), List, and Choice Các thành phần của AWT
  • 17. 17 • java.awt.Label: Hiển thị một nhãn văn bản • Phương thức khởi tạo − // Construct a Label with the given text String, of the text alignment − public Label(String strLabel, int alignment); − public Label(String strLabel); // Construct a Label with the given text − public Label(); // Construct an initially empty Label • Phương thức − public String getText(); − public void setText(String strLabel); − public int getAlignment(); − public void setAlignment(int alignment); Label
  • 18. 18 • Các bước để tạo một component và add vào container: − Khai báo và khởi tạo thành phần đó − Xác định container sẽ chứa thành phần này + Sử dụng phương thức add + VD: aContainer.add(aComponent) • Ví dụ Label lblInput; lblInput = new Label("Enter ID"); this.add(lblInput); lblInput.setText("Enter password"); lblInput.getText(); Add component vào container
  • 19. 19 • java.awt.Button: Kích hoạt một sự kiện khi nhấp chuột • Phương thức khởi tạo − public Button(String buttonLabel); − public Button(String buttonLabel); • Các phương thức − public String getLabel(); − public void setLabel(String buttonLabel); − public void setEnable(boolean enable); • Ví dụ Button btnColor = new Button("Red"); this.add(btnColor); ... btnColor.setLabel("green"); btnColor.getLabel(); Button
  • 20. 20 • java.awt.TextField: Ô văn bản để người dùng có thể nhập liệu trong một dòng (TextArea: nhiều dòng) • Phương thức khởi tạo − public TextField(String strInitialText, int columns); − public TextField(String strInitialText); − public TextField(int columns); • Các phương thức − public String getText(); − public void setText(String strText); − public void setEditable(boolean editable); TextField
  • 21. 21 • Layout: Sắp xếp các thành phần trong container • Các Layout manager trong AWT: (trong gói java.awt) − FlowLayout − GridLayout − BorderLayout − GridBagLayout − BoxLayout − CardLayout Quản lý bố cục
  • 22. 22 • Gọi đến phương thức setLayout() của container public void setLayout(LayoutManager mgr) • Các bước để thiết lập Layout trong Container − Khởi tạo đối tượng Layout tương ứng, vd. new FlowLayout() − Gọi đến phương thức setLayout với tham số là đối tượng vừa tạo − Gọi phương thức add của container theo thứ tự tương ứng • Ví dụ Panel p = new Panel(); p.setLayout(new FlowLayout()); p.add(new JLabel("One")); p.add(new JLabel("Two")); p.add(new JLabel("Three")); Thiết lập Layout Manager
  • 23. 24 • Với các container sử dụng FlowLayout: − Các thành phần được sắp xếp lần lượt từ trái sang phải − Khi đầy một dòng -> tạo dòng mới • Phương thức khởi tạo − public FlowLayout(); − public FlowLayout(int align); − public FlowLayout(int align, int hgap, int vgap); Align: + FlowLayout.LEFT (or LEADING) + FlowLayout.RIGHT (or TRAILING) + FlowLayout.CENTER hgap, vgap: khoảng cách dọc/ngang giữa các thành phần. − mặc định: hgap=5, vgap=5, align=CENTER FlowLayout
  • 24. 25 import java.awt.*; import java.awt.event.*; public class AWTFlowLayout extends Frame { public AWTFlowLayout () { setLayout(new FlowLayout()); add(new Button("Button 1")); add(new Button("This is Button 2")); add(new Button("3")); add(new Button("Another Button 4")); add(new Button("Button 5")); add(new Button("One More Button 6")); setTitle("FlowLayout"); // "this" Frame sets title setSize(280, 150); // "this" Frame sets initial size setVisible(true); // "this" Frame shows } public static void main(String[] args) { new AWTFlowLayout(); // Let the constructor do the job } } Ví dụ FlowLayout
  • 25. 26 • Với các container sử dụng FlowLayout: − Các thành phần được sắp xếp theo hàng và cột • Phương thức khởi tạo − public GridLayout(int rows, int columns); − public GridLayout(int rows, int columns, int hgap, int vgap); • mặc định: rows=1, cols=0, hgap=0, vgap=0 GridLayout
  • 26. 27 BorderLayout • Với BorderLayout, container được chia làm 5 phần: EAST, WEST, SOUTH, NORTH, CENTER • Phương thức khởi tạo • public BorderLayout(); • public BorderLayout(int hgap, int vgap); • mặc định hgap=0, vgap=0 • Khi thêm vào một thành phần • aContainer.add(acomponent, aZone) • aZone: • BorderLayout.NORTH (or PAGE_START) • BorderLayout.SOUTH (or PAGE_END) • BorderLayout.WEST (or LINE_START) • BorderLayout.EAST (or LINE_END) • BorderLayout.CENTER • aContainer.add(aComponent): thêm thành phần vào CENTER • Không bắt buộc phải thêm đủ thành phần vào cả 5 vùng
  • 27. Xử lý sự kiện Các giao diện listener và các cài đặt 3
  • 28. 29 • Xử lý sự kiện theo mô hình "hướng sự kiện" (event- driven) − Khi một sự kiện xảy ra -> gọi đến mã xử lý tương ứng • Các đối tượng: source, listener, event − source là đối tượng sinh ra các sự kiện (event) (ví dụ: khi người dùng tác động vào) − event thông điệp được gửi đến các đối tượng listener đã được đăng ký − Các phương thức xử lý sự kiện tương ứng của listener sẽ được gọi để xử lý các sự kiện • Các listener phải được đăng ký với các source để quản lý các sự kiện nhất định có thể xảy ra tại các source. Tổng quan
  • 30. • AWT cung cấp các lớp xử lý sự kiện trong java.awt.event Chiến lược xử lý sự kiện Các giao diện listener Phương thức đăng ký ActionListener addActionListener AdjustmentListener addAdjustmentListener ComponentListener addComponentListener ContainerListener addContainerListener FocusListener addFocusListener ItemListener addItemListener KeyListener addKeyListener MouseListener addMouseListener MouseMotionListener addMouseMotionListener TextListener addTextListener WindowListener addWindowListener
  • 31. 32 • ActionListener − Xử lý các nút và một số ít các hành động khác + actionPerformed(ActionEvent event) • AdjustmentListener − Áp dụng khi cuộn (scrolling) + adjustmentValueChanged(AdjustmentEvent event) • ComponentListener − Xử lý các sự kiện dịch chuyển/thay đổi kích thước/ẩn các đối tượng GUI + componentResized(ComponentEvent event) + componentMoved (ComponentEvent event) + componentShown(ComponentEvent event) + componentHidden(ComponentEvent event) Một số listener cơ bản
  • 32. 33 • ContainerListener − Được kích hoạt khi cửa sổ thêm/gỡ bỏ các đối tượng GUI + componentAdded(ContainerEvent event) + componentRemoved(ContainerEvent event) • FocusListener − Phát hiện khi nào các đối tượng có/mất focus + focusGained(FocusEvent event) + focusLost(FocusEvent event) Một số listener cơ bản
  • 33. 34 • ItemListener −Xử lý các sự kiện chọn trong các list, checkbox,... + itemStateChanged(ItemEvent event) • KeyListener −Phát hiện ra các sự kiện liên quan đến bàn phím + keyPressed(KeyEvent event) // any key pressed down + keyReleased(KeyEvent event) // any key released + keyTyped(KeyEvent event) // key for printable char released Một số listener cơ bản
  • 34. 35 • MouseListener −Áp dụng cho các sự kiện chuột cơ bản + mouseEntered(MouseEvent event) + mouseExited(MouseEvent event) + mousePressed(MouseEvent event) + mouseReleased(MouseEvent event) + mouseClicked(MouseEvent event) -- Nhả chuột khi không kéo • Áp dụng khi nhả chuột mà không di chuyển từ khi nhấn chuột • MouseMotionListener −Xử lý các sự kiện di chuyển chuột + mouseMoved(MouseEvent event) + mouseDragged(MouseEvent event) Một số listener cơ bản
  • 35. 36 • TextListener − Áp dụng cho các textfield và text area + textValueChanged(TextEvent event) • WindowListener − Xử lý các sự kiện mức cao của cửa sổ + windowOpened, windowClosing, windowClosed, windowIconified, windowDeiconified, windowActivated, windowDeactivated • windowClosing đặc biệt rất hữu dụng Một số listener cơ bản
  • 36. 37 • Xây dựng lớp listener tương ứng với sự kiện − Thực thi giao diện XxxListener • Cài đặt các phương thức tương ứng với sự kiện của giao diện này • Đăng ký các listener với nguồn − public void addXxxListener(XxxListener l); − public void removeXxxListener(XxxListener l); Chiến lược xử lý sự kiện
  • 37. • Giao diện MouseListener interface MouseListener { // Called back upon mouse-button pressed public void mousePressed(MouseEvent evt); // Called back upon mouse-button released public void mouseReleased(MouseEvent evt); // Called back upon mouse-button clicked (pressed and released) public void mouseClicked(MouseEvent evt); // Called back when mouse pointer entered the component public void mouseEntered(MouseEvent evt); // Called back when mouse pointer exited the component public void mouseExited(MouseEvent evt); } Ví dụ
  • 38. Xây dựng lớp Listener class MyMouseListener implements MouseListener { public void mousePressed(MouseEvent event) { System.out.println ("Mouse-button pressed at (" + event.getX() + "," + event.getY() + ")."); } public void mouseReleased(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} }
  • 39. import java.awt.*; public class ButtonEventExample extends Frame { public ButtonEventExample () { Button b = new Button("Button"); add(b); b.addMouseListener(new MyMouseListener()); setTitle(“Button Event Example"); setSize(280, 150); setVisible(true); } public static void main(String[] args) { new ButtonEventExample(); } } Đăng ký với button
  • 40. 41 • Nhược điểm của việc sử dụng giao diện XxxListener − Phải cài đặt tất cả các phương thức của giao diện − Nếu chỉ cần xử lý 1 sự kiện -> tạo ra rất nhiều phương thức rỗng • AWT cung cấp các lớp adapter cho các listener có nhiều hơn 1 phương thức • Để sử dụng các lớp này, ta viết các lớp kế thừa từ các lớp Adapter thay vì thực thi các giao diện Adapter
  • 41. Các giao diện listener Các lớp Adapter Phương thức đăng ký ActionListener addActionListener AdjustmentListener addAdjustmentListener ComponentListener ComponentAdapter addComponentListener ContainerListener ContainerAdapter addContainerListener FocusListener FocusAdapter addFocusListener ItemListener addItemListener KeyListener KeyAdapter addKeyListener MouseListener MouseAdapter addMouseListener MouseMotionListener MouseMotionAdapter addMouseMotionListener TextListener addTextListener WindowListener WindowAdapter addWindowListener Các Adapter tương ứng
  • 42. Ví dụ class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent event) { System.out.println("Mouse-button pressed at (" + event.getX() + "," + event.getY() + ")."); } } public class FrameEventExample extends Frame { public FrameEventExample () { addMouseListener(new MyMouseListener()); setTitle(“Button Event Example"); setSize(640, 480); setVisible(true); } }
  • 43. 44 • Nếu ClickListener muốn vẽ một hình tròn tại vị trí chuột được nhấn? − Tại sao không thể gọi phương thức getGraphics để lấy về đối tượng Graphics để vẽ • Cách 1 − Gọi event.getSource để lấy về một tham chiếu tới cửa sổ hoặc thành phần GUI tạo ra sự kiện − Ép kết quả trả về theo ý muốn − Gọi các phương thức trên tham chiếu đó Lấy đối tượng từ source
  • 44. public class CircleListener extends MouseAdapter { private int radius = 10; public void mousePressed(MouseEvent event) { Frame app = (Frame)event.getSource(); Graphics g = app.getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } } Cách 1: Sử dụng getSource
  • 45. Cách 1: Sử dụng getSource public class FrameEventExample extends Frame { private int radius = 10; public FrameEventExample() { addMouseListener(new CircleListener()); setSize(640, 480); setVisible(true); }
  • 46. Cách 2: Thực thi giao diện listener • Giúp frame hiện tại đóng vai trò như một listener import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class FrameEventExample extends Frame implements MouseListener { private int radius = 10; public FrameEventExample() { addMouseListener(this); }
  • 47. Cách 2: Thực thi giao diện listener public void mouseEntered(MouseEvent event) {} public void mouseExited(MouseEvent event) {} public void mouseReleased(MouseEvent event) {} public void mouseClicked(MouseEvent event) {} public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } }
  • 48. Cách 3: Sử dụng inner class • Viết lớp listener bên trong lớp frame import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class FrameEventExample extends Frame { public FrameEventExample() { addMouseListener(new CircleListener()); } private class CircleListener extends MouseAdapter { … } }
  • 49. Cách 4: Sử dụng anonymous inner class public class FrameEventExample extends Frame { public FrameEventExample() { addMouseListener (new MouseAdapter() { private int radius = 25; public void mousePressed(MouseEvent event) { Graphics g = getGraphics(); g.fillOval(event.getX()-radius, event.getY()-radius, 2*radius, 2*radius); } }); } }
  • 50. Ví dụ: Simple White Board import java.awt.*; import java.awt.event.*; public class SimpleWhiteboard extends Frame { protected int lastX=0, lastY=0; public SimpleWhiteboard() { setBackground(Color.white); setForeground(Color.blue); addMouseListener(new PositionRecorder()); addMouseMotionListener(new LineDrawer()); setSize(640, 480); setVisible(true); }
  • 51. Ví dụ: Simple White Board (tiếp) protected void record(int x, int y) { lastX = x; lastY = y; } private class PositionRecorder extends MouseAdapter { public void mouseEntered(MouseEvent event) { requestFocus(); record(event.getX(), event.getY()); } public void mousePressed(MouseEvent event) { record(event.getX(), event.getY()); } }
  • 52. Ví dụ: Simple White Board (tiếp) private class LineDrawer extends MouseMotionAdapter { public void mouseDragged(MouseEvent event) { int x = event.getX(); int y = event.getY(); Graphics g = getGraphics(); g.drawLine(lastX, lastY, x, y); record(x, y); } } }
  • 53. Ví dụ: Simple White Board (tiếp) public class WhiteBoardDemo { public static void main(String args[]) { new SimpleWhiteboard(); } }
  • 55. 56 • Cách đặt tên − Tất cả các thành phần trong swing đều có tên bắt đầu với chữ hoa J và tuân theo khuôn dạng JXxxx. Ví dụ: JFrame, JPanel, JApplet, JDialog, JButton,... • Các thành phần “nhẹ”? (lightweight) − Hầu hết các thành phần swing đều “nhẹ”, được tạo ra bằng cách vẽ trong cửa sổ cơ sở • Look and Feel mới (mặc định) − Có thể thay đổi Look and Feel tự nhiên (native look) • Không nên trộn cả swing và awt trong một cửa sổ. Swing vs. AWT
  • 60. 61 Thay đổi Look and Feel • Gọi phương thức setLookAndFeel public class WindowUtilities { public static void setNativeLookAndFeel() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch(Exception e) { System.out.println(“Co loi khi thay doi LAF: “+e); } } ...
  • 61. 62 • Các lớp container − JApplet, JFrame • Các thành phần Swing tương đương với các thành phần AWT − JLabel, JButton, JPanel, JSlider • Các thành phần Swing mới − JColorChooser, JInternalFrame, JOptionPane, JToolBar, JEditorPane • Các thành phần đơn giản khác − JCheckBox, JRadioButton, JTextField, JTextArea, JFileChooser Các thành phần Swing
  • 62. 63 • Content pane − A JApplet contains a content pane in which to add components. Changing other properties like the layout manager, background color, etc., also applies to the content pane. Access the content pane through getContentPane. • Layout manager − The default layout manager is BorderLayout (as with Frame and JFrame), not FlowLayout (as with Applet). BorderLayout is really layout manager of content pane. • Look and feel − The default look and feel is Java (Metal), so you have to explicitly switch the look and feel if you want the native look. JApplet
  • 63. import java.awt.*; import javax.swing.*; public class JAppletExample extends JApplet { public void init() { WindowUtilities.setNativeLookAndFeel(); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); } } JApplet
  • 64. 65 • JLabel − New features: HTML content images, borders • JButton − New features: icons, alignment, mnemonics • JPanel − New feature: borders • JSlider − New features: tick marks and labels Các thành phần tương tự AWT
  • 65. 66 • Main new feature: icons − Create an ImageIcon by passing the ImageIcon constructor a String representing a GIF or JPG file (animated GIFs!). − Pass the ImageIcon to the JButton constructor. • Other features − HTML content as with JLabel − Alignment: location of image with respect to text − Mnemonics: keyboard accelerators that let you use Alt- someChar to trigger the button. JButton
  • 66. import java.awt.*; import javax.swing.*; public class JButtons extends JFrame { public static void main(String[] args) { new JButtons(); } public JButtons() { super("Using JButton"); WindowUtilities.setNativeLookAndFeel(); addWindowListener(new ExitListener()); Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); Ví dụ JButton
  • 67. JButton button1 = new JButton("Java"); content.add(button1); ImageIcon cup = new ImageIcon("images/cup.gif"); JButton button2 = new JButton(cup); content.add(button2); JButton button3 = new JButton("Java", cup); content.add(button3); JButton button4 = new JButton("Java", cup); button4.setHorizontalTextPosition(SwingConstants.LEFT); content.add(button4); pack(); setVisible(true); } } Ví dụ JButton
  • 68. 69 • Very rich class with many options for different types of dialog boxes. • Five main static methods − JOptionPane.showMessageDialog + Icon, message, OK button − JOptionPane.showConfirmDialog + Icon, message, and buttons: OK, OK/Cancel, Yes/No, or Yes/No/Cancel − JOptionPane.showInputDialog (2 versions) + Icon, message, textfield or combo box, buttons − JOptionPane.showOptionDialog + Icon, message, array of buttons or other components JOptionPane
  • 70. 71 Các thành phần khác • JCheckBox • Note uppercase B (vs. Checkbox in AWT) • JRadioButton • Use a ButtonGroup to link radio buttons • JTextField • Just like AWT TextField except that it does not act as a password field (use JPasswordField for that) • JTextArea • Place in JScrollPane if you want scrolling • JFileChooser