Presentation is loading. Please wait.

Presentation is loading. Please wait.

AWT  認識 AWT  容器與版面配置  事件處理  AWT 元件使用. 認識 AWT  GUI ( graphical user interface)  AWT(Abstract Window Toolkits)  Package java.awt.

Similar presentations


Presentation on theme: "AWT  認識 AWT  容器與版面配置  事件處理  AWT 元件使用. 認識 AWT  GUI ( graphical user interface)  AWT(Abstract Window Toolkits)  Package java.awt."— Presentation transcript:

1 AWT  認識 AWT  容器與版面配置  事件處理  AWT 元件使用

2 認識 AWT  GUI ( graphical user interface)  AWT(Abstract Window Toolkits)  Package java.awt

3 Object MenuComponent Component MenuBar MenuItem Menu CheckBoxMenuItem Button TextComponent TextArea TextField PopupMenu Canvas CheckBox Choice Container Label List ScrollBar Panel(FlowLayout) Window ScrollPane Dialog(BorderLayout) Frame (BorderLayout) Applet (FlowLayout)

4 Container  所有 AWT 元件 (Component) 都要裝在容器 (Container) 中  兩個主要且常用的 container –java.awt.Frame –java.awt.Panel

5 Frame  繼承關係  建構子 public Frame() throws HeadlessException public Frame(GraphicsConfiguration gc) public Frame(String title) throws HeadlessException java.lang.Object java.awt.Component java.awt.Container java.awt.Window java.awt.Frame

6 Frame  類別常數 public static final int ICONIFIED public static final int MAXIMIZED_HORIZ public static final int MAXIMIZED_VERT public static final int MAXIMIZED_BOTH public static final int NORMAL

7 Frame 常用方法  Frame 常用方法 public String getTitle() public void setTitle(String title) public MenuBar getMenuBar() public void setMenuBar(MenuBar mb) public int getState() public void setState(int state) public boolean isResizable() public void setResizable(boolean resizable)

8 Window 常用方法  Window 常用方法 public boolean isShowing() public boolean isFocused() public void toFront() public void toBack() public void pack()

9 Container 常用方法  Container 常用方法 public Component add(Component comp) public void remove(Component comp) public void remove(int index) public void removeAll() public LayoutManager getLayout() public void setLayout(LayoutManager mgr)

10 Component 常用方法  Component 常用方法 public void setFont(Font f) public void setForeground(Color c) public void setBackground(Color c) public void setSize(int width, int height) public void setVisible(boolean b) public void setLocation(int x, int y)

11 Panel  繼承關係  建構子 public Panel() public Panel(LayoutManager layout) java.lang.Object java.awt.Component java.awt.Container java.awt.Panel

12 版面配置  負責元件的相對位置,大小及長相  只有 container 的子類別可以使用 Layout  常用 Layput –BorderLayout –FlowLayout –GridLayout –CardLayout –GridBagLayout

13 Flow Layout  Flow Layout –Default Layout manager of Panel, Applet – 每個元件大小為元件之 preferred size –Resize

