Basics of getting started with Java swing

Source: Internet
Author: User
Tags extend final inheritance interface java jframe
Self-
Day line







1. Preface:


when we have learned the basic syntax in Java, and familiar with the Java Object-oriented Foundation, we can start a simple swing program design, the use of VB Friends may be its simple design user interface approach to attract, just drag a few controls to the form, Writing an event for each empty piece can be a simple interface design. But the powerful Java is no better than VB? You can also design a beautiful interface.








2.Swing Overview:





when java1.0 just appeared without swing, when the GUI Basic programming library, Sun named AWT (Abstract Window tookit), the basic AWT library processes the user interface by handing the creation behavior of these elements to the The GUI toolbox of the underlying operating system is processed to realize the purpose of Wora. For a variety of reasons, there are differences between different OS, making the AWT interface inventory in many bugs. Sun worked with Netscape in 1996 to create a new library named swing.





------If, there is no Swing,java graphical interface penniless.------


Swing is the basic class of Java, is a part of JFC, complete JFC is very large, including a lot of components.





3. Why choose Swing:swing a richer and more convenient set of user interface elements, swing has less reliance on the underlying platform, so fewer bugs on special platforms will bring a unified visual experience on the cross platform





4. Many beginners Java friends after learning the basic part of Java will feel very dazed, or what Java can do is not very understanding, so in the initial grasp of the basic concept can be near a step of research swing, can be a certain improvement for beginners.





below we'll come to the preliminary study of a program that individuals feel is a good code for getting started swing.





A simple exmple so can show the basis of swing


-------------------------------------------------------------------------


//import pakages which we need


import 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 csdn.net");


setSize (width,height);


Hellocsdnpanel panel=new Hellocsdnpanel ();


Container C=getcontentpane ();


C.add (panel);


}


public static final int width=300;


public static final int height=200;


}


/**this Part We extend my hellocsdnfram to JFrame and


construct a new object Hellocsdnpanel and add it on the frame


/*


--------------------------------------------------------------------


class Hellocsdnpanel extends jpanel{


public void Paintcomponent (Graphics g) {


Super.paintcomponent (g);


g.drawstring ("Hello csdn.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. Each section has a comment, which is what this piece of code does. To analyze This program together:





in the first part





//import pakages which we need


import 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 imported 2 packages of swing and AWT, created an object to instantiate this object, and then used the code to





frame.setdefaultcloseoperation (jframe.exit_on_close);


frame.show (); To implement the close frame, but not the end of the program, its abort is just the main thread of the program,





Part II:





class Hellocsdnframe extends jframe{


public hellocsdnframe ()


{


settitle ("Hello csdn.net");


SetSize (Width,height);


hellocsdnpanel panel=new Hellocsdnpanel ();


Container C=getcontentpane ();


C.add (panel);


}


public static final int width=300;


public static final int height=200;


}


/**this Part We extend my hellocsdnfram to JFrame and


construct a new object Hellocsdnpanel and add it on the frame


/*





Here we take the object we created to the Java JFrame class, so that he has jframe attributes. Behavior. Then set the title and size, and create a new object again Hellocsdnpanel This is because it was implemented in JFrame, so to build container c. Put the Panel object we built into the CONTAINERC.





Part III





class Hellocsdnpanel extends jpanel{


public void Paintcomponent (Graphics g) {


Super.paintcomponent (g);


g.drawstring ("Hello csdn.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 our inheritance of the newly established Hellocsdnpanel





to JPanel makes our objects have JPanel properties, and then we can invoke the method of outputting characters on the frame g.drawstring


This program on the one hand, we can see the core of Java thinking----inheritance relationship, on the other hand can see the basic structure of swing.





He has several layers, and each layer implements its own what function.





5. From this we can see the internal structure of the frame:





------JFrame (bottom)


|


---------Jroot


|


---------JLayeredPane


|


-----------Menu Bar


|


-----------Content Pane


|


-----------Transparent pane (top level)





and in these 6 layers we are most related to the menu bar and the content pane. Because it feels like our frame.





Summary: You can see that swing is a good Java performance, no wonder that the book on Swing can be written in a very thick, this chapter is only to teach those beginners, for Java has a better understanding, not near to stay in the console programming.











Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.