[Plug-in development] - 4 SWT Programming Notes

  According to the previous two blog posts, you should understand plug-in development.

The foregoing review:

plug-learning articles

simple plug-established engineering and analysis model file

using the extension points, development perspective

  SWT knowledge introduction

  Java had previously learned friend, how many pages will be something on the Swing. So here is the Eclipse SWT plug-in application to the widget development framework.

  Which contains a large number of desktop control, and a series of optimization and integration, with respect Swing, greatly reduces memory consumption. And with regard to the release of resources also requires developers to pay attention to the need to manually delete specific, but a part of such sub-components will be destroyed along with the part being destroyed.

  Here's a look at some of the components used to develop the model presented here is not all, there are many, many widgets in fact, here are several brief:

  

  Here Widget is a superclass, and all components inherit this class. It also provides some common methods, such as adding a number of listeners, obtain information, and so commonly used.

  The most common but also a number of Control, because a lot of Button Label controls are to extend this class, method frequently used in development is

  addMouseListener () listens mouse click

  setBounds re-draw controls

  and many more. Specific function, we can develop more than look at, on the line.

 

  SWT on the relationship between the inside and Display Shell

  Most of Eclipse plug-in development of the program has an unwritten rule, during a program event, only one Dispaly object, but can have multiple Shell object. So, what is Dispaly, what is it Shell.

  

 

  Here it is shown a red arrow Display, which is an instance of the underlying application. If this instance is not destroyed, and the program unexpectedly stopped, it can not be re-run. In other words, during operation, an application can only have one Display. Like the contents of the window displays, only one display, but the internal monitor can display the contents of multiple files.

  Shell corresponding green arrow is, a window corresponding to an activity of Shell, may be added various small components which form a rich application interface.

  In summary, a Display can have more than Shell, but only one Display (applicable to ordinary circumstances).!

 

  Start In Main interface development

  Next, tell us about how not to start an Eclipse plug-in project to develop SWT. This process has many of the materials described herein thus provide only the code corresponding to the above examples.

  Note that, the last to be released resource, Shell is mounted to Dispaly above (pardon the word I use to mount, Linux inside mount more vivid), so when destruction Display, you can automatically destroy the Shell object. But Color is not mounted by way of creation, and therefore a separate release.

 1 package com.xingoo.plugin.swttest;
 2 
 3 import javax.swing.Scrollable;
 4 import javax.swing.text.StyleConstants.ColorConstants;
 5 
 6 import org.eclipse.swt.SWT;
 7 import org.eclipse.swt.graphics.Color;
 8 import org.eclipse.swt.layout.FillLayout;
 9 import org.eclipse.swt.widgets.Display;
10 import org.eclipse.swt.widgets.Label;
11 import org.eclipse.swt.widgets.Shell;
12 import org.eclipse.swt.widgets.Text;
13 
14 public class mainTestExample {
15     public static void main(String[] args) {
16         Display display = new Display();
17         Color color =  new Color(display,255,0,0);
18         
19         //create a shell
20         Shell shell_1 = new Shell(display);
21         shell_1.setText("This is a shell in main function()");
22         shell_1.setBounds(100,100,400,200);
23         shell_1.setLayout(new FillLayout());
24         
25         Label label_1 = new Label(shell_1,SWT.CENTER);
26         label_1.setText("this is the text of a label");
27         label_1.setForeground(color);
28         
29         shell_1.open();
30         Text test;
31         //create another shell
32         Shell shell_2 = new Shell(display);
33         shell_2.setText("This is a shell1 in main function()");
34         shell_2.setBounds(250,250,400,200);
35         shell_2.setLayout(new FillLayout());
36         
37         Label label_2 = new Label(shell_2,SWT.CENTER);
38         label_2.setText("this is the text of a label1");
39         label_2.setForeground(color);
40         
41         shell_2.open();
42         
43         while(!shell_1.isDisposed() || !shell_2.isDisposed()){
44             if(!display.readAndDispatch())
45                 display.sleep();
46         }
47         
48         //dispose the resource
49         display.beep();
50         color.dispose();
51         display.dispose();
52     }
53 }

  This function code can run in general engineering inside, but the lack of a Jar package, swt jar package, this jar package can be found under the Eclipse plugins folder. It can be introduced by way of introduction to the project.

  In fact, only swtx86 this jar package on it, source is the source code that allows me to track the source debugger swt.

Abstract class inherits facilitate window

  In order to test the use of the latter, this code can be extracted. The main function of the class after so long inherit this AbstractExample can edit the window.

 1 package com.xingoo.plugin.swttest;
 2 
 3 import org.eclipse.swt.SWT;
 4 import org.eclipse.swt.layout.FillLayout;
 5 import org.eclipse.swt.widgets.Display;
 6 import org.eclipse.swt.widgets.Label;
 7 import org.eclipse.swt.widgets.Shell;
 8 
 9 abstract class AbstractExample{
10     public void run(){
11         Display display = new Display();
12         Shell shell = new Shell(display);
13         shell.setText("shell example");
14         shell.setBounds(100,100,400,200);
15         shell.setLayout(new FillLayout());
16         todo(shell);
17         shell.open();
18         
19         while(!shell.isDisposed()){
20             if(!display.readAndDispatch())
21                 display.sleep();
22         }
23         //dispose the resource
24         display.beep();
25         display.dispose();
26     }
27     public abstract void todo(Shell shell);//extension something here
28 }
29 
30 public class mainTestExample extends AbstractExample{
31     public static void main(String[] args) {
32         new mainTestExample().run();
33     }
34 
35     public void todo(Shell shell) {
36         //...add something you like
37         Label label_1 = new Label(shell,SWT.CENTER);
38         label_1.setText("this is the text of a label");
39     }
40 }

 

  

Reproduced in: https: //my.oschina.net/u/204616/blog/545337

Guess you like

Origin blog.csdn.net/weixin_34162401/article/details/91989671