Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java 程式設計基礎班 (9) 莊坤達 台大電信所網路資料庫研究室 Email: [email protected] Class 9 1 回顧 Java Swing/AWT Class 9 2 1 Java Swing/AWT -- Extension 完成踩地雷 Class 9 3 Why Is It Called An Applet? The Java applet is itself a fully functioning little program. It's an application, but it's a little application: An applet, in other words. Class 9 4 2 Applet 讓Java程式可透過web browser來執行的 一個方法 在client端執行 有一些安全上的限制 (Java sandbox) Class 9 5 Applet JDK有提供一個appletviewer,可以讓自己寫的 applet不用透過browser就可以在電腦上執行 使用方法Appletviewer yourhtml.html 用appletviewer執行和直接用Web browser開 啟有時是不一樣的,因為appletviewer是在 local端執行,所以安全性較高,java允許你存 取電腦上的檔案.但如果是在網路上執行,則 不能存取電腦上的檔案. Applet Security Class 9 http://java.sun.com/sfaq/ 6 3 Java Applet 每一個Applet程式都 必須繼承自Applet物 件 Class 9 7 Java Applet When you create applets, import the JApplet class (package javax.swing) import the Graphics class (package java.awt) to draw graphics Class 9 Can draw lines, rectangles ovals, strings of characters 8 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 // Fig. 3.6: WelcomeApplet.java // A first applet in Java. // Java packages import java.awt.Graphics; import javax.swing.JApplet; import allows us to use predefined classes (allowing us to use applets and import class Graphics graphics, in this case). // // import class JApplet public class WelcomeApplet extends JApplet { extends allows us to inherit the // draw text on applet’s background public void paint( Graphics g ) capabilities of class JApplet. { // call superclass version of method paint super.paint( g ); Program Output // draw a String at x-coordinate 25 and y-coordinate 25 g.drawString( "Welcome to Java Programming!", 25, 25 ); } // end method paint } // end class WelcomeApplet Method paint is guaranteed to be called in all applets. Its first line must be defined as above. Class 9 9 Java Applet paint method 繼承自JApplet By default, paint has empty body 我們重載(Override) paint這個method Methods paint, init, and start Guaranteed to be called automatically Our applet gets "free" version of these by inheriting from JApplet Every applet does not need all three methods Override the ones you need Applet container “draws itself” by calling method paint Class 9 10 5 Java Applet 執行順序 Init()Æstart()Æpaint() Init()在會這個applet被load進來時執行 Class 9 11 Applet跟HTML APPLET denotes to the browser an applet is starting here. CODEBASE tells the browser in which directory to find all the Applets. These Applets happen to be in a directory called CODEBASE. If the Applets are in the same directory as the page, this command is not needed. CODE denotes to the browser what applet will be used. Class 9 12 6 Applet跟HTML HEIGHT & WIDTH denote the "window" the applet will perform within. This is required and is set. Altering these numbers can stop the entire process. The exception to this rule is when you have an applet that uses a graphic. If you change the graphic, you also must change the HEIGHT and WIDTH to equal the new graphic. PARAM NAME is short for parameter name. This is a parameter set up in the applet itself. These are applet-specific commands. Just because a PARAM NAME works with one applet in no way assures it will work in another. This applet requires many parameter names. I will get into them in a moment. VALUE denotes what will be the actual parameter. /APPLET ends the whole deal. Class 9 13 Java Applet 1 2 3 4 <html> <applet code = "WelcomeApplet.class" width = "300" height = "45"> </applet> </html> <applet> tag Class 9 用來include一個applet在此網頁中 width and height用來設定applet出現的長寬 14 7 Java Applet 1 2 3 4 <html> <applet code=SortItem.class width=100 height=100><param name="alg“ value="QSort"> </applet> </html> 傳參數給applet,用param 和value Class 9 15 8