Conversion to Java programming

Source: Internet
Author: User
Programming in Java

The following is an example of a Java GUI-based application (a text editor developed using Java ).

First, create a project through the Project Wizard and Application Wizard, and then apply the visual design tool to modify the uidesign and Connection events, edit the source code and common controls and tasks, such as menu items, toolbar, text area, and system events. Specific technologies include:

* Use the jfilechooser dialog box to select a text file.

* Use jtextarea to read, write, and process text in a text file.

* Set the foreground color and background color.

* Use the dbswing fontchooser dialog box to set the font.

* Display information in the status bar and window title bar.

* Manually add code for processing UI events.

* Place the code in a new "help" method that can be called by both the menu item and the button event processor, so that the menu item and the button execute the same code.

* Add a right-click menu for the jtextarea control.

* Keep track of the file location and whether the file has been active, and display the processing of the logic such as file | new, file | open, file | save, file | Save As, edit and exit.

* Expand the "Text Editor" application as a jar file.

Swing component Introduction

Before explaining the source program, we will introduce the swing components (these components will be used in subsequent source programs). Swing is a GUI toolkit with rich components, which forms a jfc (Java foundation class) it is not only a substitute for the components provided by AWT, but also makes great progress on the basis of these components. Swing is 100% pure Java and can be implemented on various platforms without relying on specific Windows systems. Swing's most attractive support for pluggable PL & F (Pluggable look and feel) allows users to customize their desktops and change their color schemes, adapt the window system to your habits and needs. The swing PL & F architecture makes it easier to customize swing controls or control groups at the same time. Swing also provides some predefined appearances (PL & F), including the default, motif, and Windows appearances.

Swing contains more than 250 classes and more than 40 components, four times that of AWT components. Swing also provides a large number of additional components required to help develop the GUI, they are all defined in javax. swing expansion package. To match the original Java. the AWT package is different. Sun sets the extension package (javax. swing), and make sure that all swing components start with the letter "J", such as jlable, jbutton, and jtextfield, to distinguish them from AWT components. The following is the class hierarchy of the swing component.


     
      JComponent  |——AbstractButton——|——JToggleButton——|——JCheckBox  |——JColorChooser     |——JButton          |——JRadioButton  |——JFileChooser      |——JMenuItem    |——JLabel                    |——JMenu  |——JMenuBar                  |——JRadioButtonMenuItem    |——JPanel                    |——JCheckButtonMenuItem  |——JToolBar  |——JScrollPane  |——JTextCompoment——|——JTextArea  |——JOptionPane       |——JTextField——JPasswordField  |——JScrollPane       |——JEditorPane——JTextPane   .........   .........
     

The meaning of a component is similar to that of an AWT component.

The Text Editor contains three Java source programs: texteditframe. Java, texteditclass. Java, and texteditframe_aboutbox.java. The following describes the program as follows:

Source code of texteditframe. Java (excerpt)


     
      
