Java application skills

xiaoxiao2021-03-05  35

---- Java is a technique for building a score application program. It is the best show for the network programming. Enable the Internet to extend from the communication tools to enable operations to become a network.

---- Java has two types: Applet and Application. Simply speaking, Applet is a program embedded in a web document, and Application is the program of all its type.

---- For Java, the size and complexity of Applets and Application are not limited, but because Applet is mainly used for network communication, due to limited communication speed, the download time is longer, and Applet is generally small. There is no such concerns for Application.

---- The technical difference between applet and Application is coming from the difference in its operational environment. Applet needs a lot of information from the browser: the location and size of the browser client, the parameters of the main HTML document, the initialization process (init), startup process (START), stop process (STOP), Destory, Drawing Process (PAINT), etc., and Application is relatively much simpler, and the only input from the outside world is the command line parameters.

---- A subclass for the applet must be a Java.Applet.applet, and Application must have a public square main (). Second, the main thread of the two is different. Applet is initialized by the method init), and Application is initialized by method main (). Generally, Java's Applet and Application are the original compliance with the original, but, we can write the applet and the program of Applet is the program. In this way, we can make it more step more about the internal structure of Java and can make the same order to operate in different environments, and can improve the interest of Java.

---- The routine is as follows:

Import java.applet. *;

Import java.awt. *;

Import java.awt.event. *;

Public Class Example Extends Applet IMPLEMENTS

ActionListener {// public static void main (string [] args) {

Frame Win = New Frame ("EXAMPLE");

EXAMPLE EXAMPLE = New EXAMPLE ();

Win.Add ("Enter", Example);

Example.init (); // win.setsize (600, 360);

Win.setVisible (TRUE);

}

Public void init () {// button button;

Label Label; Button = New Button ("OK"):

Button.SetBounds (280, 200, 100, 20);

Button.addActionListener (this);

Label = new label ();

Label.setBounds (260, 100, 200, 20);

Add (Button);

Add (label);

}

Public Void ActionPerformed (ActionEvent E) {

Object Source = E.GETSource ();

IF (Source == Button) {

Label.Settext ("IT IS A Example!");

}

}

}

---- In the # 1 in the program, you must let the program relay java.applet.applet class This is the necessary condition for applet, at # 2, you need to have an instance of this class Example,

To call the init () method, # 3 is the init () method.

---- If you look at the applet, it just covers the init () method and the actionperformed () method to respond to the Button event. If you make Application, you will start by the main () method.

The instance of the primary ensembled program is added to the window, then call the init () method. To translate this approach, you can use it as an Application direct operation, and you can send it as an applet to any browser.

---- 2. Sound report in Java

---- Java as a network programming language, you can act in the browser, you can also play the sound. In the interaction of the human machine, it is necessary to add a strong effect or to the action to be added.

---- Java's package java.applet has an AudioClip interface, which seals some common methods for shutdown. Usage is as follows:

Audioclip Audio;

Audio = getaudioclip (getcodebase (), "hi.au");

Audio.Play ();

---- First line generates a variable of the interface AudioClip, the second line uses this variable to obtain the sound file Hi.au, this file and the program itself under the same directory, the getCodeBase () method is used to get the applet's Class file URL address. The third line is a broadcast sound file. This usage can be sent in the browser in the applet. So, can I use this method to send sound in Application? Not available. Because the interface AUDIOCLIP is in the package java.applet, this package is only available for Applet. Yes Yes I can use it to realize it? We can use the techniques of 1 to write one of the programs for Applets and Application. Fruit, or not. This is because the Play () method can only be implemented in the applet, and it is not possible to be called for the ApplicationPlay () method. ---- What is the presentation of the presentation in Application?

---- Remember that there is a BEEP statement in VB to enable the system speaker, then, similar approaches should also be similar in Java.

---- In Java's java.awt.toolkit class, it is possible to realize this feature. Class Toolkit is an abstract class that is the parent class of all tools that implement AWT. The abstract classes in Java are not unmembected, but it is generally, the abstract class can be generated to be variable, then taken the substitute for such replacement using some of the abstract classes. In Toolkit, it is real in the GetDefaultToolkit () method. Now give an example: this is an Application of a guest / server. When the servers are running, if the customer is connected to the server, the server will automatically vocal alert to the megadownload megadownload megadownload megadownload megadownload megadownload.

---- The program is as follows:

Server:

Import java.util. *;

Import java.io. *;

Import java.net. *;

Import java.awt. *;

Public Class Servert {

Public static void main (String [] args) {

Server Server;

String ClientRequest;

Boolean quit = false;

Server = New Server (8001);

While (! quit) {

DataInputStream Keyboard = New DataInputStream (System.in);

Try {

ClientRequest = Server.in.Readline ();

if (ClientRequest.trim (). Equals ("Close")) {

System.out.println ("Client Says:" ClientRequest);

System.exit (1);

System.out.println ("Client Says:" ClientRequest);

Server.out.println (Keyboard.Readline ());

} catch (ioexception e) {

System.out.println ("IOException in Server.in.Readline ()" E);

System.exit (1);

}

}

}

}

Class Server {

Private Serversocket Server;

PRIVATE SOCKET Socket;

Public DataInputStream in;

Public PrintStream Out;

Public server (int port) {

Try {

Server = new serversocket (port);

System.out.println ("/ N

*********************************************** " );

System.out.println ("/ n @ (version 1.00 97/12/30");

System.out.println ("CopyRight (C) 1997

All Rights Reserved. ");

System.out.println ("/ N

*********************************************************** * ");

System.out.println ("/ N Server IS: / N" Server);

Socket = server.accept ();

For (int i = 0; i <260; i ) {//

Toolkit.getDefaulttoolkit (). Beep ();

}

System.out.println ("/ N Server Is Ready! / N);

IN = New DataInputStream (Socket.getInputStream ());

OUT = New PrintStream (socket.getoutputstream ());

Out.println ("We Connect IN" New Date ());

} catch (ioexception e) {

System.out.println ("Server IS Failied!");

}

}

}

Client computer :

Import java.util. *;

Import java.io. *;

Import java.net. *;

Public class clientt {

Public static void main (String [] args) {

String Welcome, response;

Client client;

Client = New Client ("202.120.80.20", 8001); // DataInputStream

Keyboard = New DataInputStream (System.in);

Boolean Bye = False;

While (! BYE) {

Try {

Welcome = client.in.readline ();

System.out.println ("Server Says: Welcome);

Client.out.println (Keyboard.Readline ());

} catch (ioException e) {system.out.println ("/ n the talk is close!);

System.exit (1);

}

}

Try {

Thread.sleep (200);

} catch (exception e) {

System.out.println ("IT IS A Bug!");

}

}

}

Class client {

Public DataInputStream in;

Public PrintStream Out;

PRIVATE SOCKET Client;

Public Client (String Host, Int port) {

Try {

Client = New socket (Host, Port);

System.out.println ("/ N

*********************************************************** * ");

System.out.println ("/ n @ (version 1.00 97/12/30");

System.out.println ("CopyRight (C) 1997

All Rights Reserved. ");

System.out.println ("/ N

*********************************************************** ");

System.out.println ("/ N Client Socket: Client);

System.out.println ("/ N Client Is Ready! / N);

OUT = New PrintStream (Client.getOutputStream ());

IN = New DataInputStream (Client.getInputStream ());

} catch (ioexception e) {

System.out.println ("/ n ioExce! / N" e);

System.exit (1);

}

}

}

Welcome to reprint in the case of retaining http://www.javajia.com!

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

New Post(0)