以GUI为例了解物件以及Event.ppt
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如何儘速學會現代程式語言語言基礎概念+語法語法可能不太熟,但概念務必正確熟悉常用的Class Library中的class及相關API(Functions)基礎類別:數學運算,I/O,例外處理,安全管理,多緒執行等相關類別圖形使用者介面(Graphical User Interface,GUI):按鈕,文字區塊等類別.資料庫存取:支援透過一致的介面存取不同型態的DBMS的相關類別.網路連結:無線的連結建立,資料傳送,分散式運算,加密等類別.學習態度 學生:初步了解各部份用法為主,軟體工程師:以需求為導向.National Taiwan UniversityDepartment of Computer Science and Information 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 and 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之用的類別集合統稱之(package 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與TextArea建立自己的元件: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元件階層圖National 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(音樂);/Checkbox元件 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();/調整視窗大小以容納所有元件 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(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);public void actionPerformed(ActionEvent e)tf.setText(Hello);National Taiwan UniversityDepartment 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.setVisible(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(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 extends 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 button1.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 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(事件)當我們在寫程式時,多半會需要與使用者互動或回應其指令Java 的awt 則採用event-drivenprogramming 的方法來達成此目的,當某個特定的事件發生,就會驅動程式去執行某些特定的動作,而可與使用者產生即時的互動三個要素Event 意指某個特定的事件、動作,也就是發生了什麼事件。例如:視窗關閉、滑鼠移動。Event Source 產生、觸發事件的元件。例如:ButtonEvent Handler 負責接收Event object 並作處理的MethodEventSource,產生了某個Event object,而由Event Listener負責處理這個EventNational Taiwan UniversityDepartment 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.addActionListener(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 Engineering委派事件模型2.按下按鈕產生一個Event物件傳給actionListner按鈕 buttonPlusactionListener1.事先有註冊actionPerformed3.根據物件的種類指派給事件處理者National Taiwan UniversityDepartment of Computer Science and Information Engineering系統實際運作狀況當事件發生時,會有一個事件ID產生GUI元件使用這個ID碼,呼叫對應的事件方法假如收到有ActionEvent這種物件規格從全部已註冊的ActionListeners中,選出欲呼叫的actionPerformed()方法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.getText();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。所以對必須要先把Event註冊給要負責處理的Listner註冊所有想要擷取的事件,而當使用者啟動的事件並不是我們所想要的事件時,就不加以理會程式上以XX.addXXListener 來完成註冊button.addActionListener(new ActionListener()一個event source 可以被好幾個listener 所註冊,同樣地,一個listener 也可以註冊好幾個event source所有的Event Listener 都是一種interface,裡面只有定義這個Listener所提供的抽象method必須去實作出此listener interface 內所有的methodNational Taiwan UniversityDepartment of Computer Science and Information Engineering事件物件說明事件名稱發生事件的原因ActionEvent按下按鈕、或是在輸入文字方塊/選擇清單方塊時按下EnterAdjustmentEvent移動捲軸物件時ItemEvent選取核取方塊、選項鈕、下拉式清單和清單方塊TextEvent輸入的文字內容改變ComponentEvent隱藏、移動、顯示和調整元件時ContainerEvent新增或刪除元件FocusEvent元件取得或失去焦點時KeyEvent鍵盤按下、放開和輸入字元MouseEvent與滑鼠有關的行為WindowEvent視窗的操作,包括開、關、調整大小PaintEvent與繪圖有關的動作InputEvent它是KeyEvent和MouseEvent的父抽象類別National Taiwan UniversityDepartment of Computer Science and Information EngineeringActionListenerAction TypeActionListenerComponentEventComponentListenerFocusEventFocusListenerKeyEventKeyListenerContainerEventContainerListenerWindowEventWindowListenerItemEventItemListenerAdjustEventAdjustListenerTextEventTextListenerActionEventActionListener都是EventListener的子類別National Taiwan UniversityDepartment of Computer Science and Information Engineering處理的方法ButtonCheckBoxComponentNational Taiwan UniversityDepartment of Computer Science and Information EngineeringYour Turn實作出MyCalc小算盤(MyCalc.class、MyCalc.form)Form已經建好了完成計算機功能代表 00S取平方根%例如50*10%5National Taiwan UniversityDepartment of Computer Science and Information Engineering如何撰寫滑鼠移動監聽器(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)public void mousePressed(MouseEvent e)public void mouseReleased(MouseEvent e)National Taiwan UniversityDepartment of Computer Science and Information Engineering使用轉接類別(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(MouseEvent e)public void mouseReleased(MouseEvent e)public class MyClass extends MouseAdapter public void mouseClicked(MouseEvent e)缺點?National Taiwan UniversityDepartment of Computer Science and Information Engineering如何撰寫按鍵監聽器(KeyListener)implements KeyListener public interface KeyListener public void keyPressed(KeyEvent e)public void keyReleased(KeyEvent e)public void keyTyped(KeyEvent e)使用class KeyAdapterNational Taiwan UniversityDepartment of Computer Science and Information EngineeringExamplepublic 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 Exception 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.event.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);National Taiwan UniversityDepartment of Computer Science and Information EngineeringExamplepublic void button1_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()=(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 button1_mouseClicked(MouseEvent e)Point p=button1.getLocation();button1.setLocation(int)(p.getX()+50),(int)(p.getY();void button1_mousePressed(MouseEvent e)Point p=button1.getLocation();button1.setLocation(int)(p.getX(),(int)(p.getY()+50);void this_keyTyped(KeyEvent e)Point p=button1.getLocation();button1.setLocation(int)(p.getX(),(int)(p.getY()+50);National Taiwan UniversityDepartment of Computer Science and Information EngineeringSwing元件Swing不是某些字的縮寫Why Swing?java.awt.*的缺點:不夠用:如表格事件處理模式與使用的作業平台相關性太高(heavy weight components),需要lightweight components來修正awt的缺點Swing packagejavax.swing.*;class JButton,JTextField,JeditorPane,JSlider,class JFrame,JApplet,class JList,JComboBox,JTable,JTree,Javax.swing.event:National Taiwan UniversityDepartment of Computer Science and Information EngineeringExamplepublic class Frame1 extends JFrame JButton jButton1=new JButton();JTextPane jTextPane1=new JTextPane();public Frame1()try jbInit();catch(Exception e)e.printStackTrace();public static void main(String args)Frame1 frame1=new Frame1();frame1.setSize(200,200);frame1.setVisible(true);private void jbInit()throws Exception jButton1.setText(jButton1);jButton1.addActionListener(new java.awt.event.ActionListener()public void actionPerformed(ActionEvent e)jButton1_actionPerformed(e););jTextPane1.setText(jTextPane1);this.getContentPane().setBackground(Color.red);this.getContentPane().add(jButton1,BorderLayout.NORTH);this.getContentPane().add(jTextPane1,BorderLayout.CENTER);void jButton1_actionPerformed(ActionEvent e)jTextPane1.setText(jTextPane1.getText()+Hello);National Taiwan UniversityDepartment of Computer Science and Information Engineering討論swing慢慢的取代awt,直接學swing?swing是架在awt上面發展的,所以awt不會消失,只是少用而已.儘量不要把swing和awt元件隨便混用,尤其是當利用視覺化設計工具時.National Taiwan UniversityDepartment of Computer Science and Information Engineering提示GUI元件元件的使用,必須靠類別庫中的類別類別以產生可以實際應用的物件物件。在以下的文章中,凡是提到GUI元件時,我們不嚴格區分類別類別、元件元件與物件物件三個名詞,並將此三個名詞交替著使用。AWT類別庫裏的類別因為與原來平台的GUI介面息息相關,因此我們將之稱為重量級(heavy-weighted);Swing類別因為不倚賴原來平台的GUI介面,因此我們將之稱為輕量級(light-weighted)。National Taiwan UniversityDepartment of Computer Science and Information Engineering使用AWT元件a.AWT元件都是class component的子類別b.注意大小寫使用元件的一般規則除了Windows類別會自動顯示在螢幕上之外,其他屬於class component的物件都必須先加入container物件之中Frame與Diglog物件屬於最上職的元件容器,視為不能加到container物件之中的元件National Taiwan UniversityDepartment of Computer Science and Information EngineeringAWT共同父類別介紹Class component基本繒圖:paint(),update(),repaint()某元件呼叫repaint()-AWT(系統)回應:呼叫paint(),update()只有Applet,Canvas,Panel需要呼叫repaint()字型與顏色控制setForground(),setBackground(),可見度控制setVisible(boolean b);開啟或關閉元件setEnable(boolean);事件處理外觀控制National Taiwan UniversityDepartment of Computer Science and Information EngineeringAWT共同父類別介紹Class containeradd(Component component)class MyFrame extends Frame implements ActionListener TextField tf=new TextField();public MyFrame()this.setLayout(null);tf.setBounds(30,30,80,30);add(tf);National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(1/8)按鈕元件的使用必須透過java.awt類別庫中的Button類別,Button類別的建構函數與其他方法的用法如下:National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(2/8)以下範例程式說明按鈕元件的應用:範例程式(檔名:按鈕測試1.java)1./檔名:按鈕測試1.java2./說明:在小程式顯示視窗中加入一個按鈕(Button)元件3.import java.applet.Applet;/引入Applet類別4.import java.awt.Button;/引入Button類別5.public class 按鈕測試1 extends Applet 6.public void init()7.Button 按鈕=new Button(歡迎來到Java世界);8.add(按鈕);9./*以上二行可以使用下列一個敘述即可以達到同樣效果10.add(new Button(歡迎來到Java世界);11.*/12./方法:init()定義區塊結束13./類別:按鈕測試1 定義區塊結束National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(3/8)網頁檔案(檔名:按鈕測試1網頁.html)1.2.3.4.執行結果(命令視窗指令:appletviewer 按鈕測試1網頁.html)National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(4/8)在範例程式按鈕測試1.java中,我們撰寫了一個小程式(applet),並使用init()方法,在小程式顯示視窗中安排一個按鈕(Button),而且此按鈕上有歡迎來到Java世界的標記。我們可將滑鼠游標移到此按鈕上並按下滑鼠左鍵,這會有一個按鈕被按下的感覺,但按鈕被按下後卻沒有什麼特別的狀況發生,這是因為尚未將按鈕被按下的事件(event)加以指定處理程序之故。Java的事件處理我們將留到下一章中介紹。在第7行我們使用new運算子產生一個Button類別的物件 按鈕,在第8行使用add(按鈕)將此物件加入小程式顯示視窗中。add是Applet類別中的一個方法,用以將元件物件加入小程式顯示視窗中。National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(5/8)Applet類別的add方法用法摘要如下:National Taiwan UniversityDepartment of Computer Science and Information Engineering按鈕(Button)元件(6/8)範例程式(檔名:按鈕測試2.java)1./檔名:按鈕測試2.java2./說明:在小程式顯示視窗中加入八個按鈕(Button)元件3.import java.applet.Applet;/引入Applet類別4.import java.awt.Button;/引入B