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

zhaozj2021-02-11  182

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

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 prone, 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 environment constants 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 == searchbutton) SearchButtonClicke d ();} // end public void actionperformed (...} // listens to the occurrence of the event, such as the button presses, the mouse piping, etc., turn to the corresponding process.

... 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.getContentPane (). Add (Ma INPANEL); // Load the main panel into the JFrame Framework this.setSize (640, 480); // Set the display size, change this.setVisible (TRUE) in the program; // Settings can display searchText.RequestFOCUS (); // Text box focus (selection) searchText.selectll (); buttonAction baction = new buttonction (); ClearButton.addActionListener (BACTION); // Add button to the active listener SearchButton.AddActionListener (baction);} public static void main String args []) {MyGUI mgui = new mygui ();} // Start the main program Java's GUI interface consists of a series of embedded components, which uses the 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.

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

New Post(0)