National Taiwan University Department of Computer Science and

  • Slides: 115
Download presentation
National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件以及Event Lecturer:曾學文

National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件以及Event Lecturer:曾學文

National Taiwan University Department of Computer Science and Information Engineering 程式設計的方法 [純手 打造]vs. [使用API]

National Taiwan University Department of Computer Science and Information Engineering 程式設計的方法 [純手 打造]vs. [使用API] a) 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及API

National Taiwan University Department of Computer Science and Information Engineering Class 如何了解一個class與相關的API class =

National Taiwan University Department of Computer Science and Information Engineering Class 如何了解一個class與相關的API class = data member +member functions 無法自己寫(或不熟), 至少試著看懂現成的類別規格, e. g. , class Applet, Graphics 多練習, 以能run為原則

National Taiwan University Department of Computer Science and Information Engineering JAVA使用者介面簡介 AWT(abstract window Toolkit):

National Taiwan University Department of Computer Science and Information Engineering JAVA使用者介面簡介 AWT(abstract window Toolkit): Java環境中, 專供程式設計GUI之用的類別集合統稱之 (package java. awt. *) e. g. , class Button, Txet. Field (可import java. awt. Button, java. awt. Text. Field, …) AWT元件 基本控制元件: Button, Check. Box, Choice, List, Menu, Text. Field 什麼是元件(Component): 是指awt類別所生成的物件

National Taiwan University Department of Computer Science and Information Engineering JAVA使用者介面簡介 其他取得輸入的元件: Slider, Scoll.

National Taiwan University Department of Computer Science and Information Engineering JAVA使用者介面簡介 其他取得輸入的元件: Slider, Scoll. Bar與Text. Area 建立自己的元件: Canvas, 有圖案的按鈕 標籤(Lable) 元件的容器: 可以利用add() Method 將元件(如Button)加入 類別(物件)稱之 • Window, Diglog, File. Diglog, Frame • Panel, Applet 其他AWT類別 Java. awt. * • Dimension, Insert, Point, Rectangle, Polygon: 指定表示大小與形狀 的類別 Java. awt. event. *

National Taiwan University Department of Computer Science and Information Engineering AWT元件階層圖

National Taiwan University Department of Computer Science and Information Engineering AWT元件階層圖

National Taiwan University Department of Computer Science and Information Engineering GUI程式剖析 手寫版 <Frame 1.

