Memorandum of Rookie Java Java (2)

zhaozj2021-02-16  47

Thursday, January 16, 2003, Sunny Java's entry is more simple than I think. So far I have already understood the basic grammar structure of Java. But I know, in-depth research, any language, you need Time and practice accumulation.

Applet is a code written in Java, which is running in the browser side. It is a clear difference in the application that its execution method is different, and the application is running from the main () main program, and Applet is more complicated. I don't know how to complicate, but I will slowly clear. About an important nature of Applet is that I can pass the value in the HTML to the applet (by getParameter () to get the parameter value). This way, in order to produce different effects, we do not need to recompile the Java program, but only need Modify the parameter value of HTML. Because the code of HTML can be generated, I can control the dynamic effect of the web page.

There are 3 comparison methods in the life cycle of the applet: init, start, stop init (): responsible for the initialization of Applet, only this method is performed in the life cycle of the entire applet. And on the oncreate () event in Delphi. Yes, the same truth (): After calling init (), start () will automatically call, and each current window is reactivated, this method is called, and the onshow () event in Delphi is almost the same. . Stop (): The user is called after the user leaves the applet. It stops you when the user doesn't pay attention to Applet, stopping some resources to avoid affecting system running efficiency. And we don't need to remove this method. This method is similar to the onClose () event in Delphi.

Below is an applet version of the HelloWorld file name: HelloWorld.java

Import java.applet.applet; import java.awt.graphics;

Public class helloworld extends applet {string title; public void init () {title = "hello world";} public void Paint (graphics g) {g.drawstring (title, 50, 20);

}

We can see that there is no main function in the program. How do it run? Since Applet is running in a browser environment, then we need to call it in an HTML file. The relevant tags need to be used are Tags, we first build HTML file Test.htm, source code as follows. Here Comes my first applet:
< / Applet>

Put the file and helloworld.java in the same directory, then compile helloWorld.java, click Test.htm to open, you can see the Applet program is started. Or use the appletViewer command AppletViewer test.htm can also The browser is running directly of the applet.

One of the following programs can better help us understand how Java Applet is how to call the entire life cycle to call several methods we introduced above.

File name: StartStop.javaimport java.awt. *; Import java.applet. *;

Public Class StartStop Extends Applet {StringBuffer Message; Public Void Init () {Message = New StringBuffer ("init done ...");} public void start () {message.append ("start ...");} public Void Stop () {message.append ("stopped ...");}

Public void Paint (Graphics G) {g.drawstring (Message.toString (), 150, 50);

}

The operation method is the same. (This program can refer to the )

Different C language, use Java to implement GUI to easily. Since it is a pure face-to-object language, Java's AWT provides various interface elements for us to call, just like the components in Delphi. Below is a comparison table of the GUI objects in Java and corresponding components in Delphi

Java Delphi Button Tbuttoncanvas Tcanvascheckbox TcheckboxcheckboxGroup Tradiogroupchoice Tcomboboxlabel TLabelTextField TedittextArea Tmemo

However, JDK is not a visualized RAD (Rapid Application Development) development tool. For the use of objects, you can only need to drag, you can shift as Delphi, and we need to write call code. This has a trouble, how do I? Can you let the elements are placed in the interface according to my requirements? When the elements are not allowed, the Element is available, or when the elements are required, they need to use panels when they are placed according to the requirements of the application. (PANEL). The panel also has the corresponding components in Delphi, but mainly for interface segmentation, rough layout, precise positioning also requires developers to manually regulate. And Java can only use panels. Positioning, can not be said to be a defect. Maybe I haven't learned it yet.

After getting started, it will be in depth to the concept of object. Assuming a custom data type called Date is created in Java as follows PUBLIC CLASS DATE {Int Day; int Month; int year;} So what is the difference between Java? ? (1) INT I; (2) Date mybirth; (3) Date mybirth = new date ();

Obviously, the allocation is as follows: (1) Java automatically assigns the memory of the shaped variable for integer i, is generally two bytes (2) Java declares a Date class instance variable mybirth, and allocated storage Space, but this storage is only a reference, or an address, there is no in the current address, then we can't use this instance variable and cannot reference its members. (3) Java created a Date class instance variable mybirth, assigned enough storage space for its member variable, and finally returned a reference to this storage space, which is the first address of this storage space, and later by Mybirth That is, this first address to access the individual members in this instance variable, such as Mybirth.day, Mybpter.month, Mybpter.year. When we declare a variable of a basic data type (such as Boolean, Byte, Short, CHAR, INT, LONG, FLAT, DOUBLE), the system will automatically allocate memory for variables. However, if you declare string or user-defined variables, the system will not be assigned a memory immediately. Why is this? This is because String and user-defined variables belong to the category of classes. One is declared as class Variables are no longer a data, and become a reference to the data, that is, Mybirth can be seen as a pointer to an instance of the class, which is stored in the address. This is a good understanding. Deeply, since the class's instance variable value is a pointer, this pointer points to an instance of a class, then we can clearly define instance variables of multiple names different classes, and point them all to an instance. For example, UNIVERSITY U = New University ); // define a class university's instance variable U, and assign the storage space of the object university u2 = u; // also defines an instance variable U2, assign U2 to U2, then apparent U2 and U The name is different, actually a matter, because they point to the same address. I think this is still very important. The things of these data structures are what a programmer needs to know.

转载请注明原文地址:https://www.9cbs.com/read-27544.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.045, SQL: 9