"Java Applications (application)"

Source: Internet
Author: User

1 You can do this when you write a Java application (application):2 31, define the package name.42, import the related packages.53, a class is defined. 64, define the related variables. 75, define a constructor function. (Call the Init () method and the Addevents () method within the constructor)86, assemble the various components in the init () function. 97, add event listeners for various components in the Addevents () method. (Can have exception captures and declarations). Ten8, define the main function. You can create an object of this class in the main function.  One the functions of the calling program can be accomplished by means of constructors.  A  - examples are as follows: -  the  PackageMyClass; - ImportJava.awt.FileDialog; - ImportJava.awt.Frame; - ImportJava.awt.Menu; + ImportJava.awt.MenuItem; - ImportJava.awt.MenuBar; + ImportJava.awt.TextArea; A Importjava.awt.event.ActionEvent; at ImportJava.awt.event.ActionListener; - Importjava.awt.event.WindowEvent; - ImportJava.awt.event.WindowAdapter; - ImportJava.io.BufferedReader; - ImportJava.io.File; - ImportJava.io.FileReader; in  -  Public classDemo1 to { +     Privateframe frame; -     PrivateMenuBar MenuBar; the     Privatemenu menu; *     PrivateMenu submenu; $     PrivateMenuItem Exititem,openfileitem;Panax Notoginseng     PrivateFileDialog openFile; -     PrivateTextArea ta; the  +     //constructor function. A      PublicDemo1 () { the init (); + addevents (); -     } $      Public voidinit () { $frame =NewFrame ("Menu Test"); -Frame.setbounds (300,200,500,400); -         //The menu bar. theMenuBar =NewMenuBar (); -         //menu. Wuyimenu =NewMenu ("File"); the         //menu item. -Openfileitem =NewMenuItem ("Open"); WuExititem =NewMenuItem ("Exit"); -         //add a menu item. About menu.add (openfileitem); $ menu.add (exititem); -         //menu bar to add menus. - menubar.add (menu); - Frame.setmenubar (menuBar); A         //The text field. +TA =NewTextArea (); the Frame.add (TA); -Frame.setvisible (true); $     } the  the      Public voidaddevents () { theFrame.addwindowlistener (NewWindowadapter () { the              Public voidwindowclosing (windowevent e) { -System.exit (0); in             } the         }); the         //Menu list to exit. AboutExititem.addactionlistener (NewActionListener () { the              Public voidactionperformed (ActionEvent e) { theSystem.exit (0); the             } +         }); -         //open the file. theOpenfileitem.addactionlistener (NewActionListener () {Bayi              Public voidactionperformed (ActionEvent e) { theOpenFile =NewFileDialog (Frame, "Open file", filedialog.load); theOpenfile.setvisible (true); -String DirName =openfile.getdirectory (); -String FileName =openfile.getfile (); the System.out.println (dirName); the                 //read the presentation file. the                 if(DirName = =NULL|| FileName = =NULL){ the                     return; -                 } theFile File =NewFile (dirname,filename); the                 Try the                 {94BufferedReader br =NewBufferedReader (Newfilereader (file)); the String Line; theStringBuilder Text =NewStringBuilder (); the                      while(line = Br.readline ())! =NULL){98 Text.append (line); AboutText.append ("\ r \ n"); -                     }101 Ta.settext (text.tostring ());102                 }103                 Catch(Exception E1)104                 { the e1.printstacktrace ();106                 }107             }108         });109     } the      Public Static voidMain (string[] args)111     { the         NewDemo1 ();113     } the}

"Java Applications (application)"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.