java编程语言基础外文,Java编程语言基础(外文文献翻译)

41528d3028836879cd698677c3999917.gifJava编程语言基础(外文文献翻译)

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值