You are on page 1of 136

L p trnh

h a v i AWT

CAO Duc Thong Thanglong University


thongcd@thanglong.edu.vn

N i dung
ng quan

AWT

Cc thnh ph n AWT
Qu n l trnh by
l

ki n

Th vi nAWT
AWT l vi t

AWT cho php

a Abstract Windowing Toolkit


o cc thnh ph n

AWT cho php nh n


Cc thnh ph n

li u
a

chu t, bn phm
n

a AWT

t ch a (Container)
Thnh ph n (Component)
Trnh qu n l trnh by (Layout Manager)
a (Graphics), phng ch (Font),

ki n (Event)

Th vi nAWT
Object

Color
Image
Toolkit

Event
Insets

Font
Polygon

Graphics
Rectangle

MenuComponent
MenuBar

XXXLayout
MenuItem
CheckboxGroup
Menu
Component
Checkbox
MenuItem

Th vi nAWT
Component
AWTException

AWTError

Button
Canvas
Checkbox

Applet
Panel
Frame

Choice
Window

Dialog

Container
ScrollPane
Label

FileDialog
List
Scrollbar
TextComponent

TextArea
TextField
5

Ccthnhph nAWT
Component (thnh ph n)
L
t
c th
L

t abstract superclass cho

i
ng
a c th hi n th
ng tc i ng i dng
u

c trn mn hnh v

t cc component

a AWT

Component: button, checkbox, scrollbar

ph

ng th c

getBackGround(): tr

getBounds(): tr
getFont(): tr

ph m vi
font hi n

getForeGround(): tr
getHeight(): tr

a Component

a Component (Rectangle)
i

mu

chi u cao

a Component

a Component
a Component
a Component (pixel, ki u int)
6

Ccthnhph nAWT
t

ph

ng th c

getSize(): tr

kch th

getWidth(): tr

chi u

getX(), getY(): tr

a Component
a Component (Dimenstion)

ng

a Component (int)

hi n

isEnable(): boolean
paint(Graphics): ch u trch nhi m hi n th component
repaint():

i giao di n cho component

setVisible(boolean): hi n th component

Ccthnhph nAWT
Container (v t ch a)
Ch a trong gi java.awt
L vng c th
t
C

t cc thnh ph n giao di n

lo i Container: Panel, Frame, Dialog


t Component c kh

ng ch a cc Component khc

thm
t Component vo Container ta
add(Component)
Container
Component

ng

t layout manager

ng ph
p

ng th c

p cc

Ccthnhph nAWT
Frame
Th a

Window nn

Frame

a l Component

ng l

t Container

a l Container

o Frame
Frame()
Frame(String title)
V

Ccthnhph nAWT
Frame
import java.awt.*;
public class UseLessFrame extends Frame {
public UseLessFrame(){
super("Useless Frame");
setSize(300,200);
setVisible(true);
}
public static void main(String[] args) {
UseLessFrame frame = new UseLessFrame();
}
}

10

Ccthnhph nAWT
Frame

11

X l ccs ki n
ki n l g

Khi ng

i dng th c hi n
c sinh ra.

Cc

ki n l cc

i tc
nhau

ng

a ng

t hnh
ng m

i dng

ng trn GUI,

nh ng g
o ra nh ng lo i

ki n

y ra
ki n khc

Kch chu t

Button

EventHandler

ActionEvent

actionPerformed(ActionEvent e){
//Do something
}
12

X l ccs ki n
L ngu n sinh ra
ki n.

Event Sources
ki n

c sinh ra

ng

t object, v

ActionEvent.

Event Handlers
Event handler l
event, gi i m v

t ph ng th c, n nh n
l cc
ng tc i ng

p ch a cc ph ng th c (event handler)
ki n (listener)
t
ki n

p nghe c th
n ph i ng k

t ngu n sinh ra
t

l
t
p nghe

t i
i dng
c

i l

ng
p nghe

ki n no , ngu n sinh ra

ki n c th c nhi u

p mu n l listener ph i implement

p nghe
t giao ti p thch

p
13

X l ccs ki n
V

ki n

import java.awt.*;
import java.awt.event.*;
class EventTest extends Frame implements ActionListener {
Label lab = new Label("Enter a number");
TextField tf1 = new TextField(5);
TextField tf2 = new TextField(5);
Button btnResult = new Button("Double is");
Button ext = new Button("exit");
public EventTest(String title) {
super(title);
setLayout(new FlowLayout());
btnResult.addActionListener(this);
ext.addActionListener(this);
add(lab); add(tf1); add(btnResult); add(tf2); add(ext);
}
14

X l ccs ki n
V

ki n

public void actionPerformed(ActionEvent ae) {


if (ae.getSource() == btnResult) {
int num = Integer.parseInt(tf1.getText()) * 2;
tf2.setText(String.valueOf(num));
}
if (ae.getSource() == ext) {
System.exit(0);
}
}
public static void main(String args[]) {
EventTest t = new EventTest("Event handling");
t.setSize(300, 200);
t.setVisible(true);
}
}
15

X l ccs ki n
V

ki n

16

X l ccs ki n
Cc lo i

