?14? GUI????????????? - PowerPoint PPT Presentation

About This Presentation
Title:

?14? GUI?????????????

Description:

Title: 14 GUI Author: macchan Last modified by: macchan Created Date: 4/3/2002 6:22:58 AM Document presentation format – PowerPoint PPT presentation

Number of Views:98
Avg rating:3.0/5.0
Slides: 58
Provided by: macc162
Category:
Tags: gui | java

less

Transcript and Presenter's Notes

Title: ?14? GUI?????????????


1
?14? GUI?????????????
  • GUI?????????(?)

2
????
  • ?????????????????????????
  • Swing????????GUI?????????
  • ??????????????????
  • ??????????????????
  • Model?View????????????????

3
GUI????????
  • ???????Graphical User Interface??????

????
????
4
14.1 SwingAPI???
  • 14.1.1 Swing??
  • 14.1.2 Swing???????

5
14.1.1 Swing??
  • Java?GUI????????????API
  • GUI??????????
  • GUI????????????????Swing????????
  • ?????????
  • ?????????????

6
Swing??
  • ??????????
  • ????????????

JFrame
JLabel
JTextField
JButton
7
14.1.2 Swing???????
??14-1 (Example14_1.java)
  • ?????????

import javax.swing.//swing???????????????? impor
t java.awt.//swing???????????????? //??????????
public class Example14_1 //?????????
//????????? public static void main(String
args) JFrame frame new
JFrame()//Swing??????JFrame????????
frame.setTitle("?????????")//??????
frame.setSize(200,200)//?????
frame.setLocation(50,50)//????
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE
)//????????????
???????????????
frame.setVisible(true)//????
8
????
?????????????? ????????!
9
?????????????
  • ?????????????
  • JFrame????????????????????

????() ???()??
Swing ?????
10
ButtonFrame????
??14-2 (ButtonFrame.java)
import javax.swing.//swing???????????????? impor
t java.awt.//swing???????????????? //JFrame????
????????????(?????)??? //????????????? public
class ButtonFrame extends JFrame //???????
public ButtonFrame() getContentPane().setLay
out(null)//????????????????????????
???????????????
//???????? JButton button new
JButton()//??????????? button.setText("??????
?")//?????????? button.setSize(150,20)//????
????? button.setLocation(20,50)//????????(???
?????????) //?????????????
getContentPane().add(button)
11
ButtonFrame?????Main
??14-2 (Example14_2.java)
//????????????? //????????(?????)??? public class
Example14_2 //????????? //?????????
public static void main(String args)
JFrame frame new ButtonFrame()//???????????????
? frame.setTitle("?????????")//??????
frame.setSize(200,200)//?????
frame.setLocation(50,50)//????
frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE
)//????????????
???????????????
frame.setVisible(true)//????
12
????
???????????? ?????????????????
???????????????????? ????????????!
13
14.2????????????????
  • 14.2.1 ????????????????
  • 14.2.2 Java???????????????????

14
14.2.1 ????????????????
  • ???????
  • ?????
  • ????????????????????

15
?????????????
?????
?????
16
???????????????
  • ??????????????????

//Java???????? public void ???????????????????????
?????() ??????????() if(???????Button?????)
??????????() if(??????????)
if(????????????????????) ??????????

17
?????????
  • ??????

18
?????????
  • ?????(CPU???????)
  • ??????????????????????
  • ????????????????????????????????????
  • ??????????
  • ???????????????????
  • ??????????????

19
?????????????????
  • ?????????
  • ????????????????????????????
  • ?????????????????? ???????????, ?????

20
????????????
??????? ???????? ????????
?????
?????
????????
21
????????????
????????
????
????????????
??????? ???????? ????????
?????!
?????
22
???????????????
  • ?????????????????????????????????????????????????

public void ?????????????() System.exit(0)
?????????????????
23
14.2.2 Java???????????????????
  • ?Java????????????
  • ????????????
  • ????????????
  • Java?????
  • ????????
  • ????????????????????

24
?Java????????????
  • Java?Swing?????????????????????
  • Java??????????????EventListener???????????????
  • Java??????????????????(EventObject)

25
Java????????????(2)
????????
????
????????????
Swing???
EventObject
??????? ???????? ????????
EventListener
?????
?????
26
????????????
??14-3 (ExitButtonListener.java)
import java.awt.event.//event???????????????? /
/?????????????????? ??? public class
ExitButtonListener implements ActionListener
//??????????????????? public void
actionPerformed(ActionEvent e)
System.exit(0)//??????????
27
????????????
//JFrame????????????????(?????)??? public class
ButtonFrame extends JFrame //??????? public
ButtonFrame() getContentPane().setLayout(nul
l) //???????? JButton button new
JButton() button.setText("???????")
button.setSize(150,20) button.setLocation(20,
50) //????????????? ExitButtonListener
listener new ExitButtonListener()//????????????
???? button.addActionListener(listener)//????
??????????????? //?????????????
getContentPane().add(button)
??14-3 (ButtonFrame.java)
28
?????????!
  • ?????????????????????????

29
(Java?????)????????
//JFrame????????????????(?????)??? public class
ButtonFrame extends JFrame //??????? public
ButtonFrame() //???????? //????
//????????????? ExitButtonListener listener
new ExitButtonListener()//????????????????
button.addActionListener(listener)//?????????????
?????? //?????????????
getContentPane().add(button)
//?????????????????? ??????? class
ExitButtonListener implements ActionListener
//??????????????????? public void
actionPerformed(ActionEvent e)
System.exit(0)//??????????
??14-4 (ButtonFrame.java)
30
???????
  • ??????????????????
  • Java???????
  • ????????????????????

