SWT (2)

xiaoxiao2021-03-25  209

The face lets us show a more complex program than the above example. This program has a text box and a button. When the user clicks the button, the text box displays a welcome information.

For text boxes and buttons have a relatively reasonable size and layout, a gradlayout layout is used. This layout is the most commonly used in SWT and the most powerful layout, almost all formats may be reached through gradlayout. The following procedure also involves how to apply system resources (color), and how to release system resources.

PRIVATE VOID INITSHELL (shell shell) {

/ / Set the layout object for the shell

GridLayout gshelllay = new gridlayout ();

Shell.setLayout (gshelllay);

/ / Construct a container for a Composite component as a text box and a button

Composite Panel = New Composite (shell, swt.none);

/ / Specify a layout structure object for Panel. Here, Panel as much as possible, is the space of all application windows.

Griddata gpaneldata = new

GridData (GridData.grab_horizontal | griddata.grab_vertage | griddata.fill_both;

Panel.setLayOutdata (gpaneldata);

/ / Set a layout object for Panel. Text boxes and buttons will be displayed in this layout object.

GridLayout gpanellay = new gridlayout ();

Panel.setLayout (gpanellay);

/ / Generate a background color for Panel

Final Color Bkcolor = New Color (Display.getCurrent (), 200, 0,200);

Panel.setBackground; BKColor

/ / Generate text box

Final text text = new text (panel, swt.multi | swt.wrap);

/ / Specify a layout structure object for the text box, where the text box is as much as possible with the space of Panel.

Griddata gtextdata = new

GridData (GridData.grab_horizontal | griddata.grab_vertage | griddata.fill_both;

Text.setLayOutdata (gtextdata);

/ / Build button

Button Butt = New Button (Panel, SWT.PUSH);

Butt.Settext ("Push");

/ / Specify the mouse event for the button

Butt.addmouselistener (new mouseadapter () {

Public void mousedown (MouseEvent E) {

// Show information when the user clicks the button

Text.Settext ("Hello Swt");

}

});

// The Disposelistener is triggered when the main window is turned off. Here is used to release the background color of the Panel.

Shell.adddisposelistener (new disposelistener () {

Public void widgetdisposed (DisposeEvent E) {

Bkcolor.dispose ();

}

});

}

Join the method INITSHELL () in this code into the first Open an empty window, get a full GUI application that can run successfully. The operation method can refer to the first example.

System resource management

Developing programs in a graphical operating system to call resources in the system, such as pictures, fonts, colors, and more. Usually these resources are limited, the programmer must be very careful to use these resources: When you use them, please release it as soon as possible, otherwise the operating system will sooner will have the oil, and have to restart, more serious will result in system breakdown. SWT is developed by Java. The great advantage of Java language itself is JVM's "garbage recycling mechanism", and programmers usually do not have to pay the release of variables, memory recycling. Then, for SWT, is the operation of system resources? The answer is a bad news, a good news.

The bad news is that SWT does not use JVM's garbage collection mechanism to deal with the resource recovery problem of the operating system. A key factor is because JVM's garbage collection mechanism is uncontrollable, that is, the programmer can't know, it is impossible to do it. Let JVM recycle resources at a moment! This is fatal on system resources. Imagine your program hopes to see tens of thousands of pictures in a loop statement, the regular processing method is to release one, then view, then release the image resource, then Turn the next picture in the loop, this is for the operating system, and only the program is only a resource of one image. But if this process is completely handed over to JVM dealing, it may be after the end of the loop statement, the JVM will release the image resource, and the result may be that your program has not yet run, the operating system is over.

But the following good news may make this bad news becomes irrelevant. For SWT, just understand two simple "gold" rules, you can use system resources! The reason is called the gold rule, one is because few, only two, the second is because they are surprisingly simple. The first one is "who occupies, who releases", the second is "the parent member is destroyed, the sub-component is also destroyed". The first principle is a principle without any exception, as long as the program calls the constructor of the system resource class, the program should care about this system resource at a moment. For example, call

Font Font = New Font (Display, "Courier", 10, SWT.NORMAL

Then you should call when you need this Font.

Font.dispose ();

For the second principle, it means that if the program calls the Dispose () method of a component, the sub-component of all this component is also destroyed by the DISPOSE () method. The relationship between the sub-member and the parent member referred to herein is formed when the constructor of the calling member is formed. such as,

Shell shell = new shell ();

Composite Parent = New Composite (shell, swt.null);

Compositive child = new Composite (parent, swt.null);

The parent of which Parent is the shell, and the shell is the main window of the program, so there is no corresponding parent member, and the Parent includes the Child sub-component. If you call the shell.dispose () method, apply the second rule, then the Dispose () method of the PARENT and Child components will be automatically called by the SWT API, and they also destroy.

For these two rules, the articles in Reference 2 have a more in-depth introduction.

转载请注明原文地址:https://www.9cbs.com/read-130453.html

New Post(0)