以GUI为例了解物件以及Event.ppt
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《以GUI为例了解物件以及Event.ppt》由会员分享,可在线阅读,更多相关《以GUI为例了解物件以及Event.ppt(115页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、National Taiwan UniversityDepartment of Computer Science and Information Engineering以GUI为例了解物件以及Event Still waters run deep.流静水深流静水深,人静心深人静心深 Where there is life,there is hope。有生命必有希望。有生命必有希望National Taiwan UniversityDepartment of Computer Science and Information Engineering如何儘速學會現代程式語言語言基礎概念+語法語法可能
2、不太熟,但概念務必正確熟悉常用的Class Library中的class及相關API(Functions)基礎類別:數學運算,I/O,例外處理,安全管理,多緒執行等相關類別圖形使用者介面(Graphical User Interface,GUI):按鈕,文字區塊等類別.資料庫存取:支援透過一致的介面存取不同型態的DBMS的相關類別.網路連結:無線的連結建立,資料傳送,分散式運算,加密等類別.學習態度 學生:初步了解各部份用法為主,軟體工程師:以需求為導向.National Taiwan UniversityDepartment of Computer Science and Informati
3、on Engineering程式設計的方法純手工打造vs.使用APIa)C/C+:純手工打造:僅使用cin/cout,print()/scanf()API and class C:string.h,math.h,ctype.h,stdib.h,Turbo C/C+中所提供的繪圖,數學運算函數 C+:STL中的container,iterator與algorithm Visual C+/Borland C+所提供的GUI,繒圖,多序執 行,等API.Java:善用class及APINational Taiwan UniversityDepartment of Computer Science a
4、nd Information EngineeringClass如何了解一個class與相關的APIclass=data member+member functions無法自己寫(或不熟),至少試著看懂現成的類別規格,e.g.,class Applet,Graphics多練習,以能run為原則National Taiwan UniversityDepartment of Computer Science and Information EngineeringJAVA使用者介面簡介AWT(abstract window Toolkit):Java環境中,專供程式設計GUI之用的類別集合統稱之(pa
5、ckage java.awt.*)e.g.,class Button,TxetField(可import java.awt.Button,java.awt.TextField,)AWT元件基本控制元件:Button,CheckBox,Choice,List,Menu,TextField什麼是元件(Component):是指awt類別所生成的物件National Taiwan UniversityDepartment of Computer Science and Information EngineeringJAVA使用者介面簡介其他取得輸入的元件:Slider,ScollBar與TextAr
6、ea建立自己的元件:Canvas,有圖案的按鈕標籤(Lable)元件的容器:可以利用add()Method 將元件(如Button)加入類別(物件)稱之Window,Diglog,FileDiglog,FramePanel,Applet其他AWT類別Java.awt.*Dimension,Insert,Point,Rectangle,Polygon:指定表示大小與形狀的類別Java.awt.event.*National Taiwan UniversityDepartment of Computer Science and Information EngineeringAWT元件階層圖Nati
7、onal Taiwan UniversityDepartment of Computer Science and Information EngineeringGUI程式剖析手寫版 public class Frame1 public static void main(String args)Frame frame=new Frame(First Window Program);frame.setLayout(new GridLayout(7,1);frame.add(new Label(喜好選擇(可複選):);/Label元件 frame.add(new Checkbox(音樂);/Chec
8、kbox元件 frame.add(new Checkbox(體育);frame.add(new Checkbox(美術);Choice c1=new Choice();/Choice元件 c1.add(Green);c1.add(Red);c1.add(Blue);frame.add(c1);List ls1=new List(3,false);/List元件 ls1.add(一年級);ls1.add(二年級);ls1.add(三年級);frame.add(ls1);frame.add(new Button(測試按鈕);/Button元件 frame.pack();/調整視窗大小以容納所有元件
9、 frame.setVisible(true);/顯示視窗 System.out.println(結束視窗程式,請按下CTRL+C);National Taiwan UniversityDepartment of Computer Science and Information Engineering事件的處理過程public class Frame1 extends Frame implements ActionListener TextField tf=new TextField();Button b=new Button(Hi);public static void main(Strin
10、g args)Frame1 mf=new Frame1();mf.setBounds(10,10,150,100);mf.setVisible(true);public Frame1()this.setLayout(null);tf.setBounds(30,30,80,30);b.setBounds(new Rectangle(30,80,50,30);b.addActionListener(this);add(tf);add(b);public void actionPerformed(ActionEvent e)tf.setText(Hello);National Taiwan Univ
11、ersityDepartment of Computer Science and Information Engineering更清楚顯示事件的處理過程public class Frame1 public static void main(String args)MyFrame mf=new MyFrame();EventSourceFrame esf=new EventSourceFrame();mf.setBounds(10,10,150,100);esf.setBounds(180,10,100,100);esf.registerEventListener(mf);mf.setVisib
12、le(true);esf.setVisible(true);class MyFrame extends Frame implements ActionListener TextField tf=new TextField();public MyFrame()this.setLayout(null);tf.setBounds(30,30,80,30);add(tf);public void actionPerformed(ActionEvent e)tf.setText(Hello);class EventSourceFrame extends Frame Button b=new Button
13、(Hi);public void registerEventListener(ActionListener AL)b.addActionListener(AL);public EventSourceFrame()this.setLayout(null);b.setBounds(new Rectangle(30,30,50,30);this.add(b);National Taiwan UniversityDepartment of Computer Science and Information Engineering使用JBuilder視覺化設計工具public class Frame1 e
14、xtends JFrame Button button1=new Button();TextField textField1=new TextField();public Frame1()try jbInit();catch(Exception e)e.printStackTrace();public static void main(String args)Frame1 frame1=new Frame1();frame1.setSize(200,100);frame1.setVisible(true);private void jbInit()throws Exception button
15、1.setLabel(button1);button1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(ActionEvent e)button1_actionPerformed(e););textField1.setText(textField1);this.getContentPane().add(button1,BorderLayout.NORTH);this.getContentPane().add(textField1,BorderLayout.CENTER);void
16、button1_actionPerformed(ActionEvent e)textField1.setText(Hi);National Taiwan UniversityDepartment of Computer Science and Information Engineering討論用那一種好?視覺化程式設計的迷失(程式產生器,program generator)National Taiwan UniversityDepartment of Computer Science and Information EngineeringEvent(事件)當我們在寫程式時,多半會需要與使用者互
17、動或回應其指令Java 的awt 則採用event-drivenprogramming 的方法來達成此目的,當某個特定的事件發生,就會驅動程式去執行某些特定的動作,而可與使用者產生即時的互動三個要素Event 意指某個特定的事件、動作,也就是發生了什麼事件。例如:視窗關閉、滑鼠移動。Event Source 產生、觸發事件的元件。例如:ButtonEvent Handler 負責接收Event object 並作處理的MethodEventSource,產生了某個Event object,而由Event Listener負責處理這個EventNational Taiwan University
18、Department of Computer Science and Information EngineeringEvents 以物件來表示n所有的訊息都包含在java.awt.event類別庫內所有的事件都是EventObject的子類別National Taiwan UniversityDepartment of Computer Science and Information Engineering以GUI為例了解物件以及Event以MyGUI了解Event(MyGUI.class、MyGUI.form)public MyGUI()/MyGUI.java buttonPlus.addA
19、ctionListener(new ActionListener()public void actionPerformed(ActionEvent e)int varA=Integer.parseInt(textA.getText();int varB=Integer.parseInt(textB.getText();Integer varC=new Integer(varA+varB);textC.setText(varC.toString(););National Taiwan UniversityDepartment of Computer Science and Information
20、 Engineering委派事件模型2.按下按鈕產生一個Event物件傳給actionListner按鈕 buttonPlusactionListener1.事先有註冊actionPerformed3.根據物件的種類指派給事件處理者National Taiwan UniversityDepartment of Computer Science and Information Engineering系統實際運作狀況當事件發生時,會有一個事件ID產生GUI元件使用這個ID碼,呼叫對應的事件方法假如收到有ActionEvent這種物件規格從全部已註冊的ActionListeners中,選出欲呼叫的a
21、ctionPerformed()方法National Taiwan UniversityDepartment of Computer Science and Information Engineering另一個版本class MyListener implements ActionListener/介面public void actionPerformed(ActionEvent e)/實現這個介面一定要 /實作actionPerformed int varA=Integer.parseInt(textA.getText();int varB=Integer.parseInt(textB.ge
22、tText();Integer varC=new Integer(varA+varB);textC.setText(varC.toString();public MyGUI2()/MyGUI2.java MyListener listener=new MyListener();/buttonPlus.addActionListener(listener);National Taiwan UniversityDepartment of Computer Science and Information EngineeringEvent的註冊Event 產生時,只會通知有註冊過的Listener。所
23、以對必須要先把Event註冊給要負責處理的Listner註冊所有想要擷取的事件,而當使用者啟動的事件並不是我們所想要的事件時,就不加以理會程式上以XX.addXXListener 來完成註冊button.addActionListener(new ActionListener()一個event source 可以被好幾個listener 所註冊,同樣地,一個listener 也可以註冊好幾個event source所有的Event Listener 都是一種interface,裡面只有定義這個Listener所提供的抽象method必須去實作出此listener interface 內所有的m
24、ethodNational Taiwan UniversityDepartment of Computer Science and Information Engineering事件物件說明事件名稱發生事件的原因ActionEvent按下按鈕、或是在輸入文字方塊/選擇清單方塊時按下EnterAdjustmentEvent移動捲軸物件時ItemEvent選取核取方塊、選項鈕、下拉式清單和清單方塊TextEvent輸入的文字內容改變ComponentEvent隱藏、移動、顯示和調整元件時ContainerEvent新增或刪除元件FocusEvent元件取得或失去焦點時KeyEvent鍵盤按下、放開
25、和輸入字元MouseEvent與滑鼠有關的行為WindowEvent視窗的操作,包括開、關、調整大小PaintEvent與繪圖有關的動作InputEvent它是KeyEvent和MouseEvent的父抽象類別National Taiwan UniversityDepartment of Computer Science and Information EngineeringActionListenerAction TypeActionListenerComponentEventComponentListenerFocusEventFocusListenerKeyEventKeyListene
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- GUI 了解 物件 以及 Event
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内