Java swing Basics

Source: Internet
Author: User

1. Preface:

After learning the basic syntax in Java and getting familiar with the object-oriented basics of Java, we can start simple swing program design, friends who have used VB may be attracted by its simple user interface design method. They only need to drag several controls to the form, you can simply design the interface by writing an event for each empty part. but is powerful Java no inferior to VB? You can also design exquisite interfaces.

2. Swing Overview:

When java1.0 appeared, there was no swing, and sun was named AWT (Abstract Window Tookit) as the basic GUI programming library at that time ), the basic AWT library processes the user interface by handing over the creation behavior of these elements to the GUI toolbox of the underlying operating system for the purpose of Wora. For various reasons, there are differences between different operating systems, making AWT interface inventory a lot of bugs. In 1996, sun and Netscape jointly created a new library named swing.

------ If there is no swing, the Java graphical interface will be omitted .------
Swing is a basic class of Java and a part of jfc. The complete jfc is huge and contains many components.

3. why choose swing? Swing has a richer and more convenient collection of user interface elements, and swing has less dependence on the underlying platform. Therefore, there are few bugs on special platforms. Swing will bring a unified visual experience on Cross platforms.

4. after learning the basics of Java, many beginners will feel very confused. I still don't know much about what Java can do, therefore, after a preliminary understanding of the basic concepts, you can study swing in the near future, which can be improved for beginners.

Next we will take a preliminary look at a program. I personally think it is a good code for getting started with swing.

// A simple exmple that 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. setdefaclocloseoperation (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 finals int width = 300;
Public static final int size = 200;
}
/** This part we extend our 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 displays a message
*/

I divide this program into three parts. Each part has comments. What is the purpose of this piece of code. To analyze the 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. setdefaclocloseoperation (jframe. exit_on_close );
Frame. Show ();
}
}
/** This part we construct a new frame hellocsdnframe
*/

We can see that we first imported two packages, swing and AWT, and created an object to instantiate this object, and then used the code

Frame. setdefaclocloseoperation (jframe. exit_on_close );
Frame. Show,

Part 2:

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 finals int width = 300;
Public static final int size = 200;
}
/** This part we extend our hellocsdnfram to jframe and
Construct a new object hellocsdnpanel and add it on the frame
/*

Here we inherit the jframe class of Java from the object we created so that it has the attributes of jframe. action. then set the title and size, and create a new object hellocsdnpanel. This is because it is implemented in jframe, so we need to create a container C. put the Panel object we created into containerc.

Part 3

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 displays a message
*/Continue to inherit the newly created hellocsdnpanel

To jpanel, make our object have the attributes of jpanel, and then we can call the method G. drawstring that outputs characters on the frame.
From this program, we can see the core idea of Java-inheritance relationship, and what is the basic architecture of swing.

It has several layers, and each layer implements its own functions.

5. From then on, we can see the internal structure of the frame:

------ Jframe (bottom layer)
|
--------- Jroot
|
--------- Jlayeredpane
|
----------- Menu bar
|
----------- Content pane
|
----------- Transparent pane (top layer)

In these six layers, the menu bar and content pane are the most relevant, because it determines what our frame is.

Conclusion: it can be seen that swing is a good performance of Java. No wonder swing books can be written into a thick book. This chapter only teaches beginners and has a better understanding of Java, not very close is the programming that stays on the console.

Related Article

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.