Transcript AWT

AWT
 認識AWT
 容器與版面配置
 事件處理
 AWT元件使用
認識AWT
 GUI (graphical user interface)
 AWT(Abstract Window Toolkits)
 Package java.awt
MenuBar
Menu
MenuComponent
PopupMenu
MenuItem
CheckBoxMenuItem
Button
Canvas
CheckBox
Object
Choice
Panel(FlowLayout)
Component
Applet (FlowLayout)
Dialog(BorderLayout)
Container
Label
Window
ScrollPane
List
ScrollBar
TextArea
TextComponent
TextField
Frame (BorderLayout)
Container
 所有AWT元件(Component)都要裝在容器
(Container)中
 兩個主要且常用的container
– java.awt.Frame
– java.awt.Panel
Frame
 繼承關係
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Window
java.awt.Frame
 建構子
public Frame() throws HeadlessException
public Frame(GraphicsConfiguration gc)
public Frame(String title) throws
HeadlessException
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
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)
Window 常用方法
 Window 常用方法
public boolean isShowing()
public boolean isFocused()
public void toFront()
public void toBack()
public void pack()
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)
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)
Panel
 繼承關係
java.lang.Object
java.awt.Component
java.awt.Container
java.awt.Panel
 建構子
public Panel()
public Panel(LayoutManager layout)
版面配置
 負責元件的相對位置,大小及長相
 只有container的子類別可以使用Layout
 常用Layput
– BorderLayout
– FlowLayout
– GridLayout
– CardLayout
– GridBagLayout
Flow Layout
 Flow Layout
– Default Layout manager of Panel, Applet
– 每個元件大小為元件之preferred size
– Resize
BorderLayout
 BorderLayout
