1 / 49

AWT

AWT. 認識 AWT 容器與版面配置 事件處理 AWT 元件使用. 認識 AWT. GUI ( graphical user interface) AWT(Abstract Window Toolkits) Package java.awt. MenuBar. Menu. PopupMenu. MenuComponent. MenuItem. CheckBoxMenuItem. Button. Canvas. CheckBox. Object. Panel( FlowLayout ). Applet ( FlowLayout ). Choice.

nona
Télécharger la présentation

AWT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

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

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

  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. java.lang.Object java.awt.Component java.awt.Container java.awt.Panel Panel • 繼承關係 • 建構子 public Panel() public Panel(LayoutManager layout)

  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 Low Level event Semantic event .

  23. Semantic event

  24. High Level Event Behavior

  25. Low level Event Behavior

  26. Low level Event Behavior

  27. Window Event

  28. Mouse Event

  29. Key Event

  30. Other low-level event

  31. Listener vs. Adapter

  32. AWT元件 • 常用元件 • Button • Checkbox • Radio button (CheckboxGroup) • Choice • Canvas • Label • TextArea • TextField • List • Dialog

  33. Button

  34. Checkbox

  35. Radio button

  36. Choice

  37. List

  38. Label

  39. Scrollbar

  40. ScrollPane

  41. TextField

  42. TextArea

  43. AWT進階元件 • 常用元件 • Font • Color • Menu • Dialog • FileDialog

  44. Font

  45. Color

  46. Menu MenuBar Menu File, Edit, Help MenuItem New, Save, Load, Exit

  47. Dialog

  48. FileDialog

More Related