Beginner's Primer: basic knowledge of Java swing-full touch

Source: Internet
Author: User
Tags final interface net touch java jframe

1. Preface:

When we have learned the basic syntax in Java, and familiar with Java object-oriented Foundation, we can start a simple swing program design, the use of VB Friends may be its simple design of the user interface method 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 less 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 achieve the purpose of Wora. And for a variety of reasons, differences between different OS, so that the AWT interface inventory in many bug.1996 years Sun and Netscape collaborated to create a new library named swing.------IF, There is no Swing,java graphics on the face of the penniless.------Swing is the base class for Java, a part of JFC, and the complete JFC is huge. There are also a lot of components included.

3. Why to choose Swing:

Swing has a richer and more convenient set of user interface elements, and 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.Swing Getting Started example:

Many novice Java friends will be confused when they finish learning the basics of Java, or what Java can do is not very understanding, so in the initial grasp of the basic concept can be a step closer to the study of swing, can be a certain improvement for beginners. We're going to take a preliminary look at a program, Personally feel like a good code for getting started swing.

 
        
         //First part 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 ();   }//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;   //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; }
      
        

I divide the program into 3part. Each section has a comment, and this piece of code is used for what. To analyze This program together:

In the first part

       
        
         
        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 (); }   }
       
        

You can see that we first imported 2 packages of swing and AWT, created an object to instantiate the object and then frame.setdefaultcloseoperation with code (JFRAME.EXIT_ON_CLOSE); Frame.show (); To implement the close frame, but not the end of the program, its abort is only 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; }
       
        

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

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; }
       
        

We continue to inherit the newly established hellocsdnpanel to JPanel so that our objects have JPanel attributes, and then we can invoke the method of outputting the characters on the frame g.drawstring

From this program we can very well see the core of Java----inheritance relationship, on the other hand, we can see what the basic structure of swing. He has several layers, each of which implements its own 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.