– Default Layout manager of Window, Frame
– 將container分為東,西,南,北,中,五個區域
– 元件preferred size
南北- preferred 高
東西- preferred 寬
(
北
– 指定位置的常數
BorderLayout.EAST,
BorderLayout.WEST,
西
中
BorderLayout.NORTH,
╭╮
BorderLayout.SOUTH,
(
南
BorderLayout.CENTER
東
╭╮
BorderLayout
 Resize
– 南北:水平縮放
– 東西:垂直縮放
– 中:等比例縮放
Grid Layout
 Grid Layout
– 將Container分成X*Y的表格
– 元件加入順序為先由左而右,再由上至下
– 元件大小為表格的長與寬
– Resize
Card Layout
 Card Layout
– 將元件視為一系列的卡片,一次顯示一張
– 元件大小為Container的長與寬
#1
#2
GridBag Layout
 GridBag Layout
– 有彈性但複雜的版面配置方式
– 每一元件可自訂長與寬
– 元件可超出Grid的範圍
不使用Layout
 Java提供的Layout Manager無法滿足需求
時
– 自行設計Layout Manager:
實作LayoutManager或LayoutManager2
– 不使用Layout
setLayout(null)
setSize()
setLocation()
事件處理
 Package java.awt.event
 事件處理架構
– JDK 1.1之後:Delegation Model委託模式
– JDK 1.0:Hierarchical model
Hierarchical Model
Base on containment
Delegation Model
 三個角色
– 事件本身(xxxEvent)
– 事件產生來源(component)
– 事件處理者(繼承xxxAdapter, 實作xxxListener)
 委託
addXXXListener()
 取消委託
removeXXXListener()
AWT Event類別架構
– Semantic event
– Low level event
Low Level
event
Semantic
event .
Semantic event
事件類別
ActionEvent
產生事件的元件
Button
按鈕被按下
List
Double click List中項目
MenuItem
選取功能表項目
TextField
按下Enter鍵
AdjustmentEvent Scrollbar
ItemEvent
產生事件的時機
Checkbox
捲軸捲動
狀態改變時
CheckboxMenuItem 勾選功能表選項
TextEvent
Choice
選取下拉式選單中項目
List
選取List項目
TextField
文字內容改變
TextArea
文字內容改變
High Level Event Behavior
事件類別
Action
可使用的Listener
ActionListener
Adjustment AdjustmentListener
Listener中宣告Methods
actionPerformed(ActionEvent)
adjustmentValueChanged(AdjustmentEvent)
Item
ItemListener
itemStateChanged(ItemEvent)
Text
TextListener
textValueChanged(TextEvent)
Low level Event Behavior
事件類別
可使用的Listener
Listener中宣告Methods
Mouse motion MouseMotionListener mouseDragged(MouseEvent)
mouseMoved(MouseEvent)
Mouse button
Key
Focus
MouseListener
mousePressed(MouseEvent)
mouseReleased(MouseEvent)
mouseEntered(MouseEvent)
mouseExited(MouseEvent)
mouseClicked(MouseEvent)
KeyListener
keyPressed(KeyEvent)
keyReleased(KeyEvent)
keyTyped(KeyEvent)
FocusListener
focusGained(FocusEvent)
focusLost(FocusEvent)
Low level Event Behavior
事件類別
可使用的Listener
Window
WindowListener
Listener中宣告Methods
windowClosing(WindowEvent)
windowOpened(WindowEvent)
windowIconified(WindowEvent)
windowDeiconified(WindowEvent)
windowClosed(WindowEvent)
windowActivated(WindowEvent)
windowDeactivated(WindowEvent)
Component ComponentListener componentMoved(ComponentEvent)
componentHidden(ComponentEvent)
componentResized(ComponentEvent)
componentShown(ComponentEvent)
Container
ContainerListener
componentAdded(ContainerEvent)
componentRemoved(ContainerEvent)
Window Event
事件方法
產生原因
windowActivated(WindowEvent)
變為前景視窗時
windowDeactivated(WindowEvent)
變為背景視窗時
windowIconified(WindowEvent)
視窗縮到最小
windowDeiconified(WindowEvent)
視窗放到最大
windowOpened(WindowEvent)
視窗打開時
windowClosed(WindowEvent)
視窗關閉後
windowClosing(WindowEvent)
正在關閉視窗時
Mouse Event
事件方法
產生原因
mouseDragged(MouseEvent)
拖曳滑鼠
mouseMoved(MouseEvent)
移動滑鼠
mousePressed(MouseEvent)
按下滑鼠按鍵
mouseReleased(MouseEvent)
放開滑鼠按鍵
mouseEntered(MouseEvent)
滑鼠游標進入Component
mouseExited(MouseEvent)
滑鼠游標離開Component
mouseClicked(MouseEvent)
按一下滑鼠按鍵
Key Event
事件方法
產生原因
keyPressed(KeyEvent)
按一下鍵盤按鍵
keyReleased(KeyEvent)
放開鍵盤按鍵
keyTyped(KeyEvent)
按一下可產生可見字元的按鍵
Other low-level event
事件方法
產生原因
componentAdded(ContainerEvent)
有component加入
componentRemoved(ContainerEvent)
有component移除
事件方法
產生原因
focusGained(FocusEvent)
元件得到駐點
focusLost(FocusEvent)
元件失去駐點
Listener vs. Adapter
Listener名稱
Adapter名稱
ActionListener
無
AdjustmentListener
無
ItemListener
無
TextListener
無
MouseListener
MouseAdapter
MouseMotionListener
MouseMotionAdapter
KeyListener
KeyAdapter
FocusListener
FocusAdapter
WindowListener
WindowAdapter
ContainerListener
ContainerAdapter
ComponentListener
ComponentAdapter
AWT元件
 常用元件
– Button
– Checkbox
– Radio button (CheckboxGroup)
– Choice
– Canvas
– Label
– TextArea
– TextField
– List
– Dialog
Button
Checkbox
Radio button
Choice
List
Label
Scrollbar
ScrollPane
TextField
TextArea
AWT進階元件
 常用元件
– Font
– Color
– Menu
– Dialog
– FileDialog
Font
Color
Menu
MenuBar
Menu
File, Edit, Help
MenuItem
New, Save, Load, Exit
Dialog
FileDialog