ki n (Event

java.util.EventObject
java.awt.AWTEvent
java.awt.event

ActionEvent
AdjustmentEvent

FocusEvent

ComponentEvent

InputEvent

ItemEvent

ContainerEvent

TextEvent

WindowEvent

KeyEvent
MouseEvent

17

X l ccs ki n
M

ki n

18

X l ccs ki n
ActionListener

Cc lo i Listener

AdjustmentListener
E
v
e
n
t
L
i
s
t
e
n
e
r

ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
TextListener
WindowListener
19

X l ccs ki n
V

ng k

add + lo i
V

List

ng nghe
ki n + Listener(

i danh sch

ki n)

i nt Button

addActionListener(ActionListener)

addActionListener(A
ctionListener)
addItemListener(Ite
mListener)

20

X l ccs ki n
Ci

Xc

nh

ng

Xc nh
ki n c th
qu n l (object)
Xc nh i
ng ng
ng k

gy ra

ki n

ki n (source)

y ra

ng ng

ng nghe (listener) v ci
ng nghe cho

t qu n l

ng m ta

t cc ph

ng gy ra

ng th c

ki n

21

X l ccs ki n
V
ki n

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TestButton {
private Frame f;
private Button b;
public TestButton(){
f = new Frame("Test");
b = new Button("Press me");
b.setActionCommand("ButtonPressed");
}
public void init(){
b.addActionListener(new ButtonHandler());
f.add(b, BorderLayout.CENTER);
f.pack();
f.setVisible(true);

qu n l

X l ccs ki n

22

X l ccs ki n
V
ki n

qu n l

public static void main(String[] args){


TestButton test = new TestButton();
test.init();
}
}
class ButtonHandler implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Button's command is: "+
e.getActionCommand());
}
}

23

X l ccs ki n
V
ki n

qu n l

Button's command is: ButtonPressed


Button's command is: ButtonPressed
Button's command is: ButtonPressed

24

Ccthnhph nAWT
Frame dng

test cc thnh ph n khc

