Swing Getting Started 1. Preface: When we have learned the basic syntax in Java, and after familiar with Java's object-oriented foundation, we can start the simple Swing program design, and friends who have used VB may be simple by it. Designing the user interface method, only need to drag a few controls to the form, write Event for each empty piece, you can make an interface design. But powerful Java is not inferior to VB. You can also design a beautiful interface. 2.Swing Overview: When Java1.0 has just appeared, there is no SWING, the GUI basic programming library, Sun is named the AWT (Abstract Window TookIt), the basic AWT library handles the user interface to handle the creation of these elements The GUI toolbox for the underlying operating system is processed to implement WORA's purpose. For various reasons, there is a difference between different OS, making the AWT interface inventory in many bugs. In 1996, Sun has created a new one with Netscape The library is named swing. ------ If there is no Swing, Java's graphics will not be named. ------ Swing is the basic class of Java, is part of the JFC, the complete JFC is very huge There are also many components included .3. Why choose Swing: swing has a richer and more convenient user interface element collection, Swing is less dependent on the underlying platform, so the BUG on the special platform will raise Swing band To cross the unified visual experience 4. Many beginners Java's friends will feel very awkward after learning the basics of Java, or what is not very well understood by Java, so they can be close to the basic concepts. One-step research Swing can improve the beginners. Let's take a preliminary study, personal feeling is a good code of entry Swing.
// a Simple ExMple That Can Show The Basis of Swing -------------------------------------- -------------------------------------------------- --------------------------------- // Import Pakages Which We Needimport Javax.swing. *; Import Java.aw . *; public class HelloCsdn {public static void main (String [] args) {HelloCsdnFrame frame = new HelloCsdnFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.show ();}} / ** this part we construct A New Frame HellocsdnFrame * / -------------------------------------------- -------------------------------------------------- ---------------------------- Class Hellocsdnframe Extends JFrame {public hellocsdnframe () {settitle ("Hello 9cbs.net"); setsize (Width, height); hellocsdnpanel panel = new hellocsdnpanel (); container c = getContentPane (); c.add (panel);} public static final int width = 300; public static final int hotht = 200;} / ** this Part We Extendur Hellocsdnfram to JFrame and Construction A New Object Hellocsdn Panel and add it on the frame / * ----------------------------------------- -------------------------------------------------- ---------------------------------
class HelloCsdnPanel extends JPanel {public void paintComponent (Graphics g) {super.paintComponent (g); g.drawString ( "Hello 9CBS.NET", MESSAGE_X, MESSAGE_Y);} public static final int MESSAGE_X = 100; public static final int MESSAGE_Y = 100;} / ** a panel that display a message * / ----------------------------------- -------------------------------------------------- -------------------------------------- I divide this procedure into 3part. Every part has Note, this code is what is used. To analyze this procedure together: in the first part // Import Pakages Which We needimport javax.swing. *; Import java.awt. *; Public class hellocsdn {public static void main (String [] args) {HelloCsdnFrame frame = new HelloCsdnFrame (); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); frame.show ();}} / ** this part we construct a new frame HelloCsdnFrame * / can see that we first introduced 2 packs Swing and AWT, create an object to instantiate this object, then use code frame.setDefaultcloseOperation (jframe.exit_on_close); Frame.Show (); to achieve the Close Frame, but not end the program, where Just the main thread of the program, the second part: Class Hellocsdnframe Extends Jframe {PUBLIC HELLOCSDNFRAME () {settitle ("Hello 9cbs.net"); setsize (width, height); hellocsdnpanel panel = new hellocsdnpanel (); container c = getContentPane (); c.add (panel);} public static final int WIDTH = 300; public static final HEIGHT = 200 int;} / ** this part we extend our HelloCsdnFram to JFrame and construct a new object HelloCsdnPanel and add it on the frame / * here we put our established succession java object of JFrame Category, make him a JFrame's attribute. Behavior. Then set the title and size, and create a new ObjectHellocsDnPanel because it is implemented in jframe, so the container c. Put our build Panel object in Container C. Part III Class Hellocsdnpanel Extends JPanel {Public Void PainTComponent (Graphics G) {Super.PaintComponent (G); g.drawstring ("Hello 9cbs.net", Message_x, Message_y);
} Public static final int Message_x = 100; public static final int message_y = 100;} / ** a Panel That Display a message * / Continue us to inherit the HellocsDnpanel to JPanel to make our objects with JPanel's properties, then we can Method for calling the character on the Frame G. DrawString By this program we can see the core idea of Java in one side - inheritance, on the other hand, see what the basic architecture of Swing. He has a few A layer, each layer is implemented.
5. Since then we can see the internal structure of Frame: ------ JFrame | --------- JROOT | -------- JLAYEREDPANE | ---- ------- Menu: ----------- Content pane | ----------- Transparent pane (top) and in these 6 layers The most important thing is the menu strip and content pane. Because it felt what our frame is like. Summary: You can see that Swing is a good performance in Java, blame, the book can be written with a thick book, this chapter Just teach those beginners, there is a better understanding for Java, not close to the programming on the console. Swing world is very wonderful, wait for everyone to go to explore