java编程语言基础外文,Java编程语言基础(外文文献翻译)
Java編程語言基礎(外文文獻翻譯)
JavaTM Programming Language Basics Like applications, applets are created from classes. However, applets do not have a main as an entry point, but instead, have several s to control specific aspects of applet cution. This lesson converts an application from Lesson 2 to an applet and describes the structure and elements of an applet. 1、Application to Applet 2、Run the Applet 3、Applet Structure and Elements 4、Packages 5、More Ination 1、Application to Applet The following code is the applet equivalent to the LessonTwoB application from Lesson 2. The figure below shows how the running applet looks. The structure and elements of the applet code are discussed after the section on how to run the applet just below. import et; import hics; import r; public class SimpleApplet extends Applet String text “I m a simple applet“; public void init text “I m a simple applet“; setBackground Color.cyan ; public void start Sytln “starting.“ ; public void stop Sytln “stopping.“ ; public void destroy Sytln “preparing to unload.“ ; public void paint Graphics g Sytln “Paint“ ; g.setColor Color.blue ; g.drawRect 0, 0, getSize .width -1, getSize .height -1 ; g.setColor Color.red ; g.drawString text, 15, 25 ; The SimpleApplet class is declared public so the program that runs the applet a browser or appletviewer , which is not local to the program can access it. 2、Run the Applet To see the applet in action, you need an 6>HTML file with the Applet tag as follows: APPLET CODE SimpleApplet.class WIDTH 200 HEIGHT 100 The easiest way to run the applet is with appletviewer shown below where simpleApplet.html is a file that contains the above HTML code: appletviewer simpleApplet.html Note: To run an applet written with JavaTM 2 APIs in a browser, the browser must be enabled for the Java 2 Plat. If your browser is not enabled for the Java 2 Plat, you have to use appletviewer to run the applet or install Java Plug-in. Java Plug-in lets you run applets on web pages under the 1.2 version of the Java VM instead of the web browser s default Java VM. 3、Applet Structure and Elements The Java API Applet class provides what you need to design the appearance and manage the behavior of an applet. This class provides a graphical user interface GUI component called a Panel and a number of s. To create an applet, you extend or subclass the Applet class and implement the appearance and behavior you want. The applet s appearance is created by drawing onto the Panel or by attaching other GUI components such as push buttons, scrollbars, or text areas to the Panel. The applet s behavior is defined by implementing the s. 3.1 Extending a Class Most classes of any complexity extend other classes. To extend another class means to write a new class that can use the fields and s defined in the class being extended. The class being extended is the parent class, and the class doing the extending is the child class. Another way to say this is the child class inherits the fields and s of its parent or chain of parents. Child classes either call or override inherited s. This is called single inheritance. The SimpleApplet class extends Applet class, which extends the Panel class, which extends the Container class. The Container class extends Object, which is the parent of all Java API classes. The Applet class provides the init, start, stop, destroy, and paint s you saw in the example applet. The SimpleApplet class overrides these s to do what the SimpleApplet class needs them to do. The Applet class provides no functionality for these s. However, the Applet class does provide functionality for the setBackground ,which is called in the init . The call to setBackground is an example of calling a inherited from a parent class in contrast to overriding a inherited from a parent class. You might wonder why the Java language provides s without implementations. It is to provide conventions for everyone to use for consistency across Java APIs. If everyone wrote their own to start an applet, for example, but gave it a different name such as begin or go, the applet code would not be interoperable with other programs and browsers, or portable across multiple plats. For example, Netscape and Internet Explorer know how to look for the init and start s. 3.2 Behavior An applet is controlled by the software that runs it. Usually, the underlying software is a browser, but it can also be appletviewer as you saw in the example. The underlying software controls the applet by calling the s the applet inherits from the Applet class. The init : The init is called when the applet is first created and loaded by the underlying software. This pers one-time operations the applet needs for its operation such as creating the user interface or setting the font. In the example, the init initializes the text string and sets the background color. The start : The start is called when the applet is visited such as when the end user goes to a web page with an applet on it. The example prints a string to the console to tell you the applet is starting. In a more complex applet, the start would do things required at the start of the applet such as begin animation or play sounds. After the start cutes, the event thread calls the paint to draw to the applet s Panel. A thread is a single sequential flow of control within the applet, and every applet can run in multiple threads. Applet drawing s are always called from a dedicated drawing and event-handling thread. The stop and destroy s: The stop stops the applet when the applet is no longer on the screen such as when the end user goes to another web page. The example prints a string to the console to tell you the applet is stopping. In a more complex applet, this should do things like stop animation or sounds. The destroy is called when the browser exits. Your applet should implement this to do final cleanup such as stop live threads. 3.3 Appearance The Panel provided in the Applet class inherits a paint from its parent Container class. To draw something onto the Applet s Panel, you implement the paint to do the drawing. The Graphics object passed to the paint defines a graphics context for drawing on the Panel. The Graphics object has s for graphical operations such as setting drawing colors, and drawing graphics, images, and text. The paint for the SimpleApplet draws the I m a simple applet string in red inside a blue rectangle. public void paint Graphics g Sytln “Paint“ ; //Set drawing color to blue g.setColor Color.blue ; //Specify the x, y, width and height for a rectangle g.drawRect 0, 0, getSize .width -1, getSize .height -1 ; //Set drawing color to red g.setColor Color.red ; //Draw the text string at the 15, 25 x-y location g.drawString text, 15, 25 ; 4、Packages The applet code also has three import statements at the top. Applications of any size and all applets use import statements to access ready-made Java API classes in packages. This is true whether the Java API classes come in the Java plat download, from a third-party, or are classes you write yourself and store in a directory separate from the program. At compile time, a program uses import statements to locate and reference compiled Java API classes stored in packages elsewhere on the local or networked system. A compiled class in one package can have the same name as a compiled class in another package. The package name differentiates the two classes. The examples in Lessons 1 and 2 did not need a package declaration to call the Sytln Java API class because the System class is in the java.lang package that is included by default. You never need an import java.lang.* statement to use the compiled classes in that package. 5、More Ination You can find more ination on applets in the Writing Applets trail in The Java Tutorial. 文獻譯文 Java 編程語言基礎 像Java Application小應用程序一樣,Java Applet小程序也是從類中創建來的。然而,Java小程序沒有作為程序執行入口點的main方法,但是,作為代替,它有一些自身的方法來控制它的執行。 在這一課中我們把第二課中的一個小應用程序轉變成一個小程序,并藉此描述小程序的基本結構和原理。 1、從Java小應用程序到Java小程序 2、運行Java小程序 3、Java小程序的結構和原理 4、包 5、更多信息 1、從Java小應用程序到Java小程序 下面列出的代碼是和第二課中的名為LessonTwoB 的小應用程序等同的Applet小程序。下面的圖象顯示了Java小程序運行時的樣子。下面我們先講解如何運行Java小程序,然后再來討論這段Java小程序代碼的結構和原理。 import et; import hics; import r; public class SimpleApplet extends Applet String text “I m a simple applet“; public void init text “I m a simple applet“; setBackground Color.cyan ; public void start Sytln “starting.“ ; public void stop Sytln “stopping.“ ; public void destroy Sytln “preparing to unload.“ ; public void paint Graphics g Sytln “Paint“ ; g.setColor Color.blue ; g.drawRect 0, 0, getSize .width -1, getSize .height -1 ; g.setColor Color.red ; g.drawString text, 15, 25 ; 這個SimpleApplet類被聲明為公有的,于是運行這個applet的程序 瀏覽器或是appletviewer 不必是本地的。 2、運行Applet小程序 要能看到Applet小程序的運行結果,你需要一個如下所示的含有Applet標簽的HTML文件: APPLET CODE SimpleApplet.class WIDTH 200 HEIGHT 100 其實運行Java小程序的最簡單的方法是用JDK提供的appletviewer Java小程序閱覽器 執行一個包含上面列出的HTML代碼的simpleApplet.html文件。如下所示: appletviewer simpleApplet.html 注意:為了能在一個瀏覽器中運行用JavaTM 2 APIs編寫的Applet小程序,你的瀏覽器必須支持Java 2平臺。如果你的瀏覽器不支持Java 2平臺,那么你不得不利用appletviewer來運行你的Applet小程序或是安裝Java Plug-in。Java Plug-in允許你在Java VM 1.2版的環境下以web網頁的形式運行Applet小程序,而不是以web瀏覽器默認的Java VM形式運行。 3、Applet小程序的結構和原理 Java API Applet 類提供給你設計Applet小程序的外觀和控制Applet小程序行為的一切。這個類提供了一個稱為面板的圖形用戶接口(GUI) 3.1 擴充一個類 無論一個類多么復雜,它都是從其他的類擴展而來的。擴展其他的類意味著編寫一個新的類,這個新類能使用在被擴展類中定義的屬性和方法。被擴展的類我們稱之為父類。相應的,做擴展的類我們稱之為子類。換句話說,也就是子類繼承了他的父類或是祖先類的所有屬性和方法。子類可通過覆蓋或是重載來繼承父類的方法。這稱為單重繼承。 我們自己定義的SimpleApplet類繼承自Applet類,Applet類繼承自Panel類,Panel類繼承自容器類,容器類繼承自Object類,Object類是所有Java API類的父類。(如左圖) Applet類有init ,start ,stop ,destroy 和paint 等方法,正如你在上面的Applet小例程中看到的。SimpleApplet類覆蓋了Applet類中的這些方法,使這些方法能按照SimpleApplet類的自身的具體要求來完成功能。Applet類提供的這些方法都是無功能性的。 然而,Applet類卻提供了一個功能性的方法setBackground,這個方法在init 方法中聲明。setBackground方法的聲明是一個引用父類的方法的典型例子,這個例子和覆蓋父類方法形成對照。 你也許會感到奇怪:為什么Java語言提供沒有方法體的方法呢?Java規定了每一個人使用Java APIs一致性的協定。如果每個人都編寫自己的開始執行Applet小程序的方法,但卻給了它一個不同的名字,就好象begin或是go,那么結果將是Applet小程序代碼不能在其他各種程序及瀏覽器中,或是在跨平臺間通用。舉個例子:Netscape和Internet Explorer知道如何查找init 和start 方法。 3.2 行為 一個Applet小程序由運行它軟件控制。通常,底層的軟件是一個瀏覽器,但這個底層軟件也可以是appletviewer,正如你在上面的例子中看到的。底層軟件通過和Applet小程序中繼承自Applet類的方法發生聯系來控制applet。 init 方法:當Applet小程序最初被創建和被底層軟件裝載時,init 方法被引用。這個方法執行一次,主要是用來初始化Applet小程序,例如創建用戶界面或是設置字體。在上面的這個例子中,init 方法初始化了一個文本字符串并設置了字符串的前景顏色。 start 方法:start 方法在Applet小程序被訪問時才引用。在本課的例子中,程序向控制臺打印了一個字符串來通知你Applet小程序已經啟動。在一個更為復雜的Applet小程序中,start 方法將執行Applet小程序啟動所需的各種行為,如播放聲音等。start 方法執行后,事件線程引用paint 方法來畫Applet小程序的面板。一個線程是在Applet小程序中的單個的連續控制流,并且每一個Applet小程序能以多線程運行。Applet類的drawing方法經常被專注畫圖和事件處理線程引用。 stop 方法和destroy 方法:stop 方法用來停止Applet小程序。當Applet小程序不在電腦屏幕上時,如最后的用戶轉到其他網頁上時,stop 方法就被調用停止Applet小程序。再上面的例子中,程序打印出一個字符串到控制臺通知你Applet小程序停止了。在一個更為復雜的Applet小程序中,stop 方法將執行Applet小程序各種行為,如停止播放聲音等。destroy 方法在瀏覽器退出時被引用。你的Applet小程序應該執行這個方法來做最后的清理工作,例如停止活性線程和清理內存。 3.3 Applet小程序外觀 面板從他的父類即容器類中繼承了一個paint方法。要在面板上畫些東西,你只需執行這個paint方法來做畫圖工作。 Graphics object傳遞了一個在面板上作圖的圖形聯系到paint方法。Graphics object有各種圖形操作的方法,如設置圖形顏色、畫圖形、圖象、文字等。 下面演示了一個paint方法,它畫了一個藍色的矩形,并這個矩形中寫紅色的字符串“I m a simpleapplet“。 public void paint Graphics g Sytln “Paint“ ; //Set drawing color to blue g.setColor Color.blue ; //Specify the x, y, width and height for a rectangle g.drawRect 0, 0, getSize .width -1, getSize .height -1 ; //Set drawing color to red g.setColor Color.red ; //Draw the text string at the 15, 25 x-y location g.drawString text, 15, 25 ; 4、包 在這個Applet小程序的頂部還有三個import聲明。所有Application小應用程序和Applet小程序使用import聲明來訪問Java包中已經存在的Java API。不管這個Java API類來自你下載的Java平臺,或是來自第三方軟件,還是你自己編寫的。當編譯程序時,程序利用import聲明來查找和引用存放在包中的Java API類,無論這些包在本地或是別的網絡系統中。不同包中的編譯類可以有相同的名字。包名能區分這兩個類。 第一課和第二課中的例子不需要用包的聲明來引用Sytln 這個Java API 類,因為java.lang 包中的System類是被默認加載的。你不需要使用import java.lang.*來加載java.lang包中的類。 5、更多信息 要想得到更多的關于Applet小程序的信息,請到 The Java Tutorial的Writing Applet版塊。 Applet Panel Container Object SimpleApplet Applet SimpleApplet Object Applet Panel Container SimpleApplet
總結
以上是生活随笔為你收集整理的java编程语言基础外文,Java编程语言基础(外文文献翻译)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用HTML制作个人简历
- 下一篇: CNN卷积神经网络十二生肖识别项目(一)