My first SWT

Source: Internet
Author: User

In fact, due to the powerful eclipse, many plug-ins can be used to create a SWT, and the use of these plug-ins will make development more and more simple. For example, the designer plug-in is a very useful plug-in, he makes windows development as simple as net development. We just use drag to make a beautiful interface. Our job only takes time to consider the business logic, but this drag does not make us understand what is SWT development, and how we implement SWT. So I wrote a jar package that only uses the SWT package that comes with Eclipse, without using plug-ins for a simple windows program.
Like other Java programs, we first set up the lib and SRC folders to put our library files and source files respectively, and put them in our library files, these are necessary library files for SWT development.
Then we create a form class file myshell. In fact, to create a window in SWT, we must use at least two classes: Display and shell. Display is responsible for processing the interaction between all SWT widgets and the operating system. For example, we can use the display method to query the operating system: Which control gets the focus, and which windows are currently opened. Simply put, the function is to manage windows. Unlike display, shell instances provide a truly visible window, which is generally used as the main window. We can place other widgets here. Display is actually a form, but it only exists in the background. To make it visible and visualized, we must have a presentation layer shell. After coding, it will look like the following:
Package SRC;

/* Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. Button; * // The following. * contains all the space classes above, but we can also write
Import org. Eclipse. SWT. Widgets .*;

Public class myshell {

 
Myshell (){

Display display = New Display ();

Shell shell = new shell (Display );

Button button = new button (shell, 0 );
Text text = new text (shell, 0 );


Shell. setsize (600,600); // form size
Shell. settext ("People's Republic of China"); // form tag
 

Text. setbounds (, 30 );
Text. settext ("I am a Chinese ");

Button. setbounds (300,300, 50, 50 );
Button. settext ("China ");


Shell. open ();

While (! Shell. isdisposed () {// similar to a message loop

If (! Display. readanddispatch ())

Display. Sleep ();

}

Display. Dispose ();

}

}

In this way, we will draw a form, and then we will create a main entry function myrunner to display:
Package SRC;

/* Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. Shell ;*/
Import org. Eclipse. SWT. Widgets .*;

Public class myrunner {

/**
* @ Param ARGs
* @ Param myshell
*/
Public static void main (string [] ARGs ){
Myshell = new myshell ();

}

}
After running the preceding code, a form with a button and a text box is displayed.
To implement click events, We can insert the following code in the shell constructor:
Button. addmouselistener (New mouseadapter (){
Public void mousedown (mouseevent e ){
// The information is displayed when you click the button.
 
Text. settext ("I am a good Chinese ");
Shell2.open ();
}
});
Then, when we click the button of the first form, a new form will pop up, and the text box content of the far form will also change.
The complete shell code is as follows, and the main function code remains unchanged:
Package SRC;

/* Import org. Eclipse. SWT. Widgets. display;
Import org. Eclipse. SWT. Widgets. shell;
Import org. Eclipse. SWT. Widgets. Button ;*/
Import org. Eclipse. SWT .*;
Import org. Eclipse. SWT. Widgets .*;
Import org. Eclipse. SWT. Events .*;

Public class myshell {

 
Myshell (){

Display display = New Display ();

Shell shell = new shell (Display );
Final shell shell2 = new shell (Display );
Button button = new button (shell, 0 );
Final text = new text (shell, 0 );


Shell. setsize (600,600 );
Shell. settext ("People's Republic of China ");
Shell2.setsize (300,300 );
Shell2.settext ("Guangxi ");

Text. setbounds (, 30 );
Text. settext ("I am a Chinese ");

Button. setbounds (300,300, 50, 50 );
Button. settext ("China ");

Shell. open ();
Button. addmouselistener (New mouseadapter (){
Public void mousedown (mouseevent e ){
// The information is displayed when you click the button.
 
Text. settext ("I am a good Chinese ");
Shell2.open ();
}
});


While (! Shell. isdisposed ()){

If (! Display. readanddispatch ())

Display. Sleep ();

}

Display. Dispose ();

}

}

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.