How to design a "pure" Java program with GUI features

zhaozj2021-02-17  145

The Java language is now very popular in the world, and its application involves e-commerce, Internet online database, CGI, and multimedia and other aspects. In December 1999, Sun announced the latest version of Java2 SDK, laying the foundation for Java in the new century.

Java is an object-oriented programming language (OOP). It is a language of C , because Java eliminates C / C trouble memory management and some complex constructs, such as multi-inheritance, etc., Java's application is much easier than C , and Java's running speed has been similar to C almost According to the relevant US departments). There is also a mechanism that across soft hardware platforms and compilation, multi-run mechanism, how can you not let programmers?

Java has two forms: Applet and Application (Application), in order to simplify, this article only discusses the application, the applet is embedded after the Java protocol is compiled, and the IE or NC Wait until the form of browser uses, we will give an application instance.

Friends who have used VC , VB, Delphi, and Powerbuilder know that the GUI (Graphics User Interface) graphical interface is very easy to use the mouse to drag the control with a mouse. After the Java came, there were many famous big companies to develop the integrated GUI development environment software, such as Microsoft's Visual J , Borland's JBuilder, Symantec's Cafe, Visual Cafe, etc. Although these products support the GUI interface, the development program is more vivid, but it also has the following shortcomings:

1) The Java version lags behind Sun's latest version.

2) Bring compatibility issues, due to the development of their own class library files, such as MS with their own WFC class libraries, causing Java, JDK (SDK), and JFC (Java Foundation Classes) that cannot be in fact Java standard Sun. ) Compatible, causing a disadvantage that it is difficult to cross the platform.

3) The control file is huge, compiled and slow, and the running file is also large.

4) The development of high development is high, some products cannot be "good to the end", halfway die, and users are in passive situation. Sun's Java, JFC, etc. are free, and SUN has a huge technical force guarantee.

Due to the above reasons, "Pure Java", "Pure Java", is now popular internationally, that is to say only SUN's Java, JDK, JFC (GUI class library, similar AWT, but with more powerful features than AWT Also known as Swing is "swing") to develop procedures. In fact, just learn about Java's programming principle, and JDK and powerful JFC (SWING), we can write programs with professional level of GUI interfaces.

The author takes a program as an example to introduce how to prepare a program with "pure" Java. The key statement of the program has an explanation:

Import java.awt. *; import java.awt.event.windowadapter; import java.awt.event.windowevent; import com.sun.java.swing. *; public class mygui extends com.sun.java.swing.jframe { // Set the environment constant here, such as: font, color, etc. com.sun.java.swing.jPanel mainpanel; com.sun.java.swing.jpa.java.swing.jbutton clearbutton; com.sun.java.swing.jbutton searchbutton; com.sun.java. Swing.jtextfield searchText; // constructor: initialization and instantiation of setMenu () and setgui () and calling and instantiate public mygui () {Super ("Ideal Gui"); AddWindowListener (New Windowadapter () {public void windowClosing (WindowEvent E) {Dispose (); system.exit (0);}}); // Create a window event setmenu (); setgui ();} protected void clearbuttonclicked () {system.out.println ("Clear Button Clicked "); SearchText.requestFocus (); SearchText.selectAll ();} // button event processing subroutine ClearButton protected void SearchButtonClicked () {System.out.println (" Search button Clicked "); SearchText.requestFocus (); SearchText .selectAll ();} // button event processing subroutine SearchButton class buttonAction implements java.awt.event.ActionListener {public void actionPerformed (java.awt.event.ActionEvent event) {Object object = event.getSource (); if ( Object == ClearButton) ClearButtonClicked (); else if (Object == Searchbu TTON) SearchButtonClicked ();} // end public void actionPerformed (...} // listens, such as button press, mouse Press, etc., turn to the corresponding processing.

... public void setgui () {TopPanel = new com.sun.java.swing.jPanel (); // Toppanel panel java.awt.gridbagLayout TopLayout = new java.awt.gridbagLayout (); TopPanel.setLayout (TOPLAYOUT) ClearButton = new com.sun.java.swing.jbutton ("clear"); // ClearButton button searchText = new com.sun.java.swing.jtextfield ("Enter Text Here."); // SearchText Text Box SearchButton = New JButton ("Search"); // SearchButton button TopPanel.add (searchText); // Install searchText text box on TopPanel TopPanel.Add (SearchButton); // Install SearchButton button on Toppanel TopPanel.Add (ClearButton ); // Install ClearButton button on TopPanel = new com.sun.java.swing.jPanel (); // Main panel mainpanel java.awt.gridbagLayout mainlayout = new java.awt.gridbagLayout (); mainpanel.setlayout mainLayout); java.awt.GridBagConstraints MainConstraints = new java.awt.GridBagConstraints (); setConstraints (MainConstraints, GBCBOTH, TOPPANELX, TOPPANELY, 1,1); // TopPanel MainLayout.setConstraints (TopPanel, MainConstraints); MainPanel.add ( Toppanel); // Put TopPanel into Main Plate Mainpanel this.getco ntentPane (). add (mainpanel); // Load the main panel to the JFrame Framework THISSETSIZE (640, 480); // Set the display size, change this.setVisible (TRUE) in the program; // Settings SearchText. Requestfocus (); // text box focus (selection) searchText.selectall (); buttonAction baction = new buttonction (); ClearButton.AddActionListener (BACTION); // Add button to the active listener in SearchButton.addActionListener (BACTION); } Public static void main (string args []) {MyGUI mgui = new mygui ();} // start the main program

The Java's GUI interface is composed of a series of embedded components, which uses JDK and JFC (Swing) components. This example establishes a frame (JFRAME), built a main panel again, then establish a sub-panel down, then insert the two buttons and a text box into the sub-panel, this is a typical OOP instance, after the program captures two buttons, and then the button information is displayed after selecting the contents of the text box. This software is expanded to add more sub-panels, and more more complex components (such as JTABLE-forms or JTREE-trees). Note that this mygui.java file can be edited with any text editing software, with a plain text method store. JFC components are more beautiful than the AWT components, the same buttons, JFC's good-looking, and can make more settings. After installing Java 1.2 (JDK) and JFC (Swing1.1.1 or later, these systems can be compiled and running in Windows DOS mode after downloading).

Check the files first: javac mygui.java If any error can be used to run files: java mygui.class can see a beautiful interface at this time.

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

New Post(0)