以GUI为例了解物件以及Event.ppt
《以GUI为例了解物件以及Event.ppt》由会员分享,可在线阅读,更多相关《以GUI为例了解物件以及Event.ppt(115页珍藏版)》请在三一办公上搜索。
1、以GUI為例了解物件以及Event,Lecturer:曾學文,如何儘速學會現代程式語言,語言基礎概念+語法語法可能不太熟,但概念務必正確熟悉常用的Class Library中的class及相關API(Functions)基礎類別:數學運算,I/O,例外處理,安全管理,多緒執行等相關類別圖形使用者介面(Graphical User Interface,GUI):按鈕,文字區塊等類別.資料庫存取:支援透過一致的介面存取不同型態的DBMS的相關類別.網路連結:無線的連結建立,資料傳送,分散式運算,加密等類別.學習態度 學生:初步了解各部份用法為主,軟體工程師:以需求為導向.,程式設計的方法,純手工打
2、造vs.使用APIC/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及API,Class,如何了解一個class與相關的APIclass=data member+member functions無法自己寫(或不熟),至少試著看懂現成的類別規格,e
3、.g.,class Applet,Graphics多練習,以能run為原則,JAVA使用者介面簡介,AWT(abstract window Toolkit):Java環境中,專供程式設計GUI之用的類別集合統稱之(package java.awt.*)e.g.,class Button,TxetField(可import java.awt.Button,java.awt.TextField,)AWT元件基本控制元件:Button,CheckBox,Choice,List,Menu,TextField什麼是元件(Component):是指awt類別所生成的物件,JAVA使用者介面簡介,其他取得輸
4、入的元件:Slider,ScollBar與TextArea建立自己的元件:Canvas,有圖案的按鈕標籤(Lable)元件的容器:可以利用add()Method 將元件(如Button)加入類別(物件)稱之Window,Diglog,FileDiglog,FramePanel,Applet其他AWT類別Java.awt.*Dimension,Insert,Point,Rectangle,Polygon:指定表示大小與形狀的類別Java.awt.event.*,AWT元件階層圖,GUI程式剖析,手寫版 public class Frame1 public static void main(Str
5、ing args)Frame frame=new Frame(First Window Program);frame.setLayout(new GridLayout(7,1);frame.add(new Label(喜好選擇(可複選):);/Label元件 frame.add(new Checkbox(音樂);/Checkbox元件 frame.add(new Checkbox(體育);frame.add(new Checkbox(美術);Choice c1=new Choice();/Choice元件 c1.add(Green);c1.add(Red);c1.add(Blue);frame
6、.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();/調整視窗大小以容納所有元件 frame.setVisible(true);/顯示視窗 System.out.println(結束視窗程式,請按下CTRL+C);,事件的處理過程,public class Frame1 extends Frame implements ActionListener Te
7、xtField tf=new TextField();Button b=new Button(Hi);public static void main(String 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);
8、public void actionPerformed(ActionEvent e)tf.setText(Hello);,更清楚顯示事件的處理過程,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.setVisible
9、(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(
10、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);,使用JBuilder視覺化設計工具,public class Frame1 extends JFrame Button button1=new Button();TextField textField1=new TextField();publ
11、ic 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 button1.setLabel(button1);button1.addActionListener(new java.awt.event.ActionListener()p
12、ublic void actionPerformed(ActionEvent e)button1_actionPerformed(e););textField1.setText(textField1);this.getContentPane().add(button1,BorderLayout.NORTH);this.getContentPane().add(textField1,BorderLayout.CENTER);void button1_actionPerformed(ActionEvent e)textField1.setText(Hi);,討論,用那一種好?視覺化程式設計的迷失(
13、程式產生器,program generator),Event(事件),當我們在寫程式時,多半會需要與使用者互動或回應其指令Java 的awt 則採用event-drivenprogramming 的方法來達成此目的,當某個特定的事件發生,就會驅動程式去執行某些特定的動作,而可與使用者產生即時的互動三個要素Event 意指某個特定的事件、動作,也就是發生了什麼事件。例如:視窗關閉、滑鼠移動。Event Source 產生、觸發事件的元件。例如:ButtonEvent Handler 負責接收Event object 並作處理的MethodEventSource,產生了某個Event object
14、,而由Event Listener負責處理這個Event,Events 以物件來表示,所有的訊息都包含在java.awt.event類別庫內,所有的事件都是EventObject的子類別,以GUI為例了解物件以及Event,以MyGUI了解Event(MyGUI.class、MyGUI.form)public MyGUI()/MyGUI.java buttonPlus.addActionListener(new ActionListener()public void actionPerformed(ActionEvent e)int varA=Integer.parseInt(textA.ge
15、tText();int varB=Integer.parseInt(textB.getText();Integer varC=new Integer(varA+varB);textC.setText(varC.toString(););,委派事件模型,2.按下按鈕產生一個Event物件傳給actionListner,按鈕 buttonPlus,actionListener,1.事先有註冊,actionPerformed,3.根據物件的種類指派給事件處理者,系統實際運作狀況,當事件發生時,會有一個事件ID產生GUI元件使用這個ID碼,呼叫對應的事件方法假如收到有ActionEvent這種物件規格
16、從全部已註冊的ActionListeners中,選出欲呼叫的actionPerformed()方法,另一個版本,class MyListener implements ActionListener/介面public void actionPerformed(ActionEvent e)/實現這個介面一定要/實作actionPerformed int varA=Integer.parseInt(textA.getText();int varB=Integer.parseInt(textB.getText();Integer varC=new Integer(varA+varB);textC.se
17、tText(varC.toString();public MyGUI2()/MyGUI2.java MyListener listener=new MyListener();/buttonPlus.addActionListener(listener);,Event的註冊,Event 產生時,只會通知有註冊過的Listener。所以對必須要先把Event註冊給要負責處理的Listner註冊所有想要擷取的事件,而當使用者啟動的事件並不是我們所想要的事件時,就不加以理會程式上以XX.addXXListener 來完成註冊button.addActionListener(new ActionList
18、ener()一個event source 可以被好幾個listener 所註冊,同樣地,一個listener 也可以註冊好幾個event source所有的Event Listener 都是一種interface,裡面只有定義這個Listener所提供的抽象method必須去實作出此listener interface 內所有的method,事件物件說明,ActionListener,都是EventListener的子類別,處理的方法,Button,CheckBox,Component,Your Turn,實作出MyCalc小算盤(MyCalc.class、MyCalc.form)Form已經
19、建好了完成計算機功能代表 00S取平方根%例如50*10%5,如何撰寫滑鼠移動監聽器(MouseMotionListener),需求:想攔截滑鼠事件(MouseEvent)-實作MouseListener public class Myclass implements MouseListener someObject.addMouseListener(this);public void mouseClicked(MouseEvent e)public void mouseEnetered(MouseEvent e)public void mouseExited(MouseEvent e)pub
20、lic void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e),使用轉接類別(Adapter Class)來簡化監聽者的負擔,public void MouseAdapter implements MouseListener public void mouseClicked(MouseEvent e)public void mouseEnetered(MouseEvent e)public void mouseExited(MouseEvent e)public void mousePressed(Mouse
21、Event e)public void mouseReleased(MouseEvent e)public class MyClass extends MouseAdapter public void mouseClicked(MouseEvent e)缺點?,如何撰寫按鍵監聽器(KeyListener),implements KeyListener public interface KeyListener public void keyPressed(KeyEvent e)public void keyReleased(KeyEvent e)public void keyTyped(KeyE
22、vent e)使用class KeyAdapter,Example,public class Frame1 extends Frame Button button1=new Button();public Frame1()try jbInit();catch(Exception e)e.printStackTrace();public static void main(String args)Frame1 f1=new Frame1();f1.setBounds(10,10,400,200);f1.setVisible(true);private void jbInit()throws Exc
23、eption button1.setLabel(Hello);button1.setBounds(new Rectangle(109,86,144,48);button1.addMouseListener(new java.awt.event.MouseAdapter()public void mouseClicked(MouseEvent e)button1_mouseClicked(e);public void mousePressed(MouseEvent e)button1_mousePressed(e););,button1.addKeyListener(new java.awt.e
24、vent.KeyAdapter()public void keyPress(KeyEvent e)button1_keyPressed(e););this.setLayout(null);this.addKeyListener(new java.awt.event.KeyAdapter()public void keyReleased(KeyEvent e)this_keyReleased(e);public void keyTyped(KeyEvent e)this_keyTyped(e););this.add(button1,null);,Example,public void butto
25、n1_keyPressed(KeyEvent e)Point p=button1.getLocation();if(e.getKeyChar()=(char)i)button1.setLocation(int)(p.getX()+50),(int)(p.getY();else if(e.getKeyChar()=(char)j)button1.setLocation(int)(p.getX()-50),(int)(p.getY();void this_keyReleased(KeyEvent e)Point p=button1.getLocation();if(e.getKeyChar()=(
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- GUI 了解 物件 以及 Event

链接地址:https://www.31ppt.com/p-5502844.html