In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "the running environment of Java application and the transfer of parameters". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The running environment of 1Java applications
A Java application is a program that can run independently on a Java virtual machine. It is a kind of intermediate code (byte-code?copy;. For example, your application is called my.java, the program has a class named app1, using Javac or other compilers, will generate app1.class after compilation, then type: javaapp1 in the command line state to run the program. Note that classes run with the java command must have a main function, otherwise they cannot be executed. Another kind of java program that is different from ordinary Java applications is called JavaApplet. We translate it into Java Mini Program, this kind of program has the suffix .class, but it can't run directly on java virtual machine, that is, input java*.class can't run, this kind of program can not have main function, it must be run by some browser, such as Appletviewer or Netscape2.02 and so on. We will introduce this procedure in later chapters.
2 the simplest Java program interpretation
Let's look at the simplest example of a Java application to understand its structure: Filename:1.java
Classmyfirst {publicstaticvoidmain (Stringargs []) {System.out,println ("ThisismyfirstJavaApplication");}}
This is a complete Java application, compile it: Javac1.java in the current directory, it will generate the myfirst.class file, the Javamyfirst screen will output: ThisismyfirstJavaApplication
Let's analyze the meaning of each sentence step by step. The line uses the keyword class to define a new class named myfirst. Myfirst is the name of the new class and must be a valid identifier. For the definition of valid identifiers, see the basic programming section. The description of the class, including the data description and the member function description, is placed in curly braces at the end of the class. The general class definition is as follows: class class name {data definition; function definition;}
(2) publicstaticvoidmain (Stringargs []) public is a keyword that indicates access rights, indicating that this member function is public and can be called directly by other classes, including the java interpreter. The corresponding keywords are private and protected,friend. Private means it can only be accessed by this class. Protected means it can only be accessed by subclasses. Friend is the default access right, which means it can be accessed by any class in this package (package). It is not accessible to classes in other packages. Static means that main member functions are unique among all objects of the myfirst class, so if the program generates another myfirst class object, the main function called will be the same function. Void indicates that the main function does not return a value, and if there is a return type value, you can add interger or boolean and so on. For a function that has a return value, the return statement should be added at the end of its function entity. The main function is the entry point for running the application, so writing an application must have a main () function and is unique.
This sentence is a functional statement in the main function, which calls the println member function of the out class in the System package in Java, and is a standard input and output.
Transfer of 3Java application parameters
The Java language is very similar to C and C++. In C language, by entering parameters on the command line, the C program can read the copy; parameters by the main function, and so can the java program, see the following program: Filename:2.java
ClassMy2 {publicstaticvoidmain (Stringargs []) {intarc=args.length;if (arc > 0) {for (inti=0;i) {m=newMemo (args [0]);} else {m=newMemo ();} m.start ();}}
Associating Frame with the familiar functions paint () and mouseDown () looks a little familiar. The copy; function is the same as the applet function. In fact, a frame contains various GUI components in the same form as applet. Another familiar function is start (). This function is not necessary because we do not overwrite any existing functions. But if you want to inherit the style of applet programming, you can still use functions such as start (), stop (), init () and destroy ().
The new function that we are familiar with, start (), calls show (). The show () function, which inherits from the Window class, displays Fame and all its components. We see two functions in the mouseDown () function: hide () and dispose (). Hide () simply makes Frame invisible. You can call it at any time to hide the window. The dispose () function releases system resources owned by Frame. Call Frame only if you don't need it.
The constructor Memo example also includes a new function Memo (). In fact, this class has two Memo () functions! Any function with the same class name is a constructor. It is called when a new object is created. You can think of it as an object's init () initialization function. Why do you need two constructors? With two constructors, we can take advantage of polymorphism and have two ways to create a new Memo object. We can simply create a Memo object that uses the default information: m=newMemo (), or we can mention the copy; information: m=newMemo ("Ourmessage") ourselves.
Frame controls the last function of this program is to establish and display frame in main (). It is implemented by the following two steps: / / Step1m=newMemo ()
/ / Step2m.start (); the first step initializes the memo object. We manipulate m like any other object. To display frame, we need to call show (). This is achieved in the second step. Another thing to note is the robustness of the program: our basic frame does not contain functions to handle standard WINDOW_DESTROY messages. In this way, you will not be able to select "Quit" or "Exit" in the window's control menu. To add this feature, you need to add the following handlers:
PublicbooleanhandleEvent (Evente) {if (e.id==Event.WINDOW_DESTROY) {dispose (); System.exit (1); returntrue;} else {/ / Goaheadanddowhatwenormallywouldhavedonereturnsuper.handleEvent (e);}}
Menu graphical interface relies on menus to guide users to operate. Java offers a direct way to create and use menus when designing stand-alone Java applications. Like other components, new creates a menu:
MenuoptionMenu;optionsMenu=newMenu ("Options")
Menu item 1? copy; you have created a menu, and you can use add () to build menu items:
OptionsMenu.add (newMenuItem ("Option1"); optionsMenu.add (newMenuItem ("Option2"))
Menu event when you select a menu item, the character that you create this menu item? reg; will be returned in the event. You can test the menu selection just like the test button selection:
Publicbooleanaction (Evente,Objectarg) {... if (e.targetinstanceofMenuItem) {System.out.println ((String) arg);}.}
Other menu items in addition to the simple menu items described above, you can also add CheckBox menu items, split lines, and submenus. Here is an example of copy;: Menum,n;m=newMenu ("Examples"); m.add (newMenuItem ("Basic")); m.add (newMenuItem ("Simple"))
/ / addaseparatorm.add (newMenuItem ("-")
/ / addaCheckboxitemm.add (newCheckboxMenuItem ("Check"))
/ / addasubmenun=newMenu ("MoreExamples"); n.add (newMenuItem ("SubBasic")); n.add (newMenuItem ("SubSimple")); m.add (n)
Menu bar after you have created the menu, you should put reg; on the menu bar of the application: mb=newMenubar ()
Mb.add (m); mb.add (optionsMenu)
Then you can set up a menu for applet: setMenuBar (mb)
5 examples of stand-alone applications
To see a more complex stand-alone graphical interface application, here is an example of digital conversion:
Importjava.awt.*
Publicclassd2xextendsFrame {intdecimalValue=0;StringbaseXValue=newString ("0"); TextFielddDisplay,xDisplay
/ / d2xconstructorpublicd2x () {super ("DecimalConverter"); / / setthetitleoftheframeMenuBarmb=newMenuBar (); Buttond2Binary=newButton ("Binary"); Buttond2Octal=newButton ("Octal"); Buttond2Hex=newButton ("Hex"); Buttond2Base36=newButton ("Base36"); Panelp1=newPanel (); Panelp2=newPanel (); Panelp3=newPanel ()
/ / addasimplemenuMenum=newMenu ("Application"); m.add (newCheckboxMenuItem ("Base36Active"); m.add (newMenuItem ("Exit"))
/ / addmenutomenubarmb.add (m); setMenuBar (mb); / / installthismenubarintheframe
/ / Addbuttonstotheirownpanelp3.setLayout (newFlowLayout ()); p3.add (d2Binary); p3.add (d2Octal); p3.add (d2Hex); p3.add (d2Base36)
/ / AddtextfieldsLabeldLabel=newLabel ("EnterDeecimal:"); LabelxLabel=newLabel ("ConvertedValue:"); dDisplay=newTextField (integer.toString (decimalValue), 7); xDisplay=newTextField (baseXValue,32); xDisplay.setEditable (false); p1.setLayout (newFlowLayout (FlowLayout.LEFT)); p2.setLayout (newFlowLayout (FlowLayout.LEFT)); p1.add (dLabel); p1.add (dDisplay); p2.add (xLabel); p2.add (xDisplay)
/ / Addthepanelsadd ("North", p1); add ("Center", p2); add ("South", p3);} / / endd2xconstructor
Publicvoidstart () {resize (400150); show ();}
PublicvoidupdateXDisplay () {xDisplay.setText (baseXValue);}
PublicbooleanhandleEvent (Eventevt) {if (evt.targtintanceofMenuItem) {if ("Exit" .equals (MenuItem) evt.target). GetLabel ()) {hide (); dispose (); System.exit (0); returnfalse;} retruntrue;} elseif (evt.targetinstanceofButton) {Stringwhick= ((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 (); returntrue;} returnfalse;}
Publicstaticvoidmain (Stringargs []) {d2xm=newd2x (); m.start ();}}
This is the end of the content of "the running environment of Java application and the passing of parameters". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.