Java SWING Getting Started Basics

xiaoxiao2021-03-06  114

1. Preface: When we have learned the basic syntax in Java, and after familiar with Java's object-oriented foundation, we can start the design of simple Swing programs, and users who have used VB may be used by its simple design user interface. The method is attracted, just dragging a few controls into the form, writing Events for each empty piece, you can simply implement the interface design. But strong Java is not more than VB influence? It 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 AWT (Abstract Window Tookit), the basic AWT library handles the user interface to handle the creation of these elements Give, the GUI toolbox for the underlying operating system is handled to implement the purpose of WORA. Because of various reasons, there is a difference between different OS, making the AWT interface inventory in many bugs. In 1996, Sun has created a new library with NetScape. ------ If there is no Swing, Java's graphical interface is unknown. ------ Swing is the basic class of Java, is part of JFC, complete JFC is very huge, including components a lot of. 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 will bring a unified visual on the cross platform. Experience 4. Many beginners Java's friends will feel very awkward after learning Java's basics, or what is not very well understood by Java, so after initially mastering basic concepts, you can study Swing, you can Scholars have certain improvements. Let's take a preliminary study, personal thinking 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 .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 * / ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------- 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 extend jframe and construct a new object hellocsdnpanel and add it on the frame / * ------------------------- ----------------------------------------- Class Hellocsdnpanel Extends JPanel {Public Void PainTcom Ponent (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 program into 3part. Every part is annotated, 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 * / You can see 2 packs swing and AWT, create an object to instantiate this object, then Use code

Frame.SETDEFAULTCLOSEOPERATION (jframe.exit_on_close); frame.show (); to implement Close Frame, but not an end program, where the procedure is 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 int rtwi Height = 200;} / ** This part we extend our Hellocsdnfram to jframe and construct a new object hellocsdnpanel and add it on the frame / * here we build our Object inherit the Java's JFrame class so that he has JFrame's properties Behavior. Then set the title and size, create a new Object HellocsdnPanel because it is implemented in JFrame, so the container c. Put our build PANEL object in Containerc. the third part

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 JPAnel to make our objects with JPanel's properties, then we can call Methods of output characters on the frame G.DrawString By this program We can see the core idea of ​​Java in a good side - inheritance relationship, on the other hand, see what the basic architecture of Swing is. He has several layers, and every layer is realized. 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: It can be seen that Swing is a good performance of Java, and no wonder the Swing book can be written into a very thick one. This chapter just teaches those beginners. There is a better understanding for Java, not close to staying at the console Programming.