Package texteditor; // texteditframe. javaimport Java. AWT. *; // import class import Java. AWT. event. *; import javax. swing. *; import COM. borland. dbswing. *; import Java. io. *; import javax. swing. text. *; import javax. swing. event. *; public class texteditframe extends jframe {intlswingsupport intlswingsupport1 = new intlswingsupport (); // swing controls are networked: localized applications, you need to add a line of code so that the swing control jfilechooser // and jcolorchooser appear in the language where the program is running. // set the jpanel control jmenubar menubar1 = new jmenubar () in the content window (contentpane (); // create a menu bar and add it to the framework form. jmenu menufile = new jmenu (); // create a file menu and corresponding menu item jmenuitem menufileexit = new jmenuitem (); jmenu menuhelp = new jmenu (); // create the Help menu and corresponding menu item jmenuitem menuhelpabout = new jmenuitem (); jtoolbar toolbar = new jtoolbar (); // create a toolbar component jbutton jbutton1 = new jbutton (); // create a button component jbutton jbutton2 = new jbutton (); jbutton jbutton3 = new jbutton (); imageicon image1; // define the icon imageicon image2; imageicon image3; jlabel statusbar = new jlabel (); // create the tag component borderlayout borderlayout1 = new borderlayout (); // create borderlayout layout. jscrollpane jscrollpane1 = new jscrollpane (); // create a rolling window control jtextarea jtextarea1 = new jtextarea (); // create a multi-line text field component jmenuitem jmenuitem1 = new jmenuitem (); // create a menu item jmenuitem jmenuitem2 = new jmenuitem (); jmenuitem jmenuitem3 = new jmenuitem (); jmenuitem identifier = new jmenuitem (); fontchooser identifier = new fontchooser (); // jmenu jmenu1 = new jmenu (); jmenuitem jmenuitem5 = new jmenuitem (); jmenuitem jmenuitem6 = new jmenuitem (); jmenuitem jmenuitem7 = new jmenuitem (); jfilechooser identifier = new jfilechooser (); // create Text Selection dialog box string currfilename = NULL; // full path with filename. null means new/Untitled. boolean dirty = false; document Document1; // text dbtextdatabinder dbtextdatabinder1 = new dbtextdatabinder (); // true means modified text. // construct the frame public texteditframe () {enableevents (awtevent. window_event_mask); try {jbinit (); updatecaption ();} catch (exception e) {e. printstacktrace () ;}/// component initialization private void jbinit () throws exception {// three toolbar button icons image1 = new imageicon (texteditframe. class. getresource ("openfile.gif"); image2 = new imageicon (texteditframe. class. getresource ("closefile.gif"); image3 = new imageicon (texteditframe. class. getresource ("help.gif"); contentpane = (jpanel) This. getcontentpane (); // content chuangge Document1 = jtextarea1.getdocument (); // multi-line text domain document contentpane. setlayout (borderlayout1); // This. setsize (new dimension (400,300); // window size this. settitle ("Text Editor"); // window title statusbar. settext (""); menufile. settext ("file"); menufileexit. settext ("exit"); menufileexit. addactionlistener (New texteditframe_menufileexit_actionadapter (this); // Add the event listener menuhelp. settext ("help"); menuhelpabout. settext ("about"); menuhelpabout. addactionlistener (new listener (this); jbutton1.seticon (image1); // set three toolbar button icons to add the event listener jbutton1.addactionlistener (New texteditframe_jbutton#actionadapter (this )); encrypt ("Open File"); jbutton2.seticon (image2); jbutton2.addactionlistener (new listener (this); jbutton2.settooltiptext ("Close file"); jbutton3.seticon (image3 ); jbutton3.addactionlistener (new listener (this); jbutton3.settooltiptext ("about"); jtextarea1.setlinewrap (true); Combine (true); jtextarea1.setbackground (color. white); jmenuitem1.settext ("new"); // set the menu to add the event listener jmenuitem1.addactionlistener (New texteditframe_jmenuitem1_actionadapter (this); jmenuitem2.settext ("open "); jmenuitem2.addactionlistener (new listener (this); jmenuitem3.settext ("save"); jmenuitem3.addactionlistener (new listener (this); jmenuitem4.settext ("Save "); jmenuitem4.addactionlistener (new listener (this); fontchooser1.setframe (this); fontchooser1.settitle ("font"); jmenu1.settext ("edit"); jmenuitem5.settext ("font "); jmenuitem5.addactionlistener (new listener (this); jmenuitem6.settext ("foreground color"); jmenuitem6.addactionlistener (new listener (this); jmenuitem7.settext ("background color "); jmenuitem7.addactionlistener (new listener (this); document1.adddocumentlistener (New texteditframe_document1_documentadapter (this); dbtextdatabinder1.setjtextcomponent (jtextarea1); // turn off right-click File Open... menu item. dbtextdatabinder1.setenablefileloading (false); // turn off right-click File Save... menu item. dbtextdatabinder1.setenablefilesaving (false); toolbar. add (jbutton1); // tool component Add button toolbar. add (jbutton2); toolbar. add (jbutton3); menufile. add (jmenuitem1); // menu component add menu item menufile. add (jmenuitem2); menufile. add (jmenuitem3); menufile. add (jmenuitem4); menufile. addseparator (); // Add the separation line menufile to the Collection ticket component. add (menufileexit); menuhelp. add (menuhelpabout); menubar1.add (menufile); menubar1.add (jmenu1); menubar1.add (menuhelp); this. setjmenubar (menubar1); contentpane. add (toolbar, borderlayout. north); // set the contentpane In the content window. add (statusbar, borderlayout. south); contentpane. add (jscrollpane1, borderlayout. center); jscrollpane1.getviewport (). add (jtextarea1, null); jmenu1.add (jmenuitem5); jmenu1.add (jmenuitem6); jmenu1.add (jmenuitem7);} // display the about box. void helpabout () {texteditframe_aboutbox DLG = new texteditframe_aboutbox (this); dimension dlgsize = DLG. getpreferredsize (); dimension frmsize = getsize (); point loc = getlocation (); DLG. setlocation (frmsize. width-dlgsize. width)/2 + loc. x, (frmsize. height-dlgsize. height)/2 + loc. y); DLG. setmodal (true); DLG. show ();}..................
     

Programming Skills:

Texteditframe. Java is the main program for implementing the text editor. It has the following features:

(1) create a hidden area fully filled with the area between the top menu bar and the bottom status bar of the user interface. The layout manager of the main user interface container must adopt the border layout (borderlayout ). The main container contains a jpanel control called the content window (contentpane) and is changed to a border layout. All you need to do is add a border control in the content window. To this end, add a scroll window in the content window, and then place the preview area control (jtextarea) in the scroll window ). The rolling window provides a rolling area with a Rolling Rod (jscollpane.

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.