import java.awt.*;
import java.awt.event.*;
public class ComponentTestFrame extends Frame implements
WindowListener {
public ComponentTestFrame(String title){
super(title);
setBackground(SystemColor.control);
setSize(400,300);
setLocation(200,150);
setLayout(new FlowLayout());
addWindowListener(this);
}
25

Ccthnhph nAWT
Frame dng

test cc thnh ph n khc

public void windowClosing(WindowEvent e){


dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
}

26

Ccthnhph nAWT
t

ph

ng th c

a Frame

Frame()
Frame(String)
Image getIconImage()
MenuBar getMenuBar()
String getTitle()
Boolean isResizeable()
setIconImage(Image)
setMenuBar(MenuBar)
setTitle(String)
setVisible(boolean)
27

Ccthnhph nAWT
GUIFrame
import java.awt.*;
import java.awt.event.*;
public class GUIFrame extends Frame {
public GUIFrame(String title){
super(title);
setBackground(SystemColor.control);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}

28

Ccthnhph nAWT
GUIFrame
public void setVisible(boolean visible){
if(visible){
Dimension d =
Toolkit.getDefaultToolkit().getScreenSize();
setLocation((d.width - getWidth())/2, (d.height
-getHeight())/2);
}
super.setVisible(visible);
}
public static void main(String[] args){
GUIFrame frame = new GUIFrame("GUI Frame");
frame.setSize(400,300);
frame.setVisible(true);
}
}
29

Ccthnhph nAWT
GUIFrame

30

Ccthnhph nAWT
Label
Dng

hi n th

Cc ph

ng th c kh t

n trong

t Container

Label()
Label(String text)
Label(String text, alignment): alignment c th nh n cc gi
tr Label.LEFT, Label.RIGHT, Label.CENTER
Ph

ng th c khc
setFont(Font f)
setText(String s)
getText()
getAlignment()

31

Ccthnhph nAWT
Label
import java.awt.*;
public class LabelTest {
public LabelTest() {
Label l1 = new Label("Label");
Label l2 = new Label("I am a label");
l2.setFont(new Font("Timesroman", Font.BOLD, 18));
Label l3 = new Label();
l3.setText("I am disable");
l3.setEnabled(false);
Label l4 = new Label("Colored, right aligned", Label.RIGHT);
l4.setForeground(Color.green);
l4.setBackground(Color.black);
ComponentTestFrame frame = new
ComponentTestFrame("Label Test");
32

Ccthnhph nAWT
Label
frame.add(l1);
frame.add(l2);
frame.add(l3);
frame.add(l4);
frame.setVisible(true);
}
public static void main(String[] args) {
LabelTest lt = new LabelTest();
}
}

33

Ccthnhph nAWT
TextComponent

p cha
t

ph

a TextField v TextArea
ng th c

a TextComponent

getCaretPosition()
getSelectedText()
getSelectionStart()
getSelectionEnd()
getText(), setText()
select(int, int)
setCaretPosition(int)
setEditable(boolean)
setSelectionStart(int)
setSelectionEnd(int)
34

Ccthnhph nAWT
t

ng th c

TextField()

TextField
Ch ch a

ph

t dng

TextField(int columns)
TextField(String s)

TextField(String s, int columns)


addActionListener(ActionListener)
echoCharIsSet()
setEchoChar(char)
setText()
setColumn(int)

35

Ccthnhph nAWT
TextField
t

ph

ng th c

setEditable(boolean):
hay khng
isEditable(): xc

t ch

nh xem c

TextField c so n th o
ch

Editable khng

36

Ccthnhph nAWT
TextField
import java.awt.*;
public class TextFieldTest {
public TextFieldTest() {
super();
TextField tf1 = new TextField();
TextField tf2 = new TextField(25);
tf2.setText("Type stuff here");
tf2.setFont(new Font("Timesroman",Font.BOLD,18));
TextField tf3 = new TextField("I am disabled",15);
tf3.setEnabled(false);
TextField tf4 = new TextField("Colors");
tf4.setBackground(Color.BLACK);
tf4.setForeground(Color.WHITE);
TextField tf5 = new TextField("Not editable");
tf5.setEditable(false);
TextField tf6 = new TextField("I am selected text !!!");

tf6.select(5, 13);

Ccthnhph nAWT
37

Ccthnhph nAWT
TextField
TextField tf7 = new TextField("Caret here --><--");
TextField tf8 = new TextField("username",8);
TextField tf9 = new TextField("password",8);
tf9.setEchoChar('*');
ComponentTestFrame frame = new
ComponentTestFrame("TextField Test");
frame.add(tf1); frame.add(tf2); frame.add(tf3);
frame.add(tf4); frame.add(tf5); frame.add(tf6);
frame.add(tf7); frame.add(tf8); frame.add(tf9);
frame.setVisible(true);
tf7.setCaretPosition(14);
}
public static void main(String[] args) {
TextFieldTest test = new TextFieldTest();
}
}
38

Ccthnhph nAWT
TextField

39

Ccthnhph nAWT
TextArea
Hi n th

n c nhi u

i TextArea c
t

ph

t dng

t Scrollbar

ng th c kh i

TextArea()
TextArea(int rows, int columns)
TextArea(String text)
TextArea(String text, int rows, int columns)
TextArea(String text, int rows, int columns, int ScrollType)

40

Ccthnhph nAWT
TextArea
t

ph

ng th c th

ng dng

setText/getText
get/set row/column
setEditable/isEditable
append(String)
insert(String s, int i): chn chu i vo
replaceRange(String, int, int): thay th
int v int cho tr c

tr
n

m gi a

tr

41

Ccthnhph nAWT
TextArea
import java.awt.*;
public class TextAreaTest {
public TextAreaTest() {
super();
TextArea ta1 = new TextArea(10,20);
TextArea ta2 = new TextArea("Text Area\n with
color",10,10,TextArea.SCROLLBARS_NONE);
ta2.setFont(new Font("Timesroman",Font.ITALIC,12));
ta2.setBackground(Color.BLACK);
ta2.setForeground(Color.GREEN);
TextArea ta3 = new TextArea("This textarea is not
editable",
10,15,TextArea.SCROLLBARS_HORIZONTAL_ONLY);
ta3.setEditable(false);
TextArea ta4 = new TextArea("This textarea is not enable",
4,25,TextArea.SCROLLBARS_NONE);
42

Ccthnhph nAWT
TextArea
ta4.setEditable(false);
ComponentTestFrame frame = new
ComponentTestFrame("TextArea Test");
frame.add(ta1); frame.add(ta2);
frame.add(ta3); frame.add(ta4);
frame.setVisible(true);
}
public static void main(String[] args) {
TextAreaTest test = new TextAreaTest();
}
}

43

Ccthnhph nAWT
TextArea

44

Ccthnhph nAWT
Button
ng tc

i ng

Th c hi n
t

ph

i dng

t hnh

ng no khi ng

i dng nh n nt

ng th c

Button()
Button(String text)
addActionListener(ActionListener)
String getLabel()
setLabel(String)
removeActionListener(ActionListener)

45

Ccthnhph nAWT
Button
import java.awt.Frame;
import java.awt.*;
public class ButtonTest {
public ButtonTest() {
Button b1 = new Button("Button");
Button b2 = new Button();
b2.setLabel("Press me!");
b2.setFont(new Font("Timesroman", Font.BOLD, 16));
Button b3 = new Button("Can't press me");
b3.setEnabled(false);
Button b4 = new Button("Colors");
b4.setForeground(Color.green);
b4.setBackground(Color.black);
ComponentTestFrame frame = new
ComponentTestFrame("Button Test");
46

Ccthnhph nAWT
Button
frame.add(b1);
frame.add(b2);
frame.add(b3);
frame.add(b4);
frame.setVisible(true);
}
public static void main(String[] args) {
ButtonTest lt = new ButtonTest();
}
}

47

Ccthnhph nAWT
Checkbox v RadioButton
Checkbox cho php ng
C th ch n nhi u option

i dng ch n

t hay nhi u ty ch n

a Checkbox

RadioButton ng gi ng nh Checkbox nh ng ch cho php ch n


t option i
t th i
m
Thnh ph n Checkbox c th dng
o RadioButton)
t

ph

p ph (CheckboxGroup

ng th c

Checkbox()
Checkbox(String)
Checkbox(String, boolean)
Checkbox(String, boolean, CheckboxGroup)

48

Ccthnhph nAWT
Checkbox v RadioButton
t

ph

ng th c

addItemListener(ItemListener)
set/getCheckboxGroup()
set/getLabel()
set/getState()
removeItemListener(ItemListener)

49

Ccthnhph nAWT
Checkbox v RadioButton
import java.awt.Checkbox;
public class CheckboxTest {
public CheckboxTest() {
super();
Checkbox cb1 = new Checkbox("Java",false);
Checkbox cb2 = new Checkbox("C++",false);
cb2.setEnabled(false);
Checkbox cb3 = new Checkbox("HTML",true);
Checkbox cb4 = new Checkbox();
cb4.setLabel("Pascal"); cb4.setState(false);
ComponentTestFrame frame = new
ComponentTestFrame("CheckboxTest");
frame.add(cb1); frame.add(cb2);
frame.add(cb3); frame.add(cb4);
frame.setVisible(true);
}
50

Ccthnhph nAWT
Checkbox v RadioButton
public static void main(String[] args) {
CheckboxTest test = new CheckboxTest();
}
}

51

Ccthnhph nAWT
Checkbox v RadioButton
import java.awt.*;
public class CheckboxGroupTest {
public CheckboxGroupTest() {
super();
CheckboxGroup group = new CheckboxGroup();
Checkbox cb1 = new Checkbox("Java",false,group);
Checkbox cb2 = new Checkbox("C++",false,group);
cb2.setEnabled(false);
Checkbox cb3 = new Checkbox("HTML",true,group);
Checkbox cb4 = new Checkbox("",true,group);
cb4.setLabel("Pascal");
ComponentTestFrame frame = new
ComponentTestFrame("CheckboxGroupTest");
frame.add(cb1); frame.add(cb2);
frame.add(cb3); frame.add(cb4);
52

Ccthnhph nAWT
Checkbox v RadioButton
frame.setVisible(true);
}
public static void main(String[] args) {
CheckboxGroupTest test = new CheckboxGroupTest();
}
}

53

Ccthnhph nAWT
Choice

Cho php
t

ph

a ch n trn

t danh sch cc thnh ph n

ng th c

Choice()
add(String)
addItem(String)
addItemListener(ItemListener)
getItem(int)
getItemCount()
getSelectedIndex()
insert(String, int)

54

Ccthnhph nAWT
remove(int)

Choice
t

remove(String)
ph

ng th c

removeAll()

removeItemListener(ItemListener)
select(int)
select(String)

55

Ccthnhph nAWT

import java.awt.*;
public class ChoiceTest {
public ChoiceTest() {
super();

Choice
Choice c1 = new Choice();
c1.add("Soup");
c1.add("Salad");
Choice c2 = new Choice();
c2.add("Java");
c2.add("C++");
c2.add("HTML");
c2.add("JavaScript");
c2.add("ADA");
Choice c3 = new Choice();
c3.add("One");
c3.add("Two");
c3.add("Three");

56

Ccthnhph nAWT
Choice

c3.setFont(new Font("Timesroman",Font.BOLD,18));
Choice c4 = new Choice();
c4.add("Computer");
c4.setEnabled(false);
ComponentTestFrame frame = new
ComponentTestFrame("ChoiceTest");
frame.add(c1);
frame.add(c2);
frame.add(c3);
frame.add(c4);
frame.setVisible(true);
}
public static void main(String[] args) {
ChoiceTest test = new ChoiceTest();
}
}
57

Ccthnhph nAWT
Choice

58

Ccthnhph nAWT
List

Gi ng nh Choice nh ng cho php hi n th nhi u


lc
Ng

i dng c th

a ch n cng

a ch n nhi u item

Tham kh o API Documentation

59

Ccthnhph nAWT
Canvas

L khu
a ng

c cho php hi n th hnh nh ho c nh n


i dng

Mu n
t
th g trn Canvas ta ph i ci
paint(Graphics)

cc
i ph

ki n
ng th c

V
import java.awt.*;
public class CanvasTest extends Canvas {
public void paint(Graphics g){
setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 16));
g.drawString("Canvas", 15, 25);
}

60

Ccthnhph nAWT
Canvas

public static void main(String[] args) {


CanvasTest c1 = new CanvasTest();
c1.setSize(100,100);
CanvasTest c2 = new CanvasTest();
c2.setSize(100,100);
c2.setBackground(Color.orange);
c2.setForeground(Color.blue);
CanvasTest c3 = new CanvasTest();
c3.setSize(200,50);
c3.setBackground(Color.white);
ComponentTestFrame frame = new ComponentTestFrame
("Canvas Test");
frame.add(c1);frame.add(c2);frame.add(c3);
frame.pack();
frame.setVisible(true);
}
}
61

