Transfer to Java language programming

xiaoxiao2021-03-06  18

Programming with Java language

Let's take a Java's GUI-based application example (text editor developed by Java).

First create a project through the Project Wizard and Application Wizard, then apply visual design tools, modify UI design, connection events, editing source, and common controls and tasks such as common controls and tasks such as menu items, toolbars, text areas, and system events. Specific techniques include:

* Let the user choose a text file with the JFilechooser dialog box.

* Read and write and process text in the text file with Jtextarea.

* Set the foreground color and background colors.

* Set the font with the DBSWING FONTCHOOOSER dialog.

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

* Handmade the code that handles the UI event.

* By placing the code in a new Help method that can be called by the menu item and the button two event processor, the menu item and the button perform the same code.

* Add a right-click menu to the JTextArea control.

* Keep the tracking of the file and whether the file is active, show the file | New, File | Open, File | Save, File | Save, Edit, and Exit.

* Expand the "Text Editor" application as JAR file.

Swing component introduction

Before you tell the source program, you will introduce the SWING component (these components will be used in the source program), Swing is a GUI toolkit with rich components, which make up the JFC (Java Foundation Class) user interface function At the core part, it is not only the alternatives of the components provided by AWT, but also has great progress on the basis of these components. Swing is 100% pure Java, which does not rely on specific Windows systems, can be implemented on various platforms. The most attractive thing in swing is that it is supported inserted into the Pl & F (Pluggable Look and Feel), allowing users to customize their desktops, replace new color schemes, allow window systems to adapt to your habits and needs. The Swing PL & F architecture makes it easier to customize the Swing control or control group. Swing also provides some predefined appearances (PL & F), including the default, Motif and Windows look.

Swing contains more than 250 classes, providing more than 40 components, four times the AWT component, Swing also provides a large number of additional components needed to develop the GUI interface, which are defined in the Javax.swing extension package. In order to distinguish between the original Java.awt package, Sun specifically sets the expansion package (Javax.swing), and specifies that all SWING components begin with letters "J", such as JLABLE, JBUTTON, JTEXTFIELD, etc. Different from the AWT components. Below is a 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 the component is similar to the components of the same species of the AWT, which is not elaborated.

Text Editor Program contains three Java source programs, TEXTEDITFRAME.JAVA, TextEditclass.java, and TextEditFrame_aboutbox.java programs, below, will be described below:

Source Code of TextEditFrame.java (section section)

Package texteditor;

//Texteditframe.java

Import 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 control Internetization: Localized application, you need to add a line of code to swing control JFilechooser

// and JColorchooser appears in the language running

JPanel ContentPane; // Setting the Contentpane's JPANEL control

JMenuBar menubar1 = new jmenubar (); // Create a menu bar and add it to the frame form

JMenu menufile = new jmenu (); // Create a File menu and the corresponding menu item

JMenuitem menufileExit = new jmenuitem ();

JMenu Menuhelp = new jmenu (); // Create a Help menu and the corresponding menu item

JMenuItem MenuhelPabout = new jmenuitem ();

JToolBar Toolbar = New JTOOLBAR (); // Create toolbar components

JButton JButton1 = new jbutton (); // Create button components

JButton JButton2 = new jbutton ();

JButton JButton3 = new jbutton ();

ImageICON image1; // Define icon

ImageICON image2;

ImageICON image3;

JLabel Statusbar = New Jlabel (); // Create tag components

BorderLayout BorderLayout1 = New BorderLayout (); // Creating a BorderLayout Formwork

JScrollPane JscrollPane1 = new jscrollpane (); // Create a scroll window control

Jtextarea jtextarea1 = new jtextarea (); // Create multi-line text domain components

JMenuItem jMenuitem1 = new jmenuitem (); // Create a menu item

JMenuitem Jmenuitem2 = new jmenuitem ();

JMenuitem Jmenuitem3 = new jmenuitem ();

JMenuitem Jmenuitem4 = new jMenuItem ();

Fontchooser fontchooser1 = new fontchooser (); // Creating a Dictionary Selection dialog

JMENU JMENU1 = New JMENU ();

JMenuitem Jmenuitem5 = new jMenuItem ();

JMenuitem Jmenuitem6 = new jmenuitem ();

JMenuitem Jmenuitem7 = new jmenuitem ();

Jfilechooser jfilechooser1 = new jfilechooser (); // Create text selection dialog

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 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 Form

Document1 = jtextarea1.getdocument (); // Multi-line text domain documentation

ContentPane.setLayout (borderLayout1); // borderlayout layout

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 event listener

Menuhelp.Settext ("Help");

MenuhelPabout.Settext ("About");

MenuhelPabout.AddActionListener (New TextEditFrame_MenuhelPabout_ActionAdapter

(this));

JButton1.seticon (image1); // Set three toolbar button icons, add event listeners

JButton1.AddActionListener (New TextEditFrame_jbutton1_ActionAdapter (this));

JButton1.SetTooltiptext ("Open file");

JButton2.Seticon (image2); jbutton2.addactionListener (new texteditframe_jbutton2_actionadapter (this));

JButton2.SetTooltiptext ("Close File");

JButton3.Seticon (image3);

JButton3.AddActionListener (New TextEditFrame_jbutton3_ActionAdapter (this));

JButton3.SetTooltiptext ("About");

Jtextarea1.setLineWrap (TRUE);

Jtextarea1.SetWrapStyleWord (TRUE);

Jtextarea1.setBackground (color.white);

JMenuitem1.Settext ("new"); // Settings menu, add event listeners

JMenuitem1.AddActionListener (New

TextEditFrame_JMENUITEM1_ACTIONADAPTER (THIS);

JMenuitem2.Settext ("open");

JMenuitem2.AddActionListener (New

TextEditFrame_JMENUITEM2_ACTIONADAPTER (THIS));

JMenuitem3.Settext ("save");

JMenuitem3.AddActionListener (New

TextEditFrame_JMENUITEM3_ACTIONADAPTER (THIS));

JMenuitem4.Settext ("Save AS");

JMenuitem4.AddActionListener (New

TextEditFrame_JMenuitem4_ActionAdapter (this));

Fontchooser1.setframe (this);

Fontchooser1.SetTitle ("font");

JMenu1.Settext ("Edit");

JMenuitem5.Settext ("font");

JMenuitem5.AddActionListener (New

TEXTEDITFRAME_JMENUITEM5_ACTIONADAPTER (THIS));

JMenuitem6.Settext ("Foreground Color");

JMenuitem6.AddActionListener (New

TextEditFrame_JMENUITEM6_ACTIONADAPTER (THIS));

JMenuitem7.Settext ("Background Color");

JMenuitem7.AddActionListener (New

TextEditFrame_JMENUITEM7_ActionAdapter (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 (); // Single component adds a separation line

Menufile.Add (MenufileExit);

Menuhelp.Add (MenuhelPabout);

Menubar1.add (menufile);

Menubar1.Add (jMenu1);

Menubar1.Add (Menuhelp);

This.Setjmenubar (Menubar1);

ContentPane.Add (Toolbar, BorderLayout.North); // Content Window Setting BorderLayout Layer

ContentPane.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 description:

TextEditFrame.java is the main program for implementing the text editor, with the following features:

(1) Produce a text area that is fully full user interface top menu bar and the bottom status bar. The layout manager of the primary user interface container needs to use BorderLayout. In the main container, a JPANEL control called the content window is changed to the boundary layout, which is only required to add a text area control in the content window. To do this, first add a scroll window in the content window, and then put the text area control (JTextArea) in the scroll window. The scroll window provides a text area with rolling bats (JSCOLLPANE).

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

New Post(0)