Memorandum of Rookie Java Java (10)

zhaozj2021-02-16  41

Sunday, January 26, 2003

Review the Socket based on the connection pool that is learned yesterday, the key points in the principles are as follows: The server open limited PooledConnectionHandler thread is used to process the connection; the client's connection request is added to the POOL connection pool (actually a linklist () object); Once the PooledConnectionHandler is discovered that there is a connection request in the connection pool, it will be handled immediately; everyone uses the same pool (so it is static variable)

So far, some basic contents about the Java program have been almost learning. It is time to use it. Learn any language, to truly talk about getting started, you should be marked with an actual programming experience. I think.

Previously, a Russian square was previously used. I think this game's programming can reflect the object-oriented thought. Java and C have a lot of common, and I will reach it once, then I will re-realize it. Almost, the right is new. In addition, since it takes so much time to study Socket, then the sorry is compiled.

The Russian block program requires a graphical user interface (GUI), and in the previous Java program, only the applet program implements the graphical interface, but is done with the help of AppletViewer or browser. With Java to implement graphical user interface, We need to use the Frame class. Frame class is a subclass of the Windows class. It is the basic window of the graphical user interface. Read the following applet and implement it, you can basically have a graphical user interface under Java. understood.

File Name: frameMemo.java function: A framework with menu is popped and displayed in the frame. Click on the window in the window, and the program exits.

Import java.awt. *;

Public class framememo extends frame {public string motor;

Public framememo (String S) {Super (s); // Super is represented here that the parent class is the corresponding class, and there is corresponding to this class represented by this. Therefore, it is called the constructor of the parent class MOTD = S; Resize (300, 300) Setmenu ();

Public framememo () // Reserved Construction Function {THIS ("This Is A Memo of Frame"); // Calling is a FrameMemo (S) constructor setmenu ();

Public void setmenu () {MENU M, N; M = New Menu ("Examples"); M.Add ("Basic")); M.Add (New Menuitem ("Simple");

n = new menu ("more example"); N.Add (New Menuitem ("Sub Basic"); M.Add (n);

MenuBar MB = new menubar (); Mb.Add (m); setmenubar (MB);

Public void Paint (Graphics G) {g.drawstring (Motd, 15, 15); g.drawstring ("Click Anywhere To EXIT", 45, 45);}

Public void start () {show ();

Public Boolean MouseDown (Event E, INT X, INT Y) {hide (); // Hide Window Dispose (); // Release Resource System.exit (0); Return False;

Public static void main (string args []) {framememo m; m = new framememo (); m.Start ();}}

After establishing a window, we also need to place a variety of graphical interface objects in the window. That is to say, the principle of layout is introduced earlier, and you need to use the container, and then fill in the object according to certain rules. In an example, most of us we need to use the container. I gave a very detailed note, helping me to find a lost memory after I forgot the syntax of Java, and can quickly find the lost memory. File: // layout example file: //j2sdk1.4.1 compiled

Import java.awt. *; import java.applet. *;

Public Class Layout Extends Frame {Panel Fnames, Styles; // Two Font Buttons Put in this Panel, three style buttons are placed in the styles in the panel Button Sans, Serif; Button Bold, Bigger, Smaller; // Define two Font button, three style buttons Samplecanvas sample; // Define the built-in canvas object String fname = new string ("sansserif"); int FSIZE = 12;

Public Layout () {Super ("a memo of layout"); setFont (New Font ("Sans", Font.Bold, 12); setLayout (new borderLayout ()); // Set the BorderLayout layout, this method can be arranged Five elements File: // The orientation is in the middle of the north and south

File: // The following program is setting the two buttons of the bottom of the South FNAMES = New Panel (); FNAMES.SetLayout (New GridLayout (1, 2, 5, 10)); / / Set GridLayout Layout for Panel: / / This layout is the grid layout File: // four parameters are file: // row number, number, horizontal interval (pixel), vertical interval (pixel) SANS = New Button ("Sans Serif"); FNAMES. Add (SANS); Serif = New Button ("Sans Serif"); FNames.Add (Serif); file: // Add two buttons

Add (FNames, "South"); file: // lists Panel in the BorderLayout layout

File: // The following program is set to the three buttons of the west side styles = new panel (); styles.setLayout (New GridLayout (3, 1, 10, 5)); bold = new button ("bold") Styles.add (bold); Bigger = New Button ("BIGGER"); STYLES.ADD (BIGGER); Smaller = New Button ("smaller"); styles.add (smaller); add (styles, "west") Sample = new samplecanvas (); add (sample, "center"); setsize (200, 200); // This sentence is not falling Luo // setvisible (TRUE);} public void start () {show ();} public static Void main (String [] args) // initialization {layout mainframe = new layout (); mainframe.start ();} class samplecanvas extends canvas {string message;

Public samplecanvas () File: // Class constructor {REWRITE ("sanserif"); file: // only executed when you create an object, Java does not support destructive functions}

Public void Rewrite (String S) {setFont (New Font (Font (FNAME, FBOLD, FSIZE)); Message = "this font is" fname; repaint ();

Public Void Paint (Graphics G) {g.drawstring (Message, 10, 50);}}}

(January 28) In the online version of the Russian square program, there should be the following classes:

1.MAINFRAME: Main interface class, from frame inheritance, basic definition and class China method functions

Class Mainframe Extends frame {public mainframe () {// Initialization work, mainly to object (button, canvas, etc.) layout} public boolean keydown (Event E, int key) {// handle keyboard operation} public boilean action (Event E , Object arg) {// Handling mouse operation} public void start () {// Looks at the previous day notes show ();} public static void main (string args []) THROWS IOEXCEPTION {mainframe app; app = new mainframe App.Start ();}}

2. MAINCANVAS class. This canvas object will complete all logical operations related to the block, inheritance from Canvas class

3. Block class, ie, block class. This class has information (a multi-dimensional array), which will compete with the maincanvas class. In fact, you can make Block and Maincanvas into a class, in me This is this in the original C version. However, from the object-oriented point of view, it is appropriate to divide two classes.

4.ClientThread class, ie client thread class

5. ServerthRead class, the server-side thread class

ServerThread and ClientThread are responsible for network synchronization operations

It is basically completed to analyze this work. I decided to give up the plan to implement the web version of the Russian square, because I have repeated my very familiar idea is a very boring thing. I am going to lock the target in Java Applet The implementation of the electronic whiteboard. Related research I will put it in another series after the year.

The rookie note for half a month is finally ended. 15 days is enough for any language in any language, after all, the language is only a tool for design ideas, and it should not spend too much time for any tools. How to create a fine artwork should be thinking in your mind.

In the process of getting started with Java, three books have given me a lot of help. The first is the

Ok, I wish you a happy new year! New Year!

Attachment: Download Address 1. Title: Java Socket (Socket) 101 Note: This tutorial will tell you what is socket and teach you how to use it in a Java program. By actually analyzing a few examples, communication from a single client / server to a set of client access servers, you will learn how to use a set of typical situations in the real world. URL: http://www-900.ibm.com/developerWorks/cn/edsucation/java/j-sockets/tutorial/j-sockets.zip

2. Title: Think in java (Chinese version) --- CHM format URL: http://www.code-labs.com/manual/think IN java.chm

Currently valid

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

New Post(0)