National Taiwan University Department of Computer Science and Information Engineering GUI程式剖析 手寫版 <Frame 1. java> public class Frame 1 { public static void main(String[] args) { Frame frame = new Frame("First Window Program"); frame. set. Layout(new Grid. Layout(7, 1)); frame. add(new Label("喜好選擇(可複選):")); // Label元件 frame. add(new Checkbox("音樂")); // Checkbox元件 frame. add(new Checkbox("體育")); frame. add(new Checkbox("美術")); Choice c 1 = new Choice(); // Choice元件 c 1. add("Green"); c 1. add("Red"); c 1. add("Blue"); frame. add(c 1); List ls 1 = new List(3, false); // List元件 ls 1. add("一年級"); ls 1. add("二年級"); ls 1. add("三年級"); frame. add(ls 1); frame. add(new Button("測試按鈕")); // Button元件 frame. pack(); //調整視窗大小以容納所有元件 frame. set. Visible(true); //顯示視窗 System. out. println("結束視窗程式,請按下CTRL+C"); }

National Taiwan University Department of Computer Science and Information Engineering 事件的處理過程 public class Frame

National Taiwan University Department of Computer Science and Information Engineering 事件的處理過程 public class Frame 1 extends Frame implements Action. Listener { Text. Field tf = new Text. Field(); Button b = new Button("Hi"); public static void main(String[] args) { Frame 1 mf = new Frame 1(); mf. set. Bounds(10, 150, 100); mf. set. Visible(true); } public Frame 1(){ this. set. Layout(null); tf. set. Bounds(30, 80, 30); b. set. Bounds(new Rectangle(30, 80, 50, 30)); b. add. Action. Listener(this); add(tf); add(b); } public void action. Performed(Action. Event e) { tf. set. Text("Hello"); } }

National Taiwan University Department of Computer Science and Information Engineering 更清楚顯示事件的處理過程 public class Frame

National Taiwan University Department of Computer Science and Information Engineering 更清楚顯示事件的處理過程 public class Frame 1 { public static void main(String[] args) { My. Frame mf = new My. Frame(); Event. Source. Frame esf = new Event. Source. Frame(); mf. set. Bounds(10, 150, 100); esf. set. Bounds(180, 100, 100); esf. register. Event. Listener(mf); mf. set. Visible(true); esf. set. Visible(true); } } class My. Frame extends Frame implements Action. Listener { Text. Field tf = new Text. Field(); public My. Frame(){ this. set. Layout(null); tf. set. Bounds(30, 80, 30); add(tf); } public void action. Performed(Action. Event e) { tf. set. Text("Hello"); } } class Event. Source. Frame extends Frame{ Button b = new Button("Hi"); public void register. Event. Listener(Action. Listener AL) { b. add. Action. Listener(AL); } public Event. Source. Frame(){ this. set. Layout(null); b. set. Bounds(new Rectangle(30, 50, 30)); this. add(b); } }

National Taiwan University Department of Computer Science and Information Engineering 使用JBuilder視覺化設計 具 public class

National Taiwan University Department of Computer Science and Information Engineering 使用JBuilder視覺化設計 具 public class Frame 1 extends JFrame { Button button 1 = new Button(); Text. Field text. Field 1 = new Text. Field(); public Frame 1() { try { jb. Init(); } catch(Exception e) { e. print. Stack. Trace(); } } public static void main(String[] args) { Frame 1 frame 1 = new Frame 1(); frame 1. set. Size(200, 100); frame 1. set. Visible(true); } private void jb. Init() throws Exception { button 1. set. Label("button 1"); button 1. add. Action. Listener(new java. awt. event. Action. Listener() { public void action. Performed(Action. Event e) { button 1_action. Performed(e); } }); text. Field 1. set. Text("text. Field 1"); this. get. Content. Pane(). add(button 1, Border. Layout. NORTH); this. get. Content. Pane(). add(text. Field 1, Border. Layout. CENTER); } void button 1_action. Performed(Action. Event e) { text. Field 1. set. Text("Hi"); } }

National Taiwan University Department of Computer Science and Information Engineering 討論 用那一種好? 視覺化程式設計的迷失 (程式產生器,

National Taiwan University Department of Computer Science and Information Engineering 討論 用那一種好? 視覺化程式設計的迷失 (程式產生器, program generator)

National Taiwan University Department of Computer Science and Information Engineering Events 以物件來表示 所有的事件都是 Event.

National Taiwan University Department of Computer Science and Information Engineering Events 以物件來表示 所有的事件都是 Event. Object的子類別 n所有的訊息都包含在java. awt. event類別庫內

National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件以及Event 以My. GUI了解Event (My.

National Taiwan University Department of Computer Science and Information Engineering 以GUI為例了解物件以及Event 以My. GUI了解Event (My. GUI. class、My. GUI. form) public My. GUI() //My. GUI. java { button. Plus. add. Action. Listener(new Action. Listener() { public void action. Performed(Action. Event e) { int var. A = Integer. parse. Int(text. A. get. Text()); int var. B = Integer. parse. Int(text. B. get. Text()); Integer var. C = new Integer(var. A+var. B); text. C. set. Text(var. C. to. String()); } }); }

National Taiwan University Department of Computer Science and Information Engineering 委派事件模型 1. 事先有註冊 按鈕

National Taiwan University Department of Computer Science and Information Engineering 委派事件模型 1. 事先有註冊 按鈕 button. Plus 2. 按下按鈕產生一個 Event物件傳給action. Listner action. Listener 3. 根據物件的種類 指派給事件處理者 action. Performed

National Taiwan University Department of Computer Science and Information Engineering 系統實際運作狀況 當事件發生時,會有一個事件ID產生 GUI元件使用這個ID碼,呼叫對應的事件方法 假如收到有Action.

National Taiwan University Department of Computer Science and Information Engineering 系統實際運作狀況 當事件發生時,會有一個事件ID產生 GUI元件使用這個ID碼,呼叫對應的事件方法 假如收到有Action. Event這種物件規格 從全部已註冊的Action. Listeners中,選出欲呼叫的 action. Performed() 方法

National Taiwan University Department of Computer Science and Information Engineering 另一個版本 class My. Listener

National Taiwan University Department of Computer Science and Information Engineering 另一個版本 class My. Listener implements Action. Listener //介面 { public void action. Performed(Action. Event e) //實現這個介面一定要 { //實作action. Performed int var. A = Integer. parse. Int(text. A. get. Text()); int var. B = Integer. parse. Int(text. B. get. Text()); Integer var. C = new Integer(var. A+var. B); text. C. set. Text(var. C. to. String()); } } public My. GUI 2() // My. GUI 2. java { My. Listener listener = new My. Listener(); // button. Plus. add. Action. Listener(listener); }

National Taiwan University Department of Computer Science and Information Engineering Event的註冊 Event 產生時,只會通知有註冊過的Listener。所以對必須要 先把Event『註冊』給要負責處理的Listner

National Taiwan University Department of Computer Science and Information Engineering Event的註冊 Event 產生時,只會通知有註冊過的Listener。所以對必須要 先把Event『註冊』給要負責處理的Listner 註冊所有想要擷取的事件,而當使用者啟動的事件並不 是我們所想要的事件時,就不加以理會 程式上以XX. add. XXListener 來完成註冊 button. add. Action. Listener(new Action. Listener() … 一個event source 可以被好幾個listener 所註冊,同樣地, 一個listener 也可以註冊好幾個event source 所有的Event Listener 都是一種interface,裡面只有定義這個 Listener所提供的抽象method 必須去實作出此listener interface 內所有的method

National Taiwan University Department of Computer Science and Information Engineering Action. Listener 都是Event. Listener的子類別

National Taiwan University Department of Computer Science and Information Engineering Action. Listener 都是Event. Listener的子類別 Action Type Action. Listener Component. Event Component. Listener Focus. Event Focus. Listener Key. Event Key. Listener Container. Event Container. Listener Window. Event Window. Listener Item. Event Item. Listener Adjust. Event Adjust. Listener Text. Event Text. Listener Action. Event Action. Listener

National Taiwan University Department of Computer Science and Information Engineering 處理的方法 Button Check. Box

National Taiwan University Department of Computer Science and Information Engineering 處理的方法 Button Check. Box Component

National Taiwan University Department of Computer Science and Information Engineering Your Turn 實作出My. Calc小算盤

National Taiwan University Department of Computer Science and Information Engineering Your Turn 實作出My. Calc小算盤 (My. Calc. class、My. Calc. form) Form已經建好了 完成計算機功能 @代表 00 S取平方根 % 例如 50*10% 5

National Taiwan University Department of Computer Science and Information Engineering 如何撰寫滑鼠移動監聽器 (Mouse. Motion. Listener)

National Taiwan University Department of Computer Science and Information Engineering 如何撰寫滑鼠移動監聽器 (Mouse. Motion. Listener) [需求]: 想攔截滑鼠事件(Mouse. Event)->實作Mouse. Listener public class Myclass implements Mouse. Listener{ some. Object. add. Mouse. Listener(this); public void mouse. Clicked(Mouse. Event e){…} public void mouse. Enetered(Mouse. Event e){…} public void mouse. Exited(Mouse. Event e){…} public void mouse. Pressed(Mouse. Event e){…} public void mouse. Released(Mouse. Event e){…} }

National Taiwan University Department of Computer Science and Information Engineering 使用轉接類別(Adapter Class)來簡化監聽者 的負擔 public

National Taiwan University Department of Computer Science and Information Engineering 使用轉接類別(Adapter Class)來簡化監聽者 的負擔 public void Mouse. Adapter implements Mouse. Listener { public void mouse. Clicked(Mouse. Event e){…} public void mouse. Enetered(Mouse. Event e){…} public void mouse. Exited(Mouse. Event e){…} public void mouse. Pressed(Mouse. Event e){…} public void mouse. Released(Mouse. Event e){…} } public class My. Class extends Mouse. Adapter{ public void mouse. Clicked(Mouse. Event e){…} } 缺點?

National Taiwan University Department of Computer Science and Information Engineering 如何撰寫按鍵監聽器(Key. Listener) implements Key.

National Taiwan University Department of Computer Science and Information Engineering 如何撰寫按鍵監聽器(Key. Listener) implements Key. Listener public interface Key. Listener { public void key. Pressed(Key. Event e){…} public void key. Released(Key. Event e){…} public void key. Typed(Key. Event e){…} } 使用class Key. Adapter

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame 1 extends Frame { button 1. add. Key. Listener(new java. awt. event. Key. Adapter() Button button 1 = new Button(); { public Frame 1() { public void key. Press(Key. Event e){ try { button 1_key. Pressed(e); jb. Init(); } } } catch(Exception e) { ); e. print. Stack. Trace(); this. set. Layout(null); } this. add. Key. Listener(new java. awt. event. Key. Adapter() { } public void key. Released(Key. Event e) { public static void main(String[] args) { this_key. Released(e); Frame 1 f 1 = new Frame 1(); } f 1. set. Bounds(10, 400, 200); public void key. Typed(Key. Event e) { f 1. set. Visible(true); this_key. Typed(e); } } private void jb. Init() throws Exception { }); button 1. set. Label("Hello"); this. add(button 1, null); button 1. set. Bounds(new Rectangle(109, 86, 144, 48)); } button 1. add. Mouse. Listener(new java. awt. event. Mouse. Adapter() { public void mouse. Clicked(Mouse. Event e) { button 1_mouse. Clicked(e); } public void mouse. Pressed(Mouse. Event e) { button 1_mouse. Pressed(e); }

National Taiwan University Department of Computer Science and Information Engineering Example void button 1_mouse.

National Taiwan University Department of Computer Science and Information Engineering Example void button 1_mouse. Clicked(Mouse. Event e) { public void button 1_key. Pressed(Key. Event e){ Point p = button 1. get. Location(); button 1. set. Location((int)(p. get. X()+50), (int)(p. get. Y())); if(e. get. Key. Char()==(char)'i') } { button 1. set. Location((int)(p. get. X()+50), (int)(p. get. Y())); void button 1_mouse. Pressed(Mouse. Event e) { } Point p = button 1. get. Location(); else if(e. get. Key. Char()==(char)'j') button 1. set. Location((int)(p. get. X()-50), (int)(p. get. Y())); button 1. set. Location((int)(p. get. X()), (int)(p. get. Y()+50)); } } void this_key. Typed(Key. Event e) { Point p = button 1. get. Location(); void this_key. Released(Key. Event e) { button 1. set. Location((int)(p. get. X()), (int)(p. get. Y()+50)); Point p = button 1. get. Location(); } if(e. get. Key. Char()==(char)'i') } { button 1. set. Location((int)(p. get. X()+50), (int)(p. get. Y())); } else if(e. get. Key. Char()==(char)'j') button 1. set. Location((int)(p. get. X()-50), (int)(p. get. Y())); }

National Taiwan University Department of Computer Science and Information Engineering Swing元件 Swing不是某些字的縮寫 Why Swing?

National Taiwan University Department of Computer Science and Information Engineering Swing元件 Swing不是某些字的縮寫 Why Swing? java. awt. *的缺點: 不夠用: 如表格 事件處理模式與使用的作業平台相關性太高 (heavy weight components), 需要lightweight components來修正awt的缺點 Swing package javax. swing. *; • class JButton, JText. Field, Jeditor. Pane, JSlider, … • class JFrame, JApplet, … • class JList, JCombo. Box, JTable, JTree, … Javax. swing. event:

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame 1 extends JFrame { JButton j. Button 1 = new JButton(); JText. Pane j. Text. Pane 1 = new JText. Pane(); public Frame 1() { try { jb. Init(); } catch(Exception e) { e. print. Stack. Trace(); } } public static void main(String[] args) { Frame 1 frame 1 = new Frame 1(); frame 1. set. Size(200, 200); frame 1. set. Visible(true); } private void jb. Init() throws Exception { j. Button 1. set. Text("j. Button 1"); j. Button 1. add. Action. Listener(new java. awt. event. Action. Listener() { public void action. Performed(Action. Event e) { j. Button 1_action. Performed(e); } }); j. Text. Pane 1. set. Text("j. Text. Pane 1"); this. get. Content. Pane(). set. Background(Color. red); this. get. Content. Pane(). add(j. Button 1, Border. Layout. NORTH); this. get. Content. Pane(). add(j. Text. Pane 1, Border. Layout. CENTER); } void j. Button 1_action. Performed(Action. Event e) { j. Text. Pane 1. set. Text(j. Text. Pane 1. get. Text()+"Hello"); } }

National Taiwan University Department of Computer Science and Information Engineering 討論 swing慢慢的取代awt, 直接學swing? swing是架在awt上面發展的,

National Taiwan University Department of Computer Science and Information Engineering 討論 swing慢慢的取代awt, 直接學swing? swing是架在awt上面發展的, 所以awt不會消失, 只是 少用而已. 儘量不要把swing和awt元件隨便混用, 尤其是當利用 視覺化設計 具時.

National Taiwan University Department of Computer Science and Information Engineering 使用AWT元件 a. AWT元件都是class component的子類別

National Taiwan University Department of Computer Science and Information Engineering 使用AWT元件 a. AWT元件都是class component的子類別 b. 注意大小寫 使用元件的一般規則 除了Windows類別會自動顯示在螢幕上之外, 其他屬於 class component的物件都必須先加入container物件之中 Frame與Diglog物件屬於最上職的元件容器, 視為不能加 到container物件之中的元件

National Taiwan University Department of Computer Science and Information Engineering AWT共同父類別介紹 Class component 基本繒圖:

National Taiwan University Department of Computer Science and Information Engineering AWT共同父類別介紹 Class component 基本繒圖: paint(), update(), repaint() • 某元件呼叫repaint()->AWT(系統)回應: 呼叫paint(), update() • 只有Applet, Canvas, Panel需要呼叫repaint() 字型與顏色控制 • set. Forground(), set. Background(), … 可見度控制 • set. Visible(boolean b); 開啟或關閉元件 • set. Enable(boolean); 事件處理 外觀控制

National Taiwan University Department of Computer Science and Information Engineering AWT共同父類別介紹 Class container add(Component

National Taiwan University Department of Computer Science and Information Engineering AWT共同父類別介紹 Class container add(Component component) class My. Frame extends Frame implements Action. Listener { Text. Field tf = new Text. Field(); public My. Frame(){ this. set. Layout(null); tf. set. Bounds(30, 80, 30); add(tf); }

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件 (1/8) 按鈕元件的使用必須透過java. awt類別庫中的Button類別,

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件 (1/8) 按鈕元件的使用必須透過java. awt類別庫中的Button類別, Button類別的建構函數與其他方法的用法如下:

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(2/8) 以下範例程式說明按鈕元件的應用: 範例程式(檔名: 按鈕測試

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(2/8) 以下範例程式說明按鈕元件的應用: 範例程式(檔名: 按鈕測試 1. java) 1. //檔名:按鈕測試 1. java 2. //說明:在小程式顯示視窗中加入一個按鈕(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 University Department of Computer Science and Information Engineering 按鈕(Button)元件(3/8) 網頁檔案(檔名:按鈕測試 1網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(3/8) 網頁檔案(檔名:按鈕測試 1網頁. html) 1. 2. 3. 4. <html> <applet code="按鈕測試 1. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 按鈕測試 1網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(5/8) Applet類別的add方法用法摘要如下:

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(5/8) Applet類別的add方法用法摘要如下:

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(6/8) 範例程式(檔名: 按鈕測試 2.

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(6/8) 範例程式(檔名: 按鈕測試 2. java) 1. 2. 3. 4. 5. 6. 7. 8. //檔名:按鈕測試 2. java //說明:在小程式顯示視窗中加入八個按鈕(Button)元件 import java. applet. Applet; //引入Applet類別 import java. awt. Button; //引入Button類別 public class 按鈕測試 2 extends Applet { public void init() { add( new Button("按鈕 1") ); add( new Button("按鈕 2") );

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(7/8) 9. 10. 11.

National Taiwan University Department of Computer Science and Information Engineering 按鈕(Button)元件(7/8) 9. 10. 11. 12. 13. 14. 15. 16. add( new Button("按鈕 3") ); add( new Button("按鈕 4") ); add( new Button("按鈕 5") ); add( new Button("按鈕 6") ); add( new Button("按鈕 7") ); add( new Button("按鈕 8") ); } //方法: init() 定義區塊結束 } //類別: 按鈕測試 2 定義區塊結束 網頁檔案(檔名:按鈕測試 2網頁. html) 1. 2. 3. 4. <html> <applet code="按鈕測試 2. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 按鈕測試 2網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (4/16) 例如,若我們在程式按鈕測試 2.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (4/16) 例如,若我們在程式按鈕測試 2. java中第 7行之前加入 set. Layout(new Flow. Layout(Flow. Layout. RIGHT)); 則所有的按鈕元件都會向右靠齊,其結果如下圖所示:

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (6/16)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (6/16)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (8/16) 例如,在按鈕測試 3.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (8/16) 例如,在按鈕測試 3. java中的第 10行,我們使用敘述 set. Layout(new Grid. Layout(4, 2)); 來將所有的按鈕以 4列 2欄的格狀來安排版面配置。 範例程式(檔名: 按鈕測試 3. java) 1. 2. 3. 4. 5. //檔名:按鈕測試 3. java //說明:在小程式顯示視窗中以 4列 2欄的格狀加入八個按鈕 (Button)元件 import java. applet. Applet; //引入Applet類別 import java. awt. Button; //引入Button類別 import java. awt. Grid. Layout; //引入Grid. Layout類別

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (9/16) 6. 7.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (9/16) 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. public class 按鈕測試 3 extends Applet { public void init() { set. Layout(new Grid. Layout(4, 2) ); add( new Button("按鈕 1") ); add( new Button("按鈕 2") ); add( new Button("按鈕 3") ); add( new Button("按鈕 4") ); add( new Button("按鈕 5") ); add( new Button("按鈕 6") ); add( new Button("按鈕 7") ); add( new Button("按鈕 8") ); } //方法: init() 定義區塊結束 } //類別: 按鈕測試 3定義區塊結束

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (10/16) 網頁檔案(檔名:按鈕測試 3網頁.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (10/16) 網頁檔案(檔名:按鈕測試 3網頁. html) 1. 2. 3. 4. <html> <applet code="按鈕測試 3. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 按鈕測試 3網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (13/16) 範例程式(檔名: 按鈕測試

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (13/16) 範例程式(檔名: 按鈕測試 4. java) //檔名:按鈕測試 4. java 2. //說明:在小程式顯示視窗中以邊界配置方式加入五個按鈕 (Button)元件 3. import java. applet. Applet; //引入Applet類別 4. import java. awt. Button; //引入Button類別 5. import java. awt. Border. Layout; //引入Border. Layout類別 6. public class 按鈕測試 4 extends Applet { 7. public void init() { 8. set. Layout(new Border. Layout() ); 9. add("East", new Button("東按鈕")); 10. add("South", new Button("南按鈕")); 1.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (14/16) 1. 2.

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (14/16) 1. 2. 3. 4. 5. add("West", new Button("西按鈕")); add("North", new Button("北按鈕")); add("Center", new Button("中按鈕")); } //方法: init() 定義區塊結束 } //類別: 按鈕測試 4定義區塊結束 網頁檔案(檔名:按鈕測試 4網頁. html) 1. 2. 3. 4. <html> <applet code="按鈕測試 4. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 按鈕測試 4網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (16/16)

National Taiwan University Department of Computer Science and Information Engineering 版面配置 (16/16)

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (2/5)

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (2/5)

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (3/5) 以下是類別Label所屬方法的綜合應用程式: 範例程式(檔名:

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (3/5) 以下是類別Label所屬方法的綜合應用程式: 範例程式(檔名: 標記測試. java) 1. 2. 3. 4. 5. 6. 7. 8. //檔名: 標記測試. java //說明: 各種標記方法之綜合應用 import java. applet. Applet; //引入Applet類別 import java. awt. Label; //引入Label類別 import java. awt. Grid. Layout; //引入Grid. Layout類別 public class 標記測試 extends Applet{ public void init() { Label 標記 1=new Label();

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (4/5) 1. 2.

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (4/5) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. Label 標記 2=new Label("向中靠"); Label 標記 3=new Label("向右靠", 2); 代 標記 1. set. Text("向左靠"); 標記 1. set. Alignment(0); 標記 2. set. Alignment(1); 代 set. Layout(new Grid. Layout(3, 1)); add(標記 1); add(標記 2); add(標記 3); } //方法: init() 定義區塊結束 } //類別: 標記測試 定義區塊結束 //2可以使用Label. RIGHT取 //0可以使用Label. LEFT取代 //1可以使用Label. CENTER取

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (5/5) 網頁檔案(檔名:標記測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 標記(Label)元件 (5/5) 網頁檔案(檔名:標記測試網頁. html) 1. 2. 3. 4. <html> <applet code="標記測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 標記測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (2/6)

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (2/6)

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (3/6)

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (3/6)

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (4/6) 以下是類別Checkbox所屬方法的綜合應用範例程式: 範例程式(檔名:

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (4/6) 以下是類別Checkbox所屬方法的綜合應用範例程式: 範例程式(檔名: 選項測試. java) 1. public class 選項測試 extends Applet{ 2. public void init() { 3. Label 標記=new Label("請選擇以下一項"); 4. Checkbox. Group 選項群=new Checkbox. Group(); 5. Checkbox 選項1=new Checkbox(); 6. Checkbox 選項2=new Checkbox("第二項"); 7. Checkbox 選項3=new Checkbox("第三項", false); 8. Checkbox 選項4=new Checkbox("第四項", false, 選項群);

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (5/6) 9. 10.

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (5/6) 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. Checkbox 選項5=new Checkbox("第五項", 選項群, false); 選項1. set. Label("第一項"); 選項1. set. Checkbox. Group(選項群); 選項2. set. Checkbox. Group(選項群); 選項3. set. Checkbox. Group(選項群); 選項1. set. State(true); 選項2. set. State(false); set. Layout(new Grid. Layout(6, 1)); add(標記); add(選項1);

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (6/6) 19. 20.

National Taiwan University Department of Computer Science and Information Engineering 選項(Checkbox)元件 (6/6) 19. 20. 21. 22. 23. 24. add(選項2); add(選項3); add(選項4); add(選項5); } //方法: init() 定義區塊結束 } //類別: 選項測試 定義區塊結束 網頁檔案(檔名:選項測試網頁. html) 1. 2. 3. 4. <html> <applet code="選項測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer選項測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (2/6)

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (2/6)

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (3/6)

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (3/6)

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (4/6) 以下是類別Choice所屬方法的綜合應用程式: 範例程式(檔名:

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (4/6) 以下是類別Choice所屬方法的綜合應用程式: 範例程式(檔名: 選單測試. java) 1. 2. 3. 4. 5. 6. 7. 8. import java. awt. Label; //引入Label類別 import java. awt. Choice; //引入Choice類別 import java. awt. Grid. Layout; //引入Grid. Layout類別 public class 選單測試 extends Applet{ public void init() { Label 標記=new Label("請選擇以下一項"); Choice 選單=new Choice(); 選單. add("第 1項");

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (5/6) 1. 2.

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (5/6) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 選單. add. Item("第 2項"); 選單. add. Item("第 3項"); 選單. add. Item("第 4項"); 選單. add. Item("第 5項"); 選單. insert("第 2. 1項", 2); 選單. select("第 2項"); 選單. select(1); String 訊息字串="(選單一共有"+選單. get. Item. Count()+ "項,由 0開始編號,編號為 5的項目為"+選單. get. Item(5)+")"; Label 訊息標記=new Label(訊息字串); add(標記);

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (6/6) 1. 2.

National Taiwan University Department of Computer Science and Information Engineering 選單(Choice)元件 (6/6) 1. 2. 3. 4. add(訊息標記); add(選單); } //方法: init() 定義區塊結束 } //類別: 選單測試 定義區塊結束 網頁檔案(檔名:選單測試網頁. html) 1. 2. 3. 4. <html> <applet code="選單測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer選單測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (2/7)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (2/7)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (3/7)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (3/7)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (4/7) 以下是類別List所屬方法的綜合應用程式: 範例程式(檔名:

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (4/7) 以下是類別List所屬方法的綜合應用程式: 範例程式(檔名: 清單測試. java) 1. 2. 3. 4. 5. 6. 7. 8. //檔名: 清單測試. java //說明: 清單(List)類別各種方法之綜合應用 import java. applet. Applet; //引入Applet類別 import java. awt. Label; //引入Label類別 import java. awt. List; //引入List類別 import java. awt. Grid. Layout; //引入Grid. Layout類別 public class 清單測試 extends Applet{ public void init() {

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (5/7) 9. 10.

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (5/7) 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Label 標記=new Label("請選擇以下一項"); List 清單=new List(5); 清單. set. Multiple. Mode(true); 清單. add("第 1項"); 清單. add("第 2項"); 清單. add("第 3項"); 清單. add("第 4項"); 清單. add("第 5項"); 清單. add("第 2. 1項", 2); 清單. select(1); 清單. select(3);

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (6/7) 20. 21.

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (6/7) 20. 21. 22. 23. 24. 25. 26. 27. 28. 清單. select(5); String 訊息字串="(清單一共有"+清單. get. Item. Count()+ "項,由 0開始編號,編號為 5的項目為"+清 單. get. Item(5)+")"; Label 訊息標記=new Label(訊息字串); add(標記); add(訊息標記); add(清單); } //方法: init() 定義區塊結束 } //類別: 清單測試 定義區塊結束

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (7/7) 網頁檔案(檔名:清單測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 清單(List)元件 (7/7) 網頁檔案(檔名:清單測試網頁. html) 1. 2. 3. 4. <html> <applet code="清單測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer清單測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 文字欄(Text. Field)元件(2/3)

National Taiwan University Department of Computer Science and Information Engineering 文字欄(Text. Field)元件(2/3)

National Taiwan University Department of Computer Science and Information Engineering 文字欄(Text. Field)元件(3/3) 類別Text. Field所屬方法的說明將於下一小節再介紹。

National Taiwan University Department of Computer Science and Information Engineering 文字欄(Text. Field)元件(3/3) 類別Text. Field所屬方法的說明將於下一小節再介紹。

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (2/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (2/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (3/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (3/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (4/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (4/)

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (5/) 以下是類別Text.

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (5/) 以下是類別Text. Field及類別Text. Area所屬方法的綜合應用程式: 範例程式(檔名: 文字欄文字區測試. java) 1. //檔名: 文字欄文字區測試. java 2. //說明: 文字欄(Text. Field)文字區(Text. Area)各種方法之綜合應用 3. import java. applet. Applet; //引入Applet類別 4. import java. awt. *; //引入java. awt類別庫之所有類別 5. public class 文字欄文字區測試 extends Applet{ 6. public void init() { 7. Text. Field 文字欄1=new Text. Field("這是文字欄1裡的文字", 30); 8. Text. Field 文字欄2=new Text. Field(); 9. 文字欄2. set. Columns(30); 10. 文字欄2. set. Text("這是文字欄2裡的文字");

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (6/) 1.

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (6/) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 文字欄2. set. Echo. Char('*'); Text. Area 文字區=new Text. Area(5, 10); 文字區. set. Text("歡迎來到Java世界n你現在看到的是n「文字區」 元件"); 文字區. select. All(); add(new Label("文字欄1") ); add(文字欄1); add(new Label("文字欄2") ); add(文字欄2); add(new Label("文字區") ); add(文字區); } //方法: init() 定義區塊結束 } //類別: 文字欄文字區測試 定義區塊結束

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (7/) 網頁檔案(檔名:文字欄文字區測試網頁.

National Taiwan University Department of Computer Science and Information Engineering 文字區(Text. Area)元件 (7/) 網頁檔案(檔名:文字欄文字區測試網頁. html) 1. 2. 3. 4. <html> <applet code="文字欄文字區測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer文字欄文字區測試網 頁. html)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(2/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(2/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(3/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(3/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(4/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(4/7)

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(5/7) 以下是類別Scrollbar所屬方法的綜合應用程式: 範例程式(檔名: 捲軸測試.

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(5/7) 以下是類別Scrollbar所屬方法的綜合應用程式: 範例程式(檔名: 捲軸測試. java) 1. //檔名: 捲軸測試. java 2. //說明: 捲軸(Scrollbar)各種方法之綜合應用 3. import java. applet. Applet; //引入Applet類別 4. import java. awt. *; //引入java. awt類別庫之所有類別 5. public class 捲軸測試 extends Applet{ 6. public void init() { 7. Scrollbar 捲軸 1=new Scrollbar(); 8. Scrollbar 捲軸 2=new Scrollbar(Scrollbar. VERTICAL); 9. Scrollbar 捲軸 3=new Scrollbar(Scrollbar. HORIZONTAL, 100, 50, 0, 100); 10. Scrollbar 捲軸 4=new Scrollbar();

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(6/7) 1. 2. 3.

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(6/7) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 捲軸 2. set. Values(100, 10, 0, 100); 捲軸 4. set. Orientation(Scrollbar. HORIZONTAL); 捲軸 4. set. Value(50); 捲軸 4. set. Visible. Amount(25); 捲軸 4. set. Block. Increment(25); 捲軸 4. set. Unit. Increment(10); 捲軸 4. set. Minimum(0); 捲軸 4. set. Maximum(100); set. Layout(new Border. Layout()); add("West", 捲軸 1); add("East", 捲軸 2); add("North", 捲軸 3); add("South", 捲軸 4); } //方法: init() 定義區塊結束 } //類別: 捲軸測試 定義區塊結束

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(7/7) 網頁檔案(檔名:捲軸測試網頁. html) 1.

National Taiwan University Department of Computer Science and Information Engineering 捲軸(Scrollbar)元件(7/7) 網頁檔案(檔名:捲軸測試網頁. html) 1. 2. 3. 4. <html> <applet code="捲軸測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer捲軸測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(2/5) Container類別常用的方法摘要如下:

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(2/5) Container類別常用的方法摘要如下:

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(3/5) 以下是Container類別的衍生類別的綜合應用範例程式: 範例程式(檔名: 容器測試.

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(3/5) 以下是Container類別的衍生類別的綜合應用範例程式: 範例程式(檔名: 容器測試. java) 1. //檔名: 容器測試. java 2. //說明: 容器(Container) 類別的衍生類別的綜合應用 3. import java. applet. Applet; //引入Applet類別 4. import java. awt. *; //引入java. awt類別庫之所有類別 5. public class 容器測試 extends Applet{ 6. public void init() { 7. Frame 窗框=new Frame("窗框"); 8. Dialog 對話框=new Dialog(窗框, "對話框"); 9. File. Dialog 檔案對話框=new File. Dialog(窗框, "檔案對話框"); 10. Panel 面板 1=new Panel();

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(4/5) 1. 2. 3.

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(4/5) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. Panel 面板 2=new Panel(); 窗框. set. Size(300, 300); 窗框. set. Location(200, 200); 對話框. set. Size(200, 100); 對話框. set. Location(50, 50); 面板 1. set. Background(Color. red); 面板 2. set. Background(Color. blue); 面板 1. add(new Label("紅色")); 面板 2. add(new Label("藍色")); 對話框. set. Layout(new Grid. Layout(2, 1, 0, 0)); 對話框. add(面板 1); 對話框. add(面板 2);

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(5/5) 1. 2. 3.

National Taiwan University Department of Computer Science and Information Engineering 容器(Container)相關類別(5/5) 1. 2. 3. 4. 5. 窗框. set. Visible(true); 對話框. set. Visible(true); 檔案對話框. set. Visible(true); } //方法: init() 定義區塊結束 } //類別: 容器測試 定義區塊結束 網頁檔案(檔名:容器測試網頁. html) 1. 2. 3. 4. <html> <applet code="容器測試. class" width="800" height="600"> </applet> </html> 執行結果(命令視窗指令:appletviewer 容器測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (3/7)

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (3/7)

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (4/7) 以下是Menu. Component類別衍生類別的綜合應用範例程式:

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (4/7) 以下是Menu. Component類別衍生類別的綜合應用範例程式: 範例程式(檔名: 功能表測試. java) 1. //檔名: 功能表測試. java 2. //說明: 功能表(Menu)相關元件的綜合應用 3. import java. applet. Applet; //引入Applet類別 4. import java. awt. *; //引入awt的所有類別 5. public class 功能表測試 extends Applet { 6. Frame 窗框 = new Frame("一個包含功能表的測試窗框"); 7. Menu 功能表 1 = new Menu("檔案"); 8. Menu 功能表 2 = new Menu("設定"); 9. Menu 功能表 21 = new Menu("色彩"); 10. Menu 功能表 22 = new Menu("樣式");

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (5/7) 11. 12.

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (5/7) 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. Checkbox. Menu. Item 功能表 221 = new Checkbox. Menu. Item("粗體 ", true); Checkbox. Menu. Item 功能表 222 = new Checkbox. Menu. Item("斜體"); Checkbox. Menu. Item 功能表 223 = new Checkbox. Menu. Item("底線"); Menu 功能表 3 = new Menu("說明"); Menu. Bar 功能表區 = new Menu. Bar(); public void init() { 功能表 1. add("建新檔"); 功能表 1. add("開舊檔"); 功能表 1. add("存檔"); 功能表 1. add. Separator(); 功能表 1. add("離開"); 功能表 2. add(功能表 21);

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (6/7) 23. 24.

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (6/7) 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 功能表 2. add(功能表 22); 功能表 21. add("紅"); 功能表 21. add("綠"); 功能表 21. add("藍"); 功能表 22. add(功能表 221); 功能表 22. add(功能表 222); 功能表 22. add(功能表 223); 功能表 3. add("說明主題"); 功能表 3. add("內容"); 功能表 3. add. Separator(); 功能表 3. add("關於本軟體"); 功能表區. add(功能表 1);

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (7/7) 35. 36.

National Taiwan University Department of Computer Science and Information Engineering 功能表(Menu)相關元件 (7/7) 35. 36. 37. 38. 39. 40. 41. 42. 功能表區. add(功能表 2); 功能表區. set. Help. Menu(功能表 3); 窗框. set. Menu. Bar(功能表區); 窗框. set. Size(250, 250); 窗框. set. Resizable(true); 窗框. set. Visible(true); } //方法: init() 定義區塊結束 } //類別: 功能表測試 定義區塊結束 網頁檔案(檔名:容器測試網頁. html) 1. 2. 3. 4. <html> <applet code="容器測試. class" width="350" height="100"> </applet> </html> 執行結果(命令視窗指令:appletviewer 容器測試網頁. html)

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame

National Taiwan University Department of Computer Science and Information Engineering Example public class Frame 1 extends Frame { String passwd = "1234"; Checkbox. Group checkbox. Group 1 = new Checkbox. Group(); Checkbox checkbox 1 = new Checkbox(); Checkbox checkbox 2 = new Checkbox(); Text. Field text. Field 1 = new Text. Field(); Text. Field text. Field 2 = new Text. Field(); Label label 1 = new Label(); Label label 2 = new Label(); Button button 1 = new Button(); Text. Area text. Area 1 = new Text. Area(); Checkbox checkbox 3 = new Checkbox(); Checkbox checkbox 4 = new Checkbox(); Label label 3 = new Label(); public Frame 1() { try { jb. Init(); } catch(Exception e) { e. print. Stack. Trace(); } checkbox 1. set. Checkbox. Group(checkbox. Group 1); checkbox 2. set. Checkbox. Group(checkbox. Group 1); } public static void main(String[] args) { Frame 1 frame 1 = new Frame 1(); frame 1. set. Bounds(50, 360, 250); frame 1. set. Visible(true); }

National Taiwan University Department of Computer Science and Information Engineering Example private void jb.

National Taiwan University Department of Computer Science and Information Engineering Example private void jb. Init() throws Exception { checkbox 1. set. Label("套餐一(480)"); checkbox 1. set. Bounds(new Rectangle(57, 94, 83, 27)); this. set. Layout(null); checkbox 2. set. Label("套餐二(550)"); checkbox 2. set. Bounds(new Rectangle(55, 120, 82, 25)); text. Field 1. set. Echo. Char('*'); text. Field 1. set. Bounds(new Rectangle(81, 156, 100, 29)); text. Field 2. set. Bounds(new Rectangle(80, 202, 99, 31)); label 1. set. Text("密碼"); label 1. set. Bounds(new Rectangle(34, 164, 34, 25)); label 2. set. Text("金額"); label 2. set. Bounds(new Rectangle(33, 202, 35, 27)); button 1. set. Label("結算"); button 1. set. Bounds(new Rectangle(80, 242, 104, 47)); button 1. add. Action. Listener(new java. awt. event. Action. Listener() { public void action. Performed(Action. Event e) { button 1_action. Performed(e); } });

National Taiwan University Department of Computer Science and Information Engineering Example void button 1_action.

National Taiwan University Department of Computer Science and Information Engineering Example void button 1_action. Performed(Action. Event e) { text. Area 1. set. Bounds(new Rectangle(207, 99, 124, 128)); int money =0; checkbox 3. set. Label("會員"); checkbox 3. set. Bounds(new Rectangle(40, 64, 60, 18)); if(checkbox 3. get. State())money+=480; if(checkbox 4. get. State())money+=550; checkbox 4. set. Label("非會員"); checkbox 4. set. Bounds(new Rectangle(104, 65, 85, 17)); if(checkbox. Group 1. get. Selected. Checkbox()==checkbox 1) { label 3. set. Text("訊息欄"); if(text. Field 1. get. Text(). equals(passwd)) label 3. set. Bounds(new Rectangle(223, 61, 71, 20)); { this. add(button 1, null); text. Field 2. set. Text(""+money*0. 8); this. add(text. Field 2, null); text. Area 1. set. Text(text. Area 1. get. Text()+"會員打八折!n"); this. add(checkbox 1, null); } this. add(checkbox 2, null); else this. add(text. Field 1, null); { this. add(label 2, null); text. Area 1. set. Text(text. Area 1. get. Text()+"會員: 密碼錯誤 this. add(label 1, null); !n"); this. add(checkbox 4, null); } this. add(checkbox 3, null); } this. add(text. Area 1, null); else{ this. add(label 3, null); text. Field 2. set. Text(""+money*0. 95); } text. Area 1. set. Text(text. Area 1. get. Text()+"非會員打八折 !n"); } } }