14 BorderLayout  BorderLayout –Default Layout manager of Window, Frame – 將 container 分為東, 西, 南, 北, 中, 五個區域 – 元件 preferred size 南北 - preferred 高 東西 - preferred 寬 – 指定位置的常數 BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.CENTER 北 南 中西東 ( ( ╭╮

15 BorderLayout  Resize – 南北 : 水平縮放 – 東西 : 垂直縮放 – 中 : 等比例縮放

16 Grid Layout  Grid Layout – 將 Container 分成 X*Y 的表格 – 元件加入順序為先由左而右, 再由上至下 – 元件大小為表格的長與寬 –Resize

17 Card Layout  Card Layout – 將元件視為一系列的卡片, 一次顯示一張 – 元件大小為 Container 的長與寬 #1#2

18 GridBag Layout  GridBag Layout – 有彈性但複雜的版面配置方式 – 每一元件可自訂長與寬 – 元件可超出 Grid 的範圍

19 不使用 Layout  Java 提供的 Layout Manager 無法滿足需求 時 – 自行設計 Layout Manager: 實作 LayoutManager 或 LayoutManager2 – 不使用 Layout setLayout(null) setSize() setLocation()

20 事件處理  Package java.awt.event  事件處理架構 –JDK 1.1 之後 : Delegation Model 委託模式 –JDK 1.0:Hierarchical model Hierarchical Model Base on containment

21 Delegation Model  三個角色 – 事件本身 (xxxEvent) – 事件產生來源 (component) – 事件處理者 ( 繼承 xxxAdapter, 實作 xxxListener)  委託 addXXXListener()  取消委託 removeXXXListener()

22 AWT Event 類別架構 –Semantic event –Low level event Semantic event. Low Level event

23 Semantic event 事件類別 產生事件的元件產生事件的時機 ActionEvent Button 按鈕被按下 List Double click List 中項目 MenuItem 選取功能表項目 TextField 按下 Enter 鍵 AdjustmentEventScrollbar 捲軸捲動 ItemEventCheckbox 狀態改變時 CheckboxMenuItem 勾選功能表選項 Choice 選取下拉式選單中項目 List 選取 List 項目 TextEventTextField 文字內容改變 TextArea 文字內容改變

24 High Level Event Behavior 事件類別可使用的 ListenerListener 中宣告 Methods ActionActionListener actionPerformed(ActionEvent) AdjustmentAdjustmentListener adjustmentValueChanged(AdjustmentEvent) ItemItemListener itemStateChanged(ItemEvent) TextTextListener textValueChanged(TextEvent)

25 事件類別可使用的 ListenerListener 中宣告 Methods Mouse motionMouseMotionListenermouseDragged(MouseEvent) mouseMoved(MouseEvent) Mouse buttonMouseListenermousePressed(MouseEvent) mouseReleased(MouseEvent) mouseEntered(MouseEvent) mouseExited(MouseEvent) mouseClicked(MouseEvent) KeyKeyListenerkeyPressed(KeyEvent) keyReleased(KeyEvent) keyTyped(KeyEvent) FocusFocusListenerfocusGained(FocusEvent) focusLost(FocusEvent) Low level Event Behavior

26 事件類別可使用的 ListenerListener 中宣告 Methods WindowWindowListenerwindowClosing(WindowEvent) windowOpened(WindowEvent) windowIconified(WindowEvent) windowDeiconified(WindowEvent) windowClosed(WindowEvent) windowActivated(WindowEvent) windowDeactivated(WindowEvent) ComponentComponentListenercomponentMoved(ComponentEvent) componentHidden(ComponentEvent) componentResized(ComponentEvent) componentShown(ComponentEvent) ContainerContainerListenercomponentAdded(ContainerEvent) componentRemoved(ContainerEvent) Low level Event Behavior

27 Window Event 事件方法產生原因 windowActivated(WindowEvent) 變為前景視窗時 windowDeactivated(WindowEvent) 變為背景視窗時 windowIconified(WindowEvent) 視窗縮到最小 windowDeiconified(WindowEvent) 視窗放到最大 windowOpened(WindowEvent) 視窗打開時 windowClosed(WindowEvent) 視窗關閉後 windowClosing(WindowEvent) 正在關閉視窗時

28 Mouse Event 事件方法產生原因 mouseDragged(MouseEvent) 拖曳滑鼠 mouseMoved(MouseEvent) 移動滑鼠 mousePressed(MouseEvent) 按下滑鼠按鍵 mouseReleased(MouseEvent) 放開滑鼠按鍵 mouseEntered(MouseEvent) 滑鼠游標進入 Component mouseExited(MouseEvent) 滑鼠游標離開 Component mouseClicked(MouseEvent) 按一下滑鼠按鍵

29 Key Event 事件方法產生原因 keyPressed(KeyEvent) 按一下鍵盤按鍵 keyReleased(KeyEvent) 放開鍵盤按鍵 keyTyped(KeyEvent) 按一下可產生可見字元的按鍵

30 Other low-level event 事件方法產生原因 componentAdded(ContainerEvent) 有 component 加入 componentRemoved(ContainerEvent) 有 component 移除 事件方法產生原因 focusGained(FocusEvent) 元件得到駐點 focusLost(FocusEvent) 元件失去駐點

31

32 Listener vs. Adapter Listener 名稱 Adapter 名稱 ActionListener 無 AdjustmentListener 無 ItemListener 無 TextListener 無 MouseListenerMouseAdapter MouseMotionListenerMouseMotionAdapter KeyListenerKeyAdapter FocusListenerFocusAdapter WindowListenerWindowAdapter ContainerListenerContainerAdapter ComponentListenerComponentAdapter

33 AWT 元件  常用元件 –Button –Checkbox –Radio button (CheckboxGroup) –Choice –Canvas –Label –TextArea –TextField –List –Dialog

34 Button

35 Checkbox

36 Radio button

37 Choice

38 List

39 Label

40 Scrollbar

41 ScrollPane

42 TextField

43 TextArea

44 AWT 進階元件  常用元件 –Font –Color –Menu –Dialog –FileDialog

45 Font

46 Color

47 Menu MenuBar Menu File, Edit, Help MenuItem New, Save, Load, Exit

48 Dialog

49 FileDialog


Download ppt "AWT  認識 AWT  容器與版面配置  事件處理  AWT 元件使用. 認識 AWT  GUI ( graphical user interface)  AWT(Abstract Window Toolkits)  Package java.awt."

Similar presentations


Ads by Google