




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
本章內(nèi)容圖形用戶界面開發(fā)概述Java事件模型常用組件與事件處理
圖形用戶界面開發(fā)概述人機交互的窗口,由一組圖形界面成分和界面元素組成。主要工作:
創(chuàng)建界面,設置并布局,形成物理外觀;定義事件和響應,實現(xiàn)交互功能。組成:容器、控制組件和用戶自定義成分
容器:組織界面成分和元素??刂平M件:圖形用戶界面組成單位。用戶自定義部分:根據(jù)需要可自定義圖形、標志圖案等。AWT組件AWT——abstractwindowtoolkit(抽象窗口工具包)建立JFC的主要基礎;Swing組件:建立在AWT之上,新的、功能更強大的圖形組件包。事件類字體類GraphicsComponent顏色類布局管理類java.lang.ObjectCheckboxContainerButton…WindowPanelAppletDialogFrameList圖象類菜單類JavaApplet一個JavaApplet程序中必須有一個類是Applet的子類publicclassExample8_1extendsAppletApplet類是包java.applet中的一個類,也是java.awt中Container的子類importjava.applet.*;publicvoidinit(){….}publicvoidstart(){……}publicvoidstop(){……}publicvoiddestroy(){….}publicvoidpaint(Graphicsg){……}publicvoidrepaint(Graphicsg){……}初始化init()啟動start()中止stop()銷毀destroy()瀏覽器下載并實例化Applet轉(zhuǎn)入后臺重新激活Applet退出瀏覽器Applet生命周期:
常用組件
Component類importjava.awt.*;importjava.awt.event.*;importjava.applet.*;publicclassMyWindow1extendsApplet
implementsActionListener{
privateButtonm_btn=newButton(“test”);publicvoidinit(){add(m_btn);
m_btn.addActionListener(this);}//init()
publicvoidactionPerformed(ActionEvente){System.out.println(“Hello”);}//實現(xiàn)接口
}//MyWindowJava事件模型事件源注冊:btn.addActionListener(監(jiān)聽者對象)監(jiān)聽者類實現(xiàn)接口:publicvoidactionPerformed(ActionEvente)ActionEvent類型注冊觸發(fā)事件調(diào)用并傳遞參數(shù)ActionEvent類常用方法publicObjectgetSource(){…}
獲取發(fā)生ActionEvent事件的事件源對象例:publicvoidactionPerformed(ActionEvente){if(e.getSource()==text1)label1.setText(“It’stext1!”);elseif(e.getSource()==text2)label1.setText(“It’stext2!”);…….}publicStringgetActionCommand(){…}發(fā)生事件時,獲取文本框中的文本字符串例:publicvoidactionPerformed(ActionEvente){Strings=e.getActionCommand();label1.setText(s);}事件模型實現(xiàn)方法:方法1:定義類時實現(xiàn)監(jiān)聽接口publicclassMyWindow1extendsApplet
implementsActionListener{
privateButtonm_btn=newButton(“test”);publicvoidinit(){
add(m_btn);m_btn.addActionListener(this);}
publicvoidactionPerformed(ActionEvente){……}}方法2:在另一個類中實現(xiàn)監(jiān)聽接口
publicclassLess6extendsApplet{privateButtonm_btn=newButton(“test”);publicvoidinit(){
add(m_btn);m_btn.addActionListener(newbtnListener());}//init()}//Less6classbtnListenerimplementsActionListener{publicvoidactionPerformed(ActionEvente){
m_btn.setLabel(“事件被觸發(fā)”);}//實現(xiàn)事件接口
}//btnListener
方法3:創(chuàng)建匿名內(nèi)部類importjava.applet.*;importjava.awt.*;Importjava.awt.event.*;
publicclasstestextendsApplet
{
privateButtonm_btn=newButton(“test”);publicvoidinit(){add(m_btn);m_btn.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){
m_btn.setLabel(“事件被觸發(fā)”);
}//actionPerformed
});}//init()}//testEventObjectActionEventItemEventAdjustmentEventComponentEventTextEventAWTEventMouseEventKeyEventContainerEventPaintEventFocusEventInputEventWindowEvent事件類體系結(jié)構(gòu)圖:事件類監(jiān)聽器接口增加監(jiān)聽器方法刪除監(jiān)聽器方法ActionEventActionListeneraddActionListener()removeActionListener()AdjustmentEventAdjustmentListeneraddAdjustmentListener()removeAdjustmentListener()ComponentEventComponentListeneraddComponentListener()removeComponentListener()ContainerEventContainerListeneraddContainerListener()removeContainerListener()FocusEventFocusListeneraddFocusListener()removeFocusListener()KeyEventKeyListeneraddKeyListener()removeKeyListener()MouseEventMouseListeneraddMouseListener()removeMouseListener()MouseMotionListeneraddMouseMotionListener()removeMouseMotionListener()WindowEventWindowListeneraddWindowListener()removeWindowListener()ItemEventItemListeneraddItemListener()removeItemListener()TextEventTextListeneraddTextListener()removeTextListener()事件與監(jiān)聽器對照表監(jiān)聽器接口接口中的函數(shù)ActionListeneractionPerformedAdjustmentListeneradjustmentValueChangedComponentListenercomponentHidden,componentShown,componentMoved,componentResizedContainerListenercomponentAdded,componentRemovedFocusListenerfocusGained,focusLostKeyListenerkeyPressed,keyReleased,keyTypedMouseListenermouseClicked,mouseEntered,mouseExited,mousePressed,mouseReleasedMouseMotionListenermouseDragged,mouseMovedWindowListenerwindowOpened,windowClosing,windowClosed,windowActivated,windowDeactivated,windowIconified,windowDeiconifiedItemListeneritemStateChangedTextListenertextValueChanged標簽(Label)創(chuàng)建標簽
Label
prompt
=
newLabel(“你好”);常用方法
getText()setText(Strings)
if(prompt.getText()==“你好”)
prompt.setText(“再見”);產(chǎn)生事件
標簽不能接收用戶輸入,所以不能引發(fā)事件。按鈕(Button)創(chuàng)建按鈕
Button
btn=
newButton(“操作”);常用方法
getLabel()setLabel(Strings)
Strings=btn.getLabel();btn.setLabel(“你好”);產(chǎn)生事件
單擊按鈕引發(fā)動作事件ActionEvent
對應的監(jiān)聽接口是ActionListener
文本框(TextField)P131創(chuàng)建文本框
TextFieldtxt=newTextField();TextFieldtxt=newTextField(“hello”);TextFieldtxt=newTextField(10);TextFieldtxt=newTextField(“hel
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學三年級上冊美術(shù)國際交流計劃
- 教師職業(yè)素養(yǎng)提升云平臺心得體會
- 教育機構(gòu)法人驗收工作規(guī)范
- 企業(yè)員工健康管理組織及職責
- 2025保險公司風險評估與管理計劃
- 專利分析報告模板
- 廚房廚師安全保護措施
- 教師職業(yè)發(fā)展自評范文
- 2025年小學班主任教學管理計劃
- 音樂班平安夜活動策劃方案
- 商務英語寫作實踐智慧樹知到答案章節(jié)測試2023年中北大學
- 新年春節(jié)廉潔過年過廉潔年端午節(jié)清廉文化中秋節(jié)廉潔過節(jié)優(yōu)秀課件兩篇
- GB/T 10920-2008螺紋量規(guī)和光滑極限量規(guī)型式與尺寸
- 認知宇宙飛船之星際探索
- 皮膚病理知識學習整理課件整理
- 人工智能課件213產(chǎn)生式表示法
- 空調(diào)維保質(zhì)量保障體系及措施方案
- 建筑樁基技術(shù)規(guī)范2018
- 信息隱藏與數(shù)字水印課件(全)全書教學教程完整版電子教案最全幻燈片
- c型鋼理論重量表規(guī)格表
- 幼兒園室內(nèi)裝飾裝修技術(shù)規(guī)程TCBDA25-2018
評論
0/150
提交評論