




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
本章內(nèi)容圖形用戶界面開發(fā)概述Java事件模型常用組件與事件處理
圖形用戶界面開發(fā)概述人機(jī)交互的窗口,由一組圖形界面成分和界面元素組成。主要工作:
創(chuàng)建界面,設(shè)置并布局,形成物理外觀;定義事件和響應(yīng),實(shí)現(xiàn)交互功能。組成:容器、控制組件和用戶自定義成分
容器:組織界面成分和元素??刂平M件:圖形用戶界面組成單位。用戶自定義部分:根據(jù)需要可自定義圖形、標(biāo)志圖案等。AWT組件AWT——abstractwindowtoolkit(抽象窗口工具包)建立JFC的主要基礎(chǔ);Swing組件:建立在AWT之上,新的、功能更強(qiáng)大的圖形組件包。事件類字體類GraphicsComponent顏色類布局管理類java.lang.ObjectCheckboxContainerButton…WindowPanelAppletDialogFrameList圖象類菜單類JavaApplet一個(gè)JavaApplet程序中必須有一個(gè)類是Applet的子類publicclassExample8_1extendsAppletApplet類是包java.applet中的一個(gè)類,也是java.awt中Container的子類importjava.applet.*;publicvoidinit(){….}publicvoidstart(){……}publicvoidstop(){……}publicvoiddestroy(){….}publicvoidpaint(Graphicsg){……}publicvoidrepaint(Graphicsg){……}初始化init()啟動(dòng)start()中止stop()銷毀destroy()瀏覽器下載并實(shí)例化Applet轉(zhuǎn)入后臺(tái)重新激活A(yù)pplet退出瀏覽器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”);}//實(shí)現(xiàn)接口
}//MyWindowJava事件模型事件源注冊(cè):btn.addActionListener(監(jiān)聽者對(duì)象)監(jiān)聽者類實(shí)現(xiàn)接口:publicvoidactionPerformed(ActionEvente)ActionEvent類型注冊(cè)觸發(fā)事件調(diào)用并傳遞參數(shù)ActionEvent類常用方法publicObjectgetSource(){…}
獲取發(fā)生ActionEvent事件的事件源對(duì)象例:publicvoidactionPerformed(ActionEvente){if(e.getSource()==text1)label1.setText(“It’stext1!”);elseif(e.getSource()==text2)label1.setText(“It’stext2!”);…….}publicStringgetActionCommand(){…}發(fā)生事件時(shí),獲取文本框中的文本字符串例:publicvoidactionPerformed(ActionEvente){Strings=e.getActionCommand();label1.setText(s);}事件模型實(shí)現(xiàn)方法:方法1:定義類時(shí)實(shí)現(xiàn)監(jiān)聽接口publicclassMyWindow1extendsApplet
implementsActionListener{
privateButtonm_btn=newButton(“test”);publicvoidinit(){
add(m_btn);m_btn.addActionListener(this);}
publicvoidactionPerformed(ActionEvente){……}}方法2:在另一個(gè)類中實(shí)現(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ā)”);}//實(shí)現(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)聽器對(duì)照表監(jiān)聽器接口接口中的函數(shù)ActionListeneractionPerformedAdjustmentListeneradjustmentValueChangedComponentListenercomponentHidden,componentShown,componentMoved,componentResizedContainerListenercomponentAdded,componentRemovedFocusListenerfocusGained,focusLostKeyListenerkeyPressed,keyReleased,keyTypedMouseListenermouseClicked,mouseEntered,mouseExited,mousePressed,mouseReleasedMouseMotionListenermouseDragged,mouseMovedWindowListenerwindowOpened,windowClosing,windowClosed,windowActivated,windowDeactivated,windowIconified,windowDeiconifiedItemListeneritemStateChangedTextListenertextValueChanged標(biāo)簽(Label)創(chuàng)建標(biāo)簽
Label
prompt
=
newLabel(“你好”);常用方法
getText()setText(Strings)
if(prompt.getText()==“你好”)
prompt.setText(“再見”);產(chǎn)生事件
標(biāo)簽不能接收用戶輸入,所以不能引發(fā)事件。按鈕(Button)創(chuàng)建按鈕
Button
btn=
newButton(“操作”);常用方法
getLabel()setLabel(Strings)
Strings=btn.getLabel();btn.setLabel(“你好”);產(chǎn)生事件
單擊按鈕引發(fā)動(dòng)作事件ActionEvent
對(duì)應(yīng)的監(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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 個(gè)人食堂承包協(xié)議書
- 單片機(jī)原理與應(yīng)用練習(xí)題與參考答案
- 人防租賃轉(zhuǎn)讓合同范本
- 熱工基礎(chǔ) 模擬試題
- 萬年牢說課稿
- 一周學(xué)習(xí)總結(jié)
- 一防水合同范例
- 兼職定金合同范本
- 《荊棘鳥》讀書心得
- 制作甲方合同范本
- 口腔健康全身健康課件
- 2025年高考英語復(fù)習(xí)專項(xiàng)突破:讀后續(xù)寫提分寫作(解析版)
- 數(shù)字出版概論 課件 第一章 數(shù)字出版及其發(fā)展歷程
- Wonderware InTouch:報(bào)警與事件處理機(jī)制技術(shù)教程.Tex.header
- 一把手講安全課件:提升全員安全意識(shí)
- CNAS實(shí)驗(yàn)室內(nèi)審檢查表
- 華為全屋智能試題
- Unit 3 Food and Culture Reading and thinking閱讀課教學(xué)設(shè)計(jì) 2023-2024學(xué)年人教版高中英語選擇性必修第二冊(cè)
- 數(shù)學(xué)大觀 知到智慧樹網(wǎng)課答案
- 小兒白血病飲食
- 2024年杭州科技職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫及答案解析
評(píng)論
0/150
提交評(píng)論