Ccthnhph nAWT
ng quan

Menu

MenuBar

Menu

Separator
MenuItem

62

Ccthnhph nAWT
o MenuBar

ng quan

Menu

MenuBar menuBar = new MenuBar();


n MenuBar vo

myFrame.setMenuBar(menuBar);
o Menu
Menu fileMenu = new Menu(File);
n Menu vo MenuBar
menuBar.add(fileMenu);
o MenuItem
MenuItem fileOpen = new MenuItem(Open);

63

Ccthnhph nAWT

fileMenu.add(fileOpen);

ng quan

Menu

n MenuItem vo Menu

ng phn cch

fileMenu.addSeparator();

64

Ccthnhph nAWT
set/getLabel();

MenuItem
MenuItem()

t)

get/setShortcut(MenuShortcu

MenuItem(String)

isEnabled()

MenuItem(String, MenuShortcut)

setEnabled()

addActionListener(ActionListener)

65

Ccthnhph nAWT
V

Menu

import java.awt.*;
import java.awt.event.KeyEvent;
public class MenuTest {
public MenuTest() {
super();
MenuItem fileNew = new MenuItem("New");
fileNew.setShortcut(new
MenuShortcut(KeyEvent.VK_N));
MenuItem fileOpen = new MenuItem("Open");
fileOpen.setShortcut(new
MenuShortcut(KeyEvent.VK_O));
MenuItem fileSave = new MenuItem("Save");
fileSave.setShortcut(new
MenuShortcut(KeyEvent.VK_S));
fileSave.setEnabled(false);
MenuItem fileSaveAs = new MenuItem("Save As");
66

Ccthnhph nAWT
V

Menu

fileSaveAs.setShortcut(new
MenuShortcut(KeyEvent.VK_A));
fileSaveAs.setEnabled(false);
MenuItem fileExit = new MenuItem("Exit");
fileExit.setShortcut(new
MenuShortcut(KeyEvent.VK_X));
MenuItem editCut = new MenuItem("Cut");
editCut.setShortcut(new
MenuShortcut(KeyEvent.VK_X));
MenuItem editCopy = new MenuItem("Copy");
editCopy.setShortcut(new
MenuShortcut(KeyEvent.VK_C));
MenuItem editPaste = new MenuItem("Paste");
editPaste.setShortcut(new
MenuShortcut(KeyEvent.VK_P));
Menu file = new Menu("File");
67

Ccthnhph nAWT
V

Menu

file.add(fileNew);file.add(fileOpen);
file.add(fileSave);file.add(fileSaveAs);
file.addSeparator();file.add(fileExit);
Menu edit = new Menu("Edit");
edit.add(editCut); edit.add(editCopy);
edit.add(editPaste);
MenuBar menuBar = new MenuBar();
menuBar.add(file); menuBar.add(edit);
ComponentTestFrame frame = new
ComponentTestFrame("MenuTest");
frame.setMenuBar(menuBar);
frame.setVisible(true);

}
public static void main(String[] args) {
MenuTest test = new MenuTest();
}

Ccthnhph nAWT

68

Ccthnhph nAWT
V

Menu

69

Ccthnhph nAWT
L

p con

a Menu

PopupMenu
i

ng PopupMenu c th hi n th m khng

C th hi n th

tr

theo yu

n MenuBar

import java.awt.*;
import java.awt.event.KeyEvent;
public class PopupMenuTest {
public PopupMenuTest() {
super();
MenuItem cut = new MenuItem("Cut");
cut.setShortcut(new MenuShortcut(KeyEvent.VK_X));
MenuItem copy = new MenuItem("Copy");
copy.setShortcut(new
MenuShortcut(KeyEvent.VK_C));
MenuItem paste = new MenuItem("Paste");
70

Ccthnhph nAWT
PopupMenu

paste.setShortcut(new
MenuShortcut(KeyEvent.VK_P));
MenuItem delete = new MenuItem("Delete");
PopupMenu popupMenu = new
PopupMenu("Clipboard");
popupMenu.add(cut);popupMenu.add(copy);
popupMenu.add(paste);popupMenu.add(delete);
ComponentTestFrame frame = new
ComponentTestFrame("PopupMenuTest");
frame.add(popupMenu);frame.setVisible(true);
popupMenu.show(frame,50,50);
}
public static void main(String[] args) {
PopupMenuTest test = new PopupMenuTest();
}
}
71

Ccthnhph nAWT
PopupMenu

72

Ccthnhph nAWT
Panel
L
Ph i

t Container nh ng khng th
n Panel vo

Dng Panel

t Frame

nhm cc thnh ph n GUI

i nhau

import java.awt.*;
public class PanelTest {
public PanelTest() {
super();
Label lb1 = new Label("URL: ");
TextField tf1 = new TextField("",20);
Button b1 = new Button("Go");
Panel p1 = new Panel();
p1.setBackground(Color.CYAN);
p1.add(lb1); p1.add(tf1); p1.add(b1);
CheckboxGroup group = new CheckboxGroup();
73

Ccthnhph nAWT
Panel
Checkbox cb1 = new Checkbox("Java",group,true);
Checkbox cb2 = new Checkbox("C++",group,false);
Checkbox cb3 = new Checkbox("HTML",group,false);
Checkbox cb4 = new Checkbox("ADA",group,false);
Panel p2 = new Panel();
p2.setBackground(Color.BLUE);
p2.add(cb1); p2.add(cb2); p2.add(cb3); p2.add(cb4);
ComponentTestFrame frame = new
ComponentTestFrame("PanelTest");
frame.add(p1); frame.add(p2);
frame.setVisible(true);
}
public static void main(String[] args) {
PanelTest test = new PanelTest();
}
}

Ccthnhph nAWT

74

Ccthnhph nAWT
V

Panel

75

Ccthnhph nAWT
Dialog
L

c thanh tiu

Th

ng dng
t Dialog ph i

ng bin

cho php nh p
n

i trong

li u
t Frame ho c Dialog khc

t Dialog c th l modal ho c non-modal


Modal dialog
Non-modal dialog

76

Ccthnhph nAWT
Dialog
t

ph

ng th c

Dialog(Dialog)
Dialog(Dialog, String)
Dialog(Dialog, String, boolean)
Dialog(Frame)
Dialog(Frame, String)
Dialog(Frame, String, boolean)

77

Ccthnhph nAWT
V

Dialog

import java.awt.*;
import java.awt.event.*;
public class DialogTest implements WindowListener {
public DialogTest() {
super();
ComponentTestFrame frame = new
ComponentTestFrame("DialogTest");
Dialog d1 = new Dialog(frame,"Modal Dialog Test",true);
d1.add(new Label("Modal Dialog Test"));
d1.addWindowListener(this);
Dialog d2 = new Dialog(frame,"NonModal Dialog
Test",false);
d2.addWindowListener(this);
d2.add(new Label("NonModal Dialog Test"));
frame.setLocation(100,100);
78

Ccthnhph nAWT
V

Dialog
frame.setVisible(true);
d2.pack();
d2.setLocation(250,280);
d2.setVisible(true);
d1.pack();
d1.setLocation(220,170);
d1.setVisible(true);
}
public void windowOpened(WindowEvent arg0) {
}
public void windowClosing(WindowEvent arg0) {
((Dialog)arg0.getSource()).setVisible(false);
}
public void windowClosed(WindowEvent arg0) {
}
79

Ccthnhph nAWT
V

Dialog
public void windowIconified(WindowEvent arg0) {
}
public void windowDeiconified(WindowEvent arg0) {
}
public void windowActivated(WindowEvent arg0) {
}
public void windowDeactivated(WindowEvent arg0) {
}
public static void main(String[] args) {
DialogTest test = new DialogTest();
}

80

Ccthnhph nAWT
V

Dialog

81

Qu nl trnh by
Layout manager: qu n l cch trnh by
components trong
t Container

a cc GUI

Cc layout manager
FlowLayout
BorderLayout
CardLayout
GridLayout
GridBagLayout

ng ph ng th c setLayout()
thay i layout

t Container

82

Qu nl trnh by
FlowLayout
L layout

nh cho Applet v Panel

Cc thnh ph n
Cc hm kh i

tri sang ph i, trn xu ng

FlowLayout()
FlowLayout(int alignment)
Alignment c th l FlowLayout.CENTER, LEFT, RIGHT
FlowLayout(int, int, int)

83

Qu nl trnh by
FlowLayout
import java.awt.*;
import java.awt.event.*;
public class FlowLayoutTest extends Frame implements WindowListener {
public FlowLayoutTest(String title){
super(title);
addWindowListener(this);
setLayout(new FlowLayout(FlowLayout.CENTER, 20, 50));
for(int i = 1; i<15; i++)
add(new Button("Button"+i));
setSize(575, 300);
setVisible(true);
}
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
84

Qu nl trnh by
FlowLayout
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public static void main(String[] args) {
FlowLayoutTest ft = new FlowLayoutTest("FlowLayout Test");
}
}

85

Qu nl trnh by
FlowLayout

86

Qu nl trnh by
BorderLayout
L layout

nh cho Frame, Window, Dialog

Cc thnh ph n
c p p trn 5 khu c khc nhau
NORTH
t
nh a container.
EAST
t pha bn ph i a container.x`
SOUTH
t pha
i a container.
WEST
t pha bn tri a container.
CENTER
t gi a a container.

thm
t thnh ph n vo vng North
Button b1=new Button(North Button); // khai bo thnh ph n
setLayout(new BorderLayout()); // thi t p layout
add(b1,BorderLayout.NORTH); // thm thnh ph n vo layout
Kh i

o: BorderLayout(), BorderLayout(int, int)

87

Qu nl trnh by

BorderLayout

88

Qu nl trnh by
CardLayout
tm hi u trong API Documentation

89

Qu nl trnh by
GridLayout
Chia Container thnh cc
Cc Component
i

ng nhau

t trong cc

i nn ch a t nh t

t thnh ph n

V
GridLayout l1 = new GridLayout(4, 3)
u new GridLayout(0,3) ngh a l c 3
Khng

Cc hm kh i

t,

hng l b t

d ng new GridLayout(0,0);
o:

GridLayout(int), GridLayout(int, int), GridLayout(int, int, int


,int)

90

Qu nl trnh by
GridLayout

import java.awt.*;
import java.awt.event.*;
public class GridLayoutTest extends Frame implements WindowListener {
public GridLayoutTest(String title){
super(title);
addWindowListener(this);
setLayout(new GridLayout(0, 3, 5, 10));
for(int i = 1; i<15; i++)
add(new Button("Button"+i));
pack();
setVisible(true);
}

91

Qu nl trnh by
GridLayout

public void windowClosing(WindowEvent e){


dispose();
System.exit(0);
}
public void windowActivated(WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowIconified(WindowEvent e){}
public void windowDeiconified(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}
public void windowOpened(WindowEvent e){}
public static void main(String[] args) {
GridLayoutTest ft = new GridLayoutTest(GridLayout Test");
}
}
92

Qu nl trnh by
GridLayout

93

Qu nl trnh by
GridBagLayout
c

94

X l ccs ki n
WindowEvent
Ci

t giao ti p WindowListener

Xem v

Frame

95

X l ccs ki n
WindowAdapter

FocusAdapter

WindowEvent
Ci

t giao ti p WindowListener

Xem v

Adapter class

Frame

KeyAdapter
MouseAdapter
MouseMotionAdapter

96

X l ccs ki n
ActionEvent

c pht sinh

i Button, MenuItem, TextField, List

p nghe ci t giao ti p ActionListener hay ci


actionPerformed(ActionEvent)
t

bi n & ph

ng th c

t ph

ng th c

a ActionEvent

int ALT_MASK: phm ALT c

c nh n ?

int CTRL_MASK: phm CTRL c

c nh n ?

int SHIFT_MASK: phm SHIFT c


int getModifiers(): c th tr
String getActionCommand(): tr
ActionEvent

c nh n ?

ALT_MASK, CTRL_MASK
command

97

X l ccs ki n
ActionEvent

import java.awt.*;
import java.awt.event.*;

class ActionListenerTest extends GUIFrame implements ActionListener {


Panel controlPanel, whoDoneItPanel, commandPanel;
MenuBar menuBar;
Menu menu;
MenuItem menuItem;
Button button;
List list;
Label whoDoneItLabel, commandLabel;
TextField whoDoneItTextField, commandTextField, textField;
public ActionListenerTest(){
super("ActionListener Test");
//create menu bar
menuBar = new MenuBar();
menu = new Menu("A Menu");

98

X l ccs ki n
ActionEvent

Label.RIGHT);

menuItem = new MenuItem("A Menu Item",new


MenuShortcut(KeyEvent.VK_M));
menuItem.addActionListener(this);
menu.add(menuItem);
menuBar.add(menu);
setMenuBar(menuBar);
//create whoDoneItPanel
whoDoneItPanel = new Panel();
whoDoneItPanel.setBackground(Color.pink);
whoDoneItLabel = new Label("Who done it",
whoDoneItTextField = new TextField("A TextField");
//whoDoneItTextField.addActionListener(this);
whoDoneItTextField.setEditable(false);
whoDoneItPanel.add(whoDoneItLabel);
whoDoneItPanel.add(whoDoneItTextField);
add(whoDoneItPanel,BorderLayout.NORTH);

X l ccs ki n
9
9

X l ccs ki n
ActionEvent
//create controlPanel
controlPanel = new Panel();
controlPanel.add(new Label("A TextField", Label.RIGHT));
textField = new TextField(15);
textField.addActionListener(this);
controlPanel.add(textField);
button = new Button("A Button");
button.addActionListener(this);
button.setActionCommand("My Action Commmand");
controlPanel.add(button);
controlPanel.add(new Label("A List",Label.RIGHT));
list = new List(5,false);
list.add("Breakfast");
list.add("Lunch");
list.add("Diner");

ck"); list.add("Dessert");

X l ccs ki n

100

X l ccs ki n
ActionEvent
list.add("Brunch");
list.addActionListener(this);
controlPanel.add(list);
add(controlPanel, BorderLayout.CENTER);
//create commandPanel
commandPanel = new Panel();
commandLabel = new Label("Action Command");
commandPanel.setBackground(Color.pink);
commandTextField = new TextField(15);
commandTextField.setEditable(false);
commandPanel.add(commandLabel);
commandPanel.add(commandTextField);
add(commandPanel,BorderLayout.SOUTH);
pack();
setVisible(true);
101

X l ccs ki n

102

X l ccs ki n
ActionEvent

public void actionPerformed(ActionEvent e){


if(e.getSource()==menuItem){
whoDoneItTextField.setText("A MenuItem");
}else if(e.getSource()==textField){
whoDoneItTextField.setText("A TextField");
}else if(e.getSource()==button){
whoDoneItTextField.setText("A Button");
}else if(e.getSource()==list){
whoDoneItTextField.setText("A List");
}
commandTextField.setText(e.getActionCommand());
}
public static void main(String[] args){
ActionListenerTest test = new ActionListenerTest();
}
}
103

X l ccs ki n
ActionEvent

104

X l ccs ki n
ItemEvent

c o ra
Choice, List

cc thnh ph n cho php

p nghe ItemEvent

n ci

Ph

ng th c

n ci

Ph

ng th c

a ItemEvent

a ch n nh Checkbox,

t giao ti p ItemListener

t: itemStateChanged(ItemEvent)

int getStateChange(): c th nh n ItemEvent.SELECTED ho c


ItemEvent.DESELECTED
Object getItem(): item thay i tr ng thi (Checkbox,
Choice ho c item
c ch n a List)

105

X l ccs ki n
TextEvent

o ra

p nghe ci
Ph

ng th c

i TextComponent (TextField, TextArea)


t giao ti p TextListener
n ci

t textValueChanged(TextEvent)

TextEvent
c sinh ra khi gi tr text
i (thm, xa text)

a TextComponent thay

106

X l ccs ki n
Ph ng th c
MouseEvent

MouseEvent
c

o ra

p nghe ci

i chu t

a ng

t giao ti p

MouseListener
MouseMotionListener

i dng

int getClickCount()
Point getPoint()
int getX()
int getY()

107

X l ccs ki n
Cc ph ng th c
MouseListener

MouseEvent

void mouseClicked(MouseEvent)
void mouseEnteredMouseEvent)
void mouseExited(MouseEvent)
void mousePressed(MouseEvent)
void mouseReleased(MouseEvent)
Cc ph

ng th c

a MouseMotionListener

void mouseMoved(MouseEvent)
void mouseDragged(MouseEvent)

108

X l ccs ki n
MouseEvent

import java.awt.event.*;
public class MouseTest extend
GUIFrame

import java.awt.*;
implements MouseListener, MouseMotionListener {
Canvas canvas;
Label location, event;
public MouseTest(){
super("Mouse Event Test");
canvas = new Canvas();
canvas.setBackground(Color.white);
canvas.setSize(450,450);
canvas.addMouseListener(this);
canvas.addMouseMotionListener(this);
add(canvas, BorderLayout.CENTER);
Panel infoPanel = new Panel();
infoPanel.setLayout(new GridLayout(0, 2, 10, 0));
location = new Label("Location: ");

108

X l ccs ki n
MouseEvent
infoPanel.add(location);
event = new Label("Event: ");
infoPanel.add(event);
add(infoPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
public static void main(String[] args) {
new MouseTest();
}
public void mouseClicked(MouseEvent e){
String text = "Event: Clicked Button ";
switch(e.getModifiers()){
case InputEvent.BUTTON1_MASK:
text += 1;

X l ccs
case InputEvent.BUTTON2_MASK:

ki n

109

X l ccs ki n
MouseEvent
text += 2;
break;
case InputEvent.BUTTON3_MASK:
text += 3;
break;
}
text += " (" + e.getClickCount() + "x)";
event.setText(text);
}
public void mouseEntered(MouseEvent e){
event.setText("Event: Entered");
}
public void mouseExited(MouseEvent e){
event.setText("Event: Exited");
}

X l ccs ki n
110

X l ccs ki n
MouseEvent

public void mousePressed(MouseEvent e){


event.setText("Event: Pressed");
}
public void mouseReleased(MouseEvent e){
event.setText("Event: Released");
}
public void mouseMoved(MouseEvent e){
location.setText("Location: (" + e.getX()+"," +
e.getY()+")");
}
public void mouseDragged(MouseEvent e){
Point p = e.getPoint();
event.setText("Event: Dragged");
location.setText("Location: (" + p.getX()+"," +
p.getY()+")");
}
}

111

X l ccs ki n
MouseEvent

112

X l ccs ki n

void
keyTyped(KeyEvent)

KeyEvent
c

o ra khi ng

p nghe ci
Cc ph

i dng g bn phm

t giao ti p KeyListener

ng th c

a KeyListener

void keyPressed(KeyEvent)
void keyReleased(KeyEvent)

t
ph ng th c
a KeyEvent
char getKeyChar()
int getKeyCode()
String
getKeyText(int
keyCode)

113

nh

N p nh
Image image = Toolkit.getDefaultToolkit().
getImage(fileName);
import java.awt.*;
public class ImageTest extends Canvas {
public ImageTest(){
super();
setSize(300,200);
setBackground(Color.white);
}
public void paint(Graphics g){
Image image =
Toolkit.getDefaultToolkit().getImage("image.jpg");
114

g.drawImage(image, 50,50, this);

nh

N p nh

115

nh

N p nh
public static void main(String[] args) {
ImageTest image = new ImageTest();
GUIFrame frame = new GUIFrame("ImageTest");
frame.add(image);
frame.pack();
frame.setVisible(true);
}
}

116

nh

N p nh

117

CU H I

Bi t p
o ch ng trnh (Frame) c ch a
t Canvas, kch
th c 200, 200 mu . Khi a chu t vo Canvas
ch ng trnh hi n dng ch In canvas, khi a chu t ra
ngoi
hi n dng ch Not in canvas
Vi t ch ng trnh cho php
ng trn i
t
t
m ng i dng nh n chu t trn mn hnh

tr

Vi t
t ch ng trnh i menu Draw c 3 menu item:
line, circle, square: khi ng i dng kch vo ng menu
item, 50 line ho c circle ho c square
c
ra

118

Bi t p
Vi t

t ch

ng trnh tnh ton

n gi n

119

Bi t p
Vi t ch ng trnh cho php ng i dng
u khi n
t
qu bng. Trn mn hnh c cc nt l: To, Nh , Tri,
Ph i, Ln, Xu ng. Khi ng i dng n 1 nt th kch /v
tr a qu bng
thay i theo. Yu u o
t p
Ball ring bi t. (M
ng bi ton cho tr ng p ng i
dng nh n chu t tr c ti p trn mn hnh)
Vi t ch ng trnh m tr ch i d mn. Trn mn hnh
c 3x3 nt m v
i nt c th l c mn ho c khng
(ng u nhin). Khi ng i dng nh n
t nt, u nt
khng c mn th cho php ng i dng n ti p, cn khng
th thng bo mn v ng i. u l
i nt c
t
v ng i dng c th nh n phm
ng ng
thay v nh n chu t vo nt.
120

Tham kh o
ki n v

ng gy ra

ki n

121

Tham kh o
ki n v

ng gy ra

ki n

122

Tham kh o
i

ng nghe v ph

ng th c

n ci

123

Tham kh o
i

ng nghe v ph

ng th c

n ci

124

Tham kh o
Listener v cc thnh ph n

ng ng
ItemListener

WindowListener
Choice

Dialog
Checkbox

ActionListener

Frame

List

Button
List
MenuItem
TextField

125

Tham kh o
Listener cho Component
Component

ComponentListener
FocusListener
KeyListener
MouseListener
MouseMotionLIstener

126

You might also like