lecture-01 java swing and event driven programming
TRANSCRIPT
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
1/54
JAVA SWING VLP TRNH HNG S KIN
tng v thnh phn
1
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
2/54
Ni dung
tng lp trnh hng s kin Thnh phn java swing v cu trc Qun l layout Nm bt cc s kin xy ra trong thnh phn Mu MVC (Model View Controller) Quan h observer/observable
2
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
3/54
Giao din chng trnh
Hng thut tonChng trnh ang c kch hotChng trnh xc nh chnh xc nhng thng tin
g n cn t mi trng, v khi no th s dngthng tin
Giao din dng text c gi l hng thutton (algorithm driven)
3
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
4/54
Giao din chng trnh
Hng s kinChng trnh ang trong trng thi b ngChng trnh ch i khi xy ra mt s kin
trong mi trngKhi s kin xy n, chng trnh kch hot s
kin , v i n s kin tip theo
Chng trnh vi ch ha, giao dinngi s dng lun lun l hng s kin(event driven)
4
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
5/54
Chng trnh hng s kin
H thng giao din, qun l s hin th v xcnh s kin
Chng trnh Java tng tc vi h thng
giao din thng qua cc thnh phn AWT Chng trnh giao tip vi h thng giao din
thng qua s hin th
5
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
6/54
Gii thiu Swing thnh phn
Thnh phn c bn: Hin th thng tin ti hoc ly thng tin t ngi dng
V d nh: JButton, JLabel, JTextField, JTextArea, pane,JComboBox
Containers: Lu tr v v tr cc thnh phn
V d: JFrame, JPanel
Container mc cao: Cha tt c cc thnh phn trc quan ca GUI Cung cp mn hnh s dng ca chng trnh
Container trung gian: S dng t chc v v tr ca cc thnh phn GUI
6
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
7/54
V d cy containers7
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
8/54
JComponent
Lp con ca java.awt.Component Tnh cht thnh phn
8
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
9/54
Thnh phn c bn
JCheckBox nh du ca mt nt hin th trng thi vi ngi sdng
JRadioButton nh du s hin th trng thi ca n ti ngi sdng
JComboBox danh sch th xung vi trng text c th thay i. JList cho php ngi s dng la chn 1 hoc nhiu khon trong
list
JMenu Menu th th hin danh sch la chn cho ngi s dng
JSlider cho php ngi s dng la chn gi tr qua nt bm JTextField vng nhp vo dng n
9
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
10/54
Thnh phn c bn
JLabel cha xu text, hnh nh hoc c hai
JProgressBar qu trnh giao tip ca mt s vic
JToolTip miu t mc ch ca thnh phn
JTree th hin s k tha d liu theo nguyn tcchun
JTable thnh phn ngi s dng sa i v th hind liu trn mt bng 2 chiu
JTextArea, JTextPane, JEditorPane xc nh vnghin th, sa i text trn nhiu dng
10
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
11/54
V d v swing
JTree
JTable
11
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
12/54
Container
Thnh phn m cha cc thnh phn khc vchnh n gi l container
12
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
13/54
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
14/54
V d v t chc thnh phn
on m sau s to JPanel v thm 2 nt vinhn l on v off
JPanel p = new JPanel();
p.add(new JButton(on));p.add(new JButton(off));
Khi m panel trn c s dng, s th hin 2
nt trong on m trn
14
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
15/54
Mt s lp swing15
Component
Container
Windows
Frame
JComponent
JFrame
RootPaneContainer
AbstractButton
JComboBox
JLabelJPanel
JRootPane
JScrollPaneJSlider
javax.swing.textJTextComponent
JTextField
JTextArea
JButton
JToggleButton
JCheckBox
JRadioButton
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
16/54
Container mc cao
Khng cha bt c container no Cung cp khung hnh ni m cc thnh phn
khc c th th hin chnh n JApplet, JDialog, JFrame, JWindows
16
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
17/54
JFrame
L ca s giao din, vi tn, khung, menu bar,v mt s thnh phn ring ca ngi dng
C th thay i v tr, di chuyn Khng cha cc lp con ca JComponent Qun l cc thnh phn ring ca ngi dng
ti ni dung lu gi Khi JFrame c t l nhn c, mt s
kin c khi to
17
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
18/54
V d JFrame
Thm thnh phn votrong JFrame
JFrame f = new JFrame(A Frame);
JButton b = new JButton(Press);Container cp = f.getContentPane();
cp.add();
18
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
19/54
Thnh phn ca JFrame19
JFrame Container
Component
Content Pane
*
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
20/54
Thnh phn chnh v ph
Cc thnh phn chnh JApplet, JDialog, JFrame, JWindowc to bi cc thnh phn GUI ca h thng
giao din trc thuc mi trngCch nhn v cm nhn ph thuc vo thnh
phn giao din
Cc thnh phn phNhng thnh phn cn li ca swing
20
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
21/54
Chng trnh n gin
import javax.swing.*;
public class DisplayFrame {
public static void main (String[] args) {
JFrame f = new JFrame("A Frame");// components
f.setSize(300,200);
f.setVisible(true);
}}
(Chng trnh trn ko ngng li c, v sao?)
21
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
22/54
Lp trnh lin tip v song song
Lung l cu trc lin tip c thc thi bi bnh
Lp trnh lin tip chng trnh tn ti bi
lung n, c thc thi lin tc bi cc hnhng trong phng thc chnh
Lp trnh song song chng trnh c th
cha mt s lung, mi lung c th thc thimt cch c lp nhau trong mt chui cchnh ng
22
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
23/54
Lp trnh lin tip v song song
S kin tr v bi lung thc thi tt c mm tham gia vo trong qu trnh th hin ccthnh phn v bt s kin
Sau khi JFrame c hin th, lung chnh skhng thi hnh cc hnh ng ph thuc votrng thi giao din ngi s dng
23
Lung chnh
S kin tr v bi lung
JFrame
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
24/54
Qun l layout (LayoutManager)
m nhim v v tr v ln ca cc thnhphn giao din c a vo
Mi container lin kt vi LayoutManager Ci t v truy cp containers ca
LayoutManagerpublic LayoutManager getLayout();
public void setLayout (LayoutManager manager); LayoutManager phi s dng kim chng ti
cc trng thi ng thng qua validate()
24
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
25/54
Cc lp LayoutManager
FlowLayout sp xp cc thnh phn t trisang phi, t trn xung di
BorderLayout sp xp ti 5 thnh phn gm:
4 hng (ng, ty, nam, bc) v trung tm GridLayout sp xp cc thnh phn 2 chiu CardLayout th hin mt thnh phn ti mt
thi im t cc bng thnh phn c sn
25
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
26/54
Cc lp LayoutManager
GridBagLayout sp xp cc thnh phn theochiu thng ng v ngang tun theo cc rngbuc
BoxLayout sp xp cc thnh phn ca mttrong cc hng ngang, hoc cc ct dc
OverlayLayout thnh phn c sp xp ln
trn cc thnh phn khc
26
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
27/54
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
28/54
S kin v thnh phn
S kin l cc i tng S kin: cc lp con ca lp tru tng
java.awt.AWTEvent Thnh phn l tng hp cc s kin Mi s kin cho bit ngun s kin v cc
thng tin lin quan ti s kin
Hm ly ngun s kinpublic Object getSource();
28
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
29/54
Listener hoc bt s kin
Listener khi mt i tng c ch v khi s kinxy ra trong thnh phn
i tng listener lin kt vi thnh phn ch ncc s kin xy ra
Listener phi thc thi cc s kin listener
Lp trnh bt s kin ti thi im thc thi ca kiu skin
29
ListenerInterface
Listener
EvenSource(component)notified
registered
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
30/54
V d chng trnh GUI
Chng trnh th hin cc nt. Khi n nt,hnh cnh v nn c trao i cho nhau
Thit k: m rng lp JFrame vi
OnOffSwitch, v xy dng constructor chacc nt n.
30
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
31/54
M v d thc thi
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class OnOffSwitch extends JFrame {
public OnOffSwitch () {
super("On/Off Switch"); // frame title
JButton button = new JButton("On/Off");
button.setForeground(Color.black);
button.setBackground(Color.white);
this.getContentPane().add(button, BorderLayout.CENTER);
}
} // end of OnOffSwitch
public class OnOffTest {
public static void main (String[] args) {
OnOffSwitch frame = new OnOffSwitch();
frame.setSize(300,200);
frame.setVisible(true);
}
}
31
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
32/54
Chng trnh cha hon thin
Khi n nt khng c g xy ra Khi nt c n sinh ra ActionEvent
Xc nh lp Switcher thc thi ActionListener
Lin kt Switcher vi JButton
32
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
33/54
nh ngha Switcher
class Switcher implements ActionListener {
public void actionPerformed (ActionEvent e) {
Component source = (Component)e.getSource();
Color oldForeground = source.getForeground();
source.setForeground(source.getBackground());
source.setBackground(oldForeground);
}
}
33
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
34/54
B sung Listener
public OnOffSwitch () {
super("On/Off Switch"); // frame title
// create button and set its colors
JButton button = new JButton("On/Off");
button.setForeground(Color.black);button.setBackground(Color.white);
// create and register buttons listener:
button.addActionListener(new Switcher());
// add button to JFrames content pane:
this.getContentPane().add(
button, BorderLayout.CENTER);
}
34
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
35/54
ng chng trnh JFrame
Kt thc chng trnh cn lp trnh listenerca ca s ng cc ca s
Qu trnh ng thng qua giao din
WindowListener:void windowClosed (WindowEvent e)void windowClosing (WindowEvent e)
35
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
36/54
Cc lp Adapter: WindowAdapter
Java cung cp b s kin tru tng Adapter Lp adapter thc thi giao din listener vi
phng thc rng
36
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
37/54
Giao din v Adapter37
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
38/54
Lp Terminator
//implements window events to close a window
class Terminator extends WindowAdapter {
public void windowClosing(WindowEvent e) {
Window w = e.getWindow();
w.dispose();
}
public void windowClosed(WindowEvent e) {
System.exit(0);
}
}
38
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
39/54
Chng trnh GUI c bn
S dng JFrameB sung cc thnh phn trong giao din Lp Terminator cho php ng ca s
Vi mi thnh phn GUI, cc s kin cnhng ti:Xc nh lp thc thi giao din Listener
Lin kt lp listener vi cc thnh phn thngqua cc s kin
39
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
40/54
Xy dng GUI
S dng JPanel nh cng c phn tch cc giao dinphc tp K thut chun
Cung cp tnh linh hot
JPanel c th c b sung ti cc cu trc khc
Xy dng chng trnh trong khung JPanel, v b sungni dung JFrame
C th s dng JComponent to khung nhnpublic void setBorder (Border border)
Th vin chun: javax.swing.BorderFactory
40
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
41/54
MenuBar v Menu
JFrame lun c thanh menu, cha cc la chn Thanh menu c th c b sung thng qua phng
thc setJMenuBarJFrame window = new JFrame("Some Application");
JMenuBar menuBar = new JMenuBar();
window.setJMenuBar(menuBar);
Menu la chn b sung thng qua JMenuItemJMenuItem swing = new JMenuItem("Swing");
JMenuItem take = new JMenuItem("Take");JMenuItem bunt = new JMenuItem("Bunt");
batter.add(swing);
batter.add(take);
batter.add(bunt);
41
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
42/54
Dialog
Ca s nhp d liu vo t ngi dng Cc thnh phn chnh JOptionPane, JFileChooser, JColorChooser
Dialog c la chn v hin th, v s chy b bi ngi dng
C 2 loi dialog:
Dialog cch thc (modal) ngi dng nhp titt c cc ca s b kha khi dialog c hin thDialog khng cch iu s dng JDialog
42
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
43/54
showInputDialog
String response = JOptionPane.showInputDialog(frame, Message Type);
int value = convertToInt(response);
43
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
44/54
showConfirmDialog
int response = JOptionPane.showConfirmDialog(frame, Take overthe world?, The Brain, JOptionPane.YES_NO_OPTION);
if (response == YES_OPTION)
44
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
45/54
JFileChooser dialog
JFileChooser directory = new JFileChooser();
directory.setCurrentDirectory(new File(.));
directory.showOpenDialog(this); //open dialog
File file = directory.getSelectedFile();
45
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
46/54
ha
Tt c cc thnh phn u cha ha. itng Graphics c phng thc v text, hnhnh, v cc hnh hnh hc
drawLine, drawImage, drawRect, drawString,fillRect
JPanel s dng nh bc v, s dng phngthc paintComponent
46
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
47/54
Mu MVC (Model View Controller)
Thnh phn mu (model) i tng mu v gii quyt cc vn Thnh phn khung nhn (view) th hin mu
Thnh phn iu khin (controller) nm cc s kin nhp vo tngi dng
47
Viewview1
Controllercontrol1
Viewview2
Controllercontrol2
Model
Viewview3
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
48/54
V d MVC
Model: tam gic vungCnh huyn v chiu cao c th thay i
View: 3 cch nhn khc nhau vi tam gic Th hin di ca cnh Th hin bng hnhGhi nh s thay i trng thi qua log file
Controller: dnh cho cch nhin th nht, thayi tam gic
48
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
49/54
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
50/54
Quan h Observer/Observable
java.util cung cp lp Observable, giao din Observer
50
Client
Observer
Observablenotifies
registers
java.util.Observable cung cp mt s phng thc saupublic void addObserver (Observer o);protected void setChanged ();public void notifyObservers ();public void notifyObservers (Object arg);
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
51/54
Thc thi Observable
public void setBase (int newBase) {
this.base = newBase;
setHypotenuse();
setChanged();
notifyObservers();}
public void setHeight (int newHeight) {
this.height = newHeight;
setHypotenuse();setChanged();
notifyObservers();
}
51
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
52/54
View
class TextView extends JPanel implements Observer {
private final static int FIELD_SIZE = 16;
private JTextField base;
private JTextField height;
private JTextField hypotenuse;
public TextView (RightTriangle model) {
super();
base =new JTextField(FIELD_SIZE);
base.setActionCommand("Base");
height =new JTextField(FIELD_SIZE);
height.setActionCommand("Height");
hypotenuse =new JTextField(FIELD_SIZE);
hypotenuse.setEditable(false);
}
52
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
53/54
Cu trc Controller53
TextView
TVController
JTextField RightTriangle
observes
modifies
has
listenes to
3
2
-
8/14/2019 Lecture-01 Java Swing and Event Driven Programming
54/54
Kt lun
GUI cu trc t cc thnh phn, theo phn cpcontainers
Sau khi th hin hnh nh ha, chng trnh s thcthi s kin ny v i cho n s kin tip
S kin listeners l i tng bt cc s kin
JFrame l mc cao nht ca container
Bt s kin v hin th giao din ha trong nhng
lung ring r l lung s kin gi i Mu MVC c s dng trong cu trc GUI hng s
kin
54