Professional Documents
Culture Documents
HCM
NHP MN JAVA
BI 6
LP TRNH S KIN
GING VIN:
V TN DNG
NI DUNG TRNH BY
Cc v d m u
M hnh x l s kin
Cc component nng cao
X l s kin chut
X l s kin bn phm
GV: V Tn Dng
PHN 1
GV: V Tn Dng
CC V D
M U
V D 1
GV: V Tn Dng
V D 1 (file MyFirstAwt.java)
MyFirstAwt()
{
this.setTitle("My First Awt"); //super("My First Awt");
this.setLayout(new FlowLayout());
this.add(button1);
this.add(button2);
this.add(button3);
status = new Label();
status.setText("Press any button, please!");
this.add(status);
//xem tip slide tip theo
GV: V Tn Dng
import java.awt.*;
import java.awt.event.*;
public class MyFirstAwt extends Frame
{
Label status;
Button button1 = new Button("Red");
Button button2 = new Button("Green");
Button button3 = new Button("Blue");
V D 1 (file MyFirstAwt.java) - tt
button1.addActionListener(new MyListener(status,this));
button2.addActionListener(new MyListener(status,this));
button3.addActionListener(new MyListener(status,this));
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt){System.exit(0);}
});
}
GV: V Tn Dng
V D 1 (file MyListener.java)
import java.awt.*;
import java.awt.event.*;
public class MyListener implements ActionListener
{
Label status;
Component compo;
MyListener(Label status1, Component compo1)
{
this.status = status1;
this.compo = compo1;
GV: V Tn Dng
}
//xem tip slide tip theo
GV: V Tn Dng
V D 1 (file MyListener.java) - tt
V D 2
GV: V Tn Dng
10
GV: V Tn Dng
V D 2 (file ButtonDemo.java)
11
GV: V Tn Dng
V D 2 (file ButtonDemo.java)-tt
V D 3
12
GV: V Tn Dng
V D 3 (AddOperator.java)
AddOperator()
{
this.setTitle("My Addition Operator");
this.setLayout(null);
sumButton.setBounds(100,150,50,30);
this.add(sumButton);
sumButton.addActionListener(this);
//xem tip slide tip theo
13
GV: V Tn Dng
import java.awt.*;
import java.awt.event.*;
public class AddOperator extends Frame implements ActionListener
{ Label firstLabel = new Label("Enter first number:");
Label secondLabel = new Label("Enter second number:");
Label resultLabel = new Label("The sum is:");
TextField firstTextField = new TextField(5);
TextField secondTextField = new TextField(5);
TextField resultTextField = new TextField(5);
Button sumButton = new Button("Sum");
Button exitButton = new Button("Exit");
exitButton.setBounds(200,150,50,30);
this.add(exitButton);
exitButton.addActionListener(this);
firstLabel.setBounds(50,50,130,30);
this.add(firstLabel);
secondLabel.setBounds(50,80,130,30);
this.add(secondLabel);
resultLabel.setBounds(50,110,130,30);
this.add(resultLabel);
firstTextField.setBounds(190,50,80,25);
this.add(firstTextField);
secondTextField.setBounds(190,80,80,25);
this.add(secondTextField);
resultTextField.setBounds(190,110,80,25);
this.add(resultTextField);
this.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent evt){System.exit(0);}
});
} //xem tip slide tip theo
14
GV: V Tn Dng
V D 3 (AddOperator.java) - tt
15
GV: V Tn Dng
V D 3 (AddOperator.java) - tt
V D 4
16
GV: V Tn Dng
17
GV: V Tn Dng
V D 4 (file CheckBoxDemo.java)
18
GV: V Tn Dng
V D 4 (file CheckBoxDemo.java)-tt
V D 4 (file CheckBoxDemo.java)-tt
19
GV: V Tn Dng
PHN 2
GV: V Tn Dng
M HNH
X L S KIN
M HNH X L S KIN
21
GV: V Tn Dng
M HNH X L S KIN
22
GV: V Tn Dng
GV: V Tn Dng
M HNH X L S KIN
Ngun s kin
Cc lp thnh phn GUI m ngi s dng tng tc.
Bn c th ng k Listener p ng vi nhng s kin
nht nh
B lng nghe (Listener)
Nhn i tng s kin khi c thng bo v thc hin
p ng thch hp.
Nhiu kiu ca b lng nghe tn ti cho cc s kin c th
nh MouseListener, ActionListener, KeyListener,
Cc giao tip c hin thc v ci t cc hnh ng
i tng s kin (Event)
ng gi thng tin v s kin xut hin
Cc i tng s kin c gi ti b lng nghe khi s
kin xut hin trn thnh phn GUI
24
GV: V Tn Dng
M HNH X L S KIN
GI java.awt.event.*
EventObject
ActionEvent
AdjustmentEvent
ItemEvent
AWTEvent
TextEvent
ComponentEvent
ContainerEvent
FocusEvent
PaintEvent
WindowEvent
InputEvent
KeyEvent
MouseEvent
25
GV: V Tn Dng
Object
MT S LP S KIN
26
GV: V Tn Dng
MT S LP S KIN
27
GV: V Tn Dng
ActionListener
AdjustmentListener
ItemListener
TextListener
WindowListener
FocusListener
ContainerListener
KeyListener
MouseListener
28
GV: V Tn Dng
EventListener
CI T V QUN L S KIN
29
GV: V Tn Dng
30
GV: V Tn Dng
31
GV: V Tn Dng
32
GV: V Tn Dng
Cc Listener Method
33
GV: V Tn Dng
Cc Listener Method
34
GV: V Tn Dng
V d vi nt Button
addActionListener(ActionListener)
V d vi danh sch List
addActionListener(ActionListener)
addItemListener(ItemListener)
PHN 3
GV: V Tn Dng
CC COMPONENT
NNG CAO
VNG VN BN (TextArea)
Cho php ngi dng nhp vo nhiu dng vn bn.
Constructors
TextArea()
TextArea(int cols, int rows)
TextArea(String S)
TextArea(String S, int rows, int cols)
TextArea(String,int cols, int rows, int Scrollbars)
Common methods
void setText(String)
String getText()
void setEditable(boolean)
boolean isEditable()
vois insert(String S, int Index)
void replaceRange(String S, int begin, int end)
36
GV: V Tn Dng
VNG VN BN (TextArea)
V d:
// Cac import can thiet...
public class DemoTextArea extends Applet implements ActionListener
{
private TextArea textArea1, textArea2;
private Button copy;
public void init()
{
textArea1 = new TextArea("Sample Text", 5, 20);
textArea2 = new TextArea(5, 20);
copy = new Button("Copy >>>");
setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
copy.addActionListener(this);
add(textArea1); add(copy); add(textArea2);
//xem tip slide tip theo
37
GV: V Tn Dng
VNG VN BN (TextArea)
public void actionPerformed(ActionEvent event)
{
textArea2.setText(textArea1.getText());
}
38
GV: V Tn Dng
KHUNG V (Canvas)
39
GV: V Tn Dng
KHUNG V (Canvas)
// Cac import can thiet...
public class DemoCanvas extends Applet implements ActionListener
{
setLayout(new BorderLayout());
rectButton = new Button("Draw Rectangle");
circleButton = new Button("Draw Circle");
rectButton.addActionListener(this);
circleButton.addActionListener(this);
Panel panel = new Panel();
panel.add(rectButton);
40
GV: V Tn Dng
panel.add(circleButton);
KHUNG V (Canvas)
canvas = new MyCanvas();
canvas.setBackground(Color.lightGray);
add(panel, BorderLayout.NORTH);
add(canvas, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent event)
{
if(event.getSource() == rectButton)
canvas.draw(1);
else if(event.getSource() == circleButton)
canvas.draw(2);
41
GV: V Tn Dng
KHUNG V (Canvas)
class MyCanvas extends Canvas
{
}
public void draw(int shape)
{
this.shape = shape;
repaint();
42
GV: V Tn Dng
Kch y s thay
i tng gim
theo tng UNIT
n nh trc
43
GV: V Tn Dng
Kch y s thay
i tng gim
theo tng BLOCK
n nh trc
44
GV: V Tn Dng
Common methods
void setMaximum(int v)
int getMaximum() ;
void setMinimum(int v)
int getMinimum()
int getOrientation()
void setUnitIncrement(int v)
int getUnitIncrement()
void setBlockIncrement(int v)
int getBlockIncrement()
void setValue(int v)
int getValue()
void setVisibleAmount(int newAmount)
int getVisibleAmount()
MENU (thc n)
Cu trc mt h menu:
Cc Menu
MenuItem
45
GV: V Tn Dng
MenuBar
MENU (thc n)
46
GV: V Tn Dng
Label-Chui m t.
Shortcut key- Phm nng c kt hp.
Enable/ Disable- Cho user tc ng?
Action Command- Chui tn lnh c
kt hp.
y thc x l s kin : ActionListener
MENU (thc n)
Bi ton c nhiu tc v
Phn nhm
cc tc v
Tc v 11
Tc v 12
Tc v 13
Tc v 14
Nhm 2
Tc v 21
Tc v 22
Tc v 23
Mt Menu cha
cc MenuItem
Nhm 3
Tc v 31
Tc v 32
Tc v 33
Tc v 34
Tc v 35
Tc v 36
C th thm cc thanh
phn cch (Separator)
phn nhm nh hn
47
GV: V Tn Dng
Nhm 1
48
GV: V Tn Dng
V D 1: MENU N GIN
V D 1: MENU N GIN
}
}
49
GV: V Tn Dng
V D 1: MENU N GIN
50
GV: V Tn Dng
import java.awt.*;
import java.awt.event.*;
public class MenuDemo
extends Frame
implements ActionListener, ItemListener {
MenuBar mb; //File, Options, Help
Menu fm, om, hm; //Options Sub-Menu
Menu opSubm; //The MenuItem for exiting
MenuItem exitItem; //An option that can be on or off
CheckboxMenuItem cb;
Label label;
MenuDemo(String s) {
super("MenuDemo: " + s);
Container cp = this;
cp.setLayout(new FlowLayout());
mb = new MenuBar();
setMenuBar(mb); // Frame implements MenuContainer
//xem tip slide tip theo
51
GV: V Tn Dng
V D 2: (Checkbox Menu)
V D 2: (Checkbox Menu)
52
GV: V Tn Dng
MenuItem mi;
// The File Menu...
fm = new Menu("File");
fm.add(mi = new MenuItem("Open", new MenuShortcut('O')));
mi.addActionListener(this);
fm.add(mi = new MenuItem("Close", new MenuShortcut('W')));
mi.addActionListener(this);
fm.addSeparator();
fm.add(mi = new MenuItem("Print", new MenuShortcut('P')));
mi.addActionListener(this);
fm.addSeparator();
fm.add(mi = new MenuItem("Exit", new MenuShortcut('Q')));
exitItem = mi;
// save for action handler
mi.addActionListener(this);
mb.add(fm);
//xem tip slide tip theo
53
GV: V Tn Dng
V D 2: (Checkbox Menu)
54
GV: V Tn Dng
V D 2: (Checkbox Menu)
V D 3: (Popup Menu)
55
GV: V Tn Dng
V D 3: (Popup Menu)
56
GV: V Tn Dng
// In constructor of a frame
// Add mouse Listener for showing popup menu
addMouseListener ( new MouseAdapter()
{ public void mouseReleased(MouseEvent e)
{ if (e.isPopupTrigger()) // check right clicked
pMenu.show(e.getComponent(),
The
e.getX(),e.getY());
right-clicked
}
position
} );
PHN 4
GV: V Tn Dng
X L S KIN CHUT
X L S KIN CHUT
Java cung cp hai intefaces lng nghe (b lng
nghe s kin chut) l MouseListener v
MouseMotionListener qun l v x l cc s
kin lin quan n thit b chut.
58
GV: V Tn Dng
GV: V Tn Dng
X L S KIN CHUT
X L S KIN CHUT
GV: V Tn Dng
X L S KIN CHUT
61
GV: V Tn Dng
62
GV: V Tn Dng
X L S KIN CHUT
PHN 5
GV: V Tn Dng
X L S KIN
BN PHM
X L S KIN BN PHM
64
GV: V Tn Dng
GV: V Tn Dng
X L S KIN BN PHM
X L S KIN BN PHM
V d: minh ha vic x l s kin chut thng qua cc phng thc ca interface KeyListener. Lp
KeyDemo bn di hin thc interface KeyListener, v vy tt c 3 phng thc trong
KeyListener phi c ci t trong chng trnh.
66
GV: V Tn Dng
// KeyDemo.java
// Demonstrating keystroke events.
// Java core packages
import java.awt.*;
import java.awt.event.*;
public class KeyDemo extends Frame implements KeyListener
{
private String line1 = "", line2 = "";
private String line3 = "";
private TextArea textArea;
//xem tip slide tip theo
// set up GUI
public KeyDemo()
{
super( "Demonstrating Keystroke Events" );
textArea = new TextArea( 10, 15 ); // set up TextArea
textArea.setText( "Press any key on the keyboard..." );
textArea.setEnabled( false );
this.add( textArea );
addKeyListener( this ); // allow frame to process Key events
setSize( 350, 100 );
setVisible( true );
}
// handle press of any key
public void keyPressed( KeyEvent event )
{
line1 = "Key pressed: " +
event.getKeyText( event.getKeyCode() );
setLines2and3( event );
} //xem tip slide tip theo
67
GV: V Tn Dng
X L S KIN BN PHM
X L S KIN BN PHM
68
GV: V Tn Dng
X L S KIN BN PHM
69
GV: V Tn Dng
GV: V Tn Dng
HT
BI 6