31
????????????????????
  • ????????????????????????????????

????????
32
???????????????????????
//JFrame????????????????(?????)??? public class
ButtonFrame extends JFrame private JTextField
textField new JTextField()//?????????
//??????? public ButtonFrame()
//????????(??) //?????????????(??)
//?????????????(??) //??????????????
textField.setText("????????????")
textField.setBounds(20,100,150,20)//????????(x,y,
width,height) getContentPane().add(textField)/
/??????????????????? //??????????????????
??????? public class TextFieldChangeButtonListen
er implements ActionListener
//??????????????????? public void
actionPerformed(ActionEvent e)
textField.setText("??????????")
33
??????????
  • ???????????????????????
  • ???????????????????????????????????????
  • ??????????????????????

34
14.3 GUI????????
  • 14.3.1 GUI???????
  • 14.3.2 Model?View???
  • 14.3.3 GUI????????????

35
14.3.1 GUI???????
  • ??????????????????
  • ????
  • ?????????(AdminFrame???)
  • CUI?CUIAdminApp?????????
  • ?????????(UserFrame???)
  • CUI?CUIUserApp?????????

36
????
????????
????????
37
14.3.2 Model?View???
  • ?CUI?????????
  • ?Model?View???
  • ????Model????
  • ?display()???????

38
?CUI?????????
  • CUI???????????????????????????

39
GUI????????
  • GUI?????????????

40
?Model?View???
  • ??????????????????????

41
Model?View
  • ????????
  • ????????????????????????Model
  • ?????????
  • ?????????View
  • (??????????????????)

42
Model?View???
  • Model?View?????????????????????????
  • ??????????GUI?????????

43
????Model????
  • ItemTypeList????display()??????CUI????????????

??11-1 (ItemTypeList.java) display()
//????????? public void display()
for(int i0iltsizei) //???????
System.out.println(itemTypeArrayi.getId()""it
emTypeArrayi.getName()""

itemTypeArrayi.getPrice()"?")
?ItemStock??????
44
display()????
  • CUI?GUI???????????
  • ???display()??????GUI???????
  • Model???????????

1001cola120? 1002soda120?
CUI??????
GUI??????
45
?display()???????
  • ??????View?????????display()?????????
  • ????CUI?GUI????????????????????????
  • int size()
  • ????????
  • ItemType get(int index)
  • index????????????

46
display()?View????
??11-2 (CUIAdminApp.java) showItemTypeList()
//???????????? public void showItemTypeList()
itemTypeList.display()
47
Model-View??
  • Model?View????????????????
  • ????????????
  • ?????(CUI,GUI)?????Model?????????
  • ??????????

48
14.3.3GUI????????????
  • ??????????????
  • ?GUI????????
  • ?UserFrame??
  • ?100???????????

49
??????????????(1)
  • ???????

BackgroundPanel??? ?????????????? ????????????????
???? ????????
ItemImagePanel??? ???????????? ??????????
50
??????????????(2)
BuyButton??? ???????
ProductOutlet??? ?????????
CoinDeposit??? ??????????
51
?GUI????????
??14-6 (Example14_6.java)
//GUI???????????????????? public class
Example14_6 / ?????????
???????2???????(???????,???????) ????????
/ public static void main(String args)
ItemTypeList itemTypeList new ItemTypeList()
//?????????? Account account new Account()
//????????
//???????????????? AdminFrame adminFrame
new AdminFrame(itemTypeList,account)//??
adminFrame.setVisible(true)//??
//???????????????? UserFrame userFrame new
UserFrame(itemTypeList,account)//??
userFrame.setVisible(true)//??
itemTypeList? Account????? 2?????????????
52
?UserFrame??
??14-6 (UserFeame.java)
  • ???????
  • ??????????????????????????
  • ??????(3??)
  • Model?????????????????????
  • ?????????
  • ?????????????????????

53
?100???????????
  • 100???????????

?????????
54
100???????????
??14-7 (UserFeame.java)
//???????(????????????)??? //??????????????? publi
c class UserFrame extends JFrame public
JButton button100 new JButton()//100????
//??????? public UserFrame(ItemTypeList
newItemTypeList,Account newAccount)
//100???????? button100.setText("100?")
button100.setBounds(new Rectangle(385, 240, 79,
27)) button100.addActionListener(new
Button100_ActionListener())
background.add(button100, null)
//100??????????????? class Button100_ActionListe
ner implements java.awt.event.ActionListener
public void actionPerformed(ActionEvent e)
account.insert(new Money(100))//100?????
stateUpdate()//???????
55
???????????
  • ???????????
  • CUI???
  • stateUpdate()???
  • ????????stateUpdate()????????????

//100??????????????? class Button100_ActionLis
tener implements java.awt.event.ActionListener
public void actionPerformed(ActionEvent e)
account.insert(new Money(100))//100?????
stateUpdate()//???????
56
?????
  • ??14-3????????????????????

//???????(????????????)??? //??????????????????? p
ublic class ShoppingFrame extends JFrame
//?????????????? //????????????????????????????
public void buyButton_pressed(BuyButton
buyButton,ItemType itemType)
57
??????????????
  • ????????(????)??CUI???????????
  • ?????????????????

//?????????????? //???????????????????????????
? public void buyButton_pressed(BuyButton
buyButton,ItemType itemType) //???????????
//???????? //?????????????????????
//???? //????????????
productOutlet.addItem(item) //????????
//?????? System.out.println("???????")
//?????? //????????????
stateUpdate()//???????
Write a Comment
User Comments (0)
About PowerShow.com