Chapter 4 Basic Structure of Java Application
Learn to learn a new language is the best way to see a few simple program cases. Here we will see several non-constrained program cases.
4.1 Java should be used in operation
The Java application refers to a program that can run on a Java virtual machine independently. It is an intermediate code (byte-code? Copy; for example, your application is MY.JAVA, there is a class named app1 in the program, With Javac or other compiler, App1.class will generate app1.class, enter: Java app1 in the command line state, you can run this program. Note that the class running with the java command must have a main function, otherwise it cannot be performed. Another another Java program called Java Applet. We translated it into a Java applet, this program suffix is also .CLASS, but it can't run directly on the Java virtual machine, that is, enter Java * .class can't run, there is no main function in this program, which must be run by a browser, such as AppletViewer or Netscape 2.02 or higher. This program we will introduce it in later chapters.
4.2 September Java Procedure Explanation
Let me take a look at the simplest Java application program case, let's explain its structure: FILENAME: 1.Java
Class myfirst {public static void main (string args []) {system.out, println ("this is my first java application");}}
This is a complete Java application program, compiles it: Javac 1.java In the previous directory, it will be generated into myfirst.class file, and the Java Myfirst screen will output: this is my first java application
Let's take a step by step to analyze each sentence (1? Copy; class myfirst this line with keyword class "to define new classes named myfirst, MyFirst is the name of the new class, must be a valid identifier, valid identifier See the program design foundation section. Class instructions include data instructions and member function descriptions, are placed inside the braces behind the class. The general class defines the following: Class class name {data definition; function definition;
(2) Public static void main (string args []) PUBLIC is a keyword for a table showing the right limit, which shows that the function function is public, and it can be directly adjusted by other classes, including Java interpretation. The corresponding keyword has private and protected, friend. Private representation can only be accessed by this class, the protected represents can only be subject to subclass, and Friend is the default access authority. The table can be accessed by this package (Package), and the class in the package is not accessible. The Static representation is only one of the MAIN member functions in the MyFirst class, so there is such a way to generate another MyFirst class object, and the MAIN function called the same function will be the same function. Void indicates that the main function does not have a return value. If there is a return type value, you can add Interger or Boolean such as such a function of the returned value, and its functional body should be added to the RETURN statement. This function is the entry point of this operation application program, and the programming program is required to have a main () function, and is only one. (3? Copy; system.out.println This sentence is the functional statement in the main function, is the PrintLn member function of the OUT class in the system package in Java, which is the standard input.
4.3 Java application program parameters pass
Java language is very similar to C and C words. In the C language, pass through the command line input parameter, the C program can be read from the main function? Copy; parameter, Java program is also the same, please see the next step: FileName: 2.java
Class my2 {public static void main (string args []) {int ARC = args.length; if (arc> 0) {for (int i = 0; i Compilation: Javac 2.java will be born into my2.class files under this directory. Run: Java my2 output: Application Have No Args! Operation: Java my2 arg1 arg2 arg3 output: arg1 arg2 arg3 This description 1] In this class. 4.4 Construction window Java program basic framework Frame introduction In the Java language, the Frame class's function is a basic window of the Creating a Type User Interface (GUI). In a typographic browser, the Frame class is the parent class that shows the GUI. The level of the class is as follows: java.lang.object java.awt.component java.awt.container java.awt.window java.awt.frame Frame is a blank window that has no border and menu strip? Copy; charge. This window is to use under the population of the population. Frame gives the window a border, a layout and a window bar. You can create the application program of the map user interface with multiple frames that create self-cultivation. How do we know how to build an independent Java application program, how do you make a variety of fascia in a piece? We have entered a Frame to realize this function. Frame allows our visit to the window, and there is a lot of places in the Applet work area. The lower surface is a small application program, which pops up a frame and expresses information in the frame. (Information can be a command line argument, or it is the default information? Copy; If you are in the window, the program is retired. Basic frame frame "Import java.awt. *; / * * MEMO.JAVA * A Simple Stand-Alone Graphical Application * / Public Class Memo Extends Frame {Publc String Mot; Public Memo (String S) {// setur title super ("Memo frame"); MOTD = S; Resize (300, 300); Public memo () {this ("this is a memo");} Public void Paint (Graphics G) {g.drawstring (MOTD, 15, 15); g.drawstring ("Click Anywhere To EXIT", 15, 25); Public void start () {show (); Public Boolean MouseDown (Event E, INT X, INT Y) {// Hide the window hide (); // free up system resources dispose (// quit the app. system.exit (0); Return False;} Public static void main (string args []) {memo m; if (args.length>) {m = new memo (args [0]);} else {m = new memo ();} m.start (); } Put the Frame to lie-familiar-familiarity Paint () and mousedown (), look at it. This is the function with the application of the applet. In practice, an Frame package contains various GUI components in the form of applet. Another familiar function is start (). This function doesn't have to be, because I have no overridden functions. But you want to keep the applet programming style, you are using start (), stop (), init () and destroy (). l The new function. We are familiar with START () called show (). The show () function is the relay of the Window class, which shows the FAME and its components. In the mousedown () function, we saw two functions: hide () and dispose (). Hide () is only simply designed to make Frame. You can call it to hide the window at any time. Dispose () Function Releases the system resources that the FRAME is occupied. Only call it when you don't have to find Frame. l Configuration Function MEMO Example also includes a new function MEMO (). It has two MEMO () functions! The functions of any of the class names are all constructed functions. It is called when creating a new object. You can think it is an initial function of an object. Why do you need two configuration functions? With two configuration functions, we can use multi-state advantages, there are two ways to create a new MEMO object. We can use the Memo pairs of the default information in a simple creation: m = new memo (); or, we can use itself? Copy; Information: M = New Memo ("ur message"); The final function of the frame control this program is to build and display the Frame in main (). It is from the following two steps: // Step 1 m = new memo (); // Step 2 m.Start (); the first step in initialization MEMO object. We like to operate its object to M. To display Frame, we need to call Show (). This is now in the second step. Another way to note is the tacitivity of the program: there is no foundation of the Based on the Frame. In this way, you will not be able to select "Quit" or "exit" in the control menu of the window. In order to add this function, you need to add the following proceedings: Public Boolean Handleevent (Event E) {if (E.ID == Event.Window_Destroy) {Dispose (); System.exit (1); Return True;} else {// Go Ahead and do What We Normal Would Have Done Return Super.handleevent (e);}} The menu graphic interface relies on the menu to refer to the guide. When designing an independent Java application, Java mentioned; Creating and using the direct method of using the menu. I am like other components, New will create a menu: Menu OptionMenu; OptionsMenu = New Menu ("options"); Menu item one? Copy; You created a menu, you can use Add () to build a menu item: Optionsmenu.add (New Menuitem ("Option1"); optionsmenu.add (New MenuItem ("Option2"); Menu event When you choose a menu item, you create the character of this menu item? REG; will return it in the event. You can select a test menu for the test button to select: Public Boolean Action (EVENT E, Object Arg) {... if (e.target instanceof meneuItem) {system.out.println (String) arg);} ...} Its menu item except the simple menu item described above, you can also increase the checkbox menu item, divide the line, and submenu. The next is a Copy; example: Menu M, N; M = New Menu ("example"); M.Add ("Basic")); M.Add (New Menuitem ("Simple"); // Add A Separetor M.Add (New MenuItem ("-")); // Add a checkbox item m.add (New CheckBoxMenuItem ("Check"); // Add a submenu n = new menu ("more example"); N.Add ("Sub Basic"); N.ADD (New Menuitem ("Sub Simple"); M.Add (N) ; After you create a good menu, you should be? REG; placed on the menu of the application program: MB = new menubar (); Mb.Add (m); Mb.Add (optionsmenu); You can set the menu section for the applet: setmenubar (MB); 4.5 Coordinate application case example In order to watch a more complex independent map interface application program, there is a digital conversion example: Import java.awt. *; Public class d2x extends frame {int decimalvalue = 0; string basexvalue = new string ("0"); TextField Ddisplay, xdisplay DDISPLAY // D2X Constructor Public D2X () {Super ("Decimal Converter"); // set the title of the frame menub = new menubar (); button d2binary = new button ("binary"); Button D2OCTAL = New Button "Octal"); Button D2HEX = New Button ("HEX"); Button D2Base36 = New Button ("Base36"); Panel P1 = New Panel (); panel p2 = new panel (); panel p3 = new panel () // Add a Simple Menu Menu M = New Menu ("Application"); M.Add (New CheckBoxMenuItem ("Base 36 Active"); M.ADD (New Menuitem ("exit")); // add menu to menubar mb.add (m); setmenubar (MB); // install this menu bar in the frame // add buttons to their owl p3.setLayout (new flowLayout ()); p3.add (d2binary); p3.add (d2toctal); p3.add (d2hex); p3.add (d2base36); // Add text fields Label dLabel = new Label ( "Enter Deecimal:"); Label xLabel = new Label ( "Converted Value:"); dDisplay = new TextField (integer.toString (decimalValue), 7); xDisplay = new TextField (BasexValue, 32); xdisplay.seteditable (false); p1.setLayout (new flowLayout (FlowLayout.Left); p2.setLayout (new flowLayout (FlowLayout.left); p1.add (dlabel); p1.add ( ddisplay; p2.add (xlabel); p2.add (xdisplay); // Add the panels add ("north", p1); add ("center", p2); add ("south", p3);} // end d2x constructor Public void start () {resize (400, 150); show (); Public void updatexdisplay () {xdisplay.setText (basexvalue); Public Boolean Handleevent (EVT.TARGT INTANCEOF MENUITEM) {IF ("exit" .Equals ((MenuItem) evt.target) .getLabel ())) {hide (); dispose (); system. EXIT (0); RETURN FALSE;} else if (est.target instanceof button) {string whick = (button) evt.target) .getlabel (); if (Whick.equals ("binary"))) {decimalValue = Integer.parseInt (dDisplay.getText ()); baseXValue = Interger.toString (decimalValue, 2);} if (whick.equals ( "Octal")) {decimalValue = Integer.parseInt (dDisplay.getText ()) BasexValue = Interger.Tostring (DecimalValue, 8);} if (Whick.EQUALS ("HEX")) {decimalValue = integer.parseint (DDisPlay.getText ()); basexValue = interger.tostring (DecimalValue, 16);} if (whick.equals ( "36")) {decimalValue = Integer.parseInt (dDisplay.getText ()); baseXValue = Interger.toString (decimalValue, 36);} updateXDisplay (); return true;} return false;} public Static void main (string args []) {d2x m = new d2x (); m.Start ();}} The output is shown in the figure: chapter summary: 1. Java has two types of applications, one is to directly run on the Java virtual machine, and use the java command to execute; another run in the browser, the browser is called execution, and it is called Applet minus. This book is to discuss the first application. 2. Java Application Program is made by class, and the class must have a MAIN entry function with a Java command line. 3. Similar to the C language, the Java program can also be delivered to the main function parameter by the command line. 4. Based on the basic window Java program is Frame. It can be used to build a graphic user interface program.