Compiling instances on the eclipse interface (2)-Understanding layout 1

Source: Internet
Author: User

The original Article of this document on the eclipse Website: http://www.eclipse.org/articles/Understanding%20Layouts/Understanding%20Layouts.htm

Copyright 2001 Object Technology International, Inc.

Summary

Use

The layout is usually used when SWT is used to write applications.LayoutsTo design beautiful interfaces. A layout is managed inCompositeThe size and position of the sub-component above. The layout class is an abstract class.Layout. This document describes how to use standard layout classes and how to write custom Layout classes.

Original AuthorCarolyn MacLeod, OTIMarch 22,200 1

RevisionShantha Ramachandran, OTIMay 02,200 2

Translation winion 2004.9.19

Section 1 Overview 1.1 layout Overview

When writing an application using the standard part library (SWT), you usually need to use the LayoutLayoutsTo design beautiful interfaces. A layout is managed inCompositeThe size and position of the sub-component above. SWT provides passing Standard Layout classes and can customize layout classes.

In SWT, the position and size of the component are not automatically arranged. Applications can be computed during initialization.CompositeThe size and position of the Child widget can also beResizeListener. If the size of the stator component is not given, the size of the child components is 0 by default, so the page cannot be displayed.

Demonstrate several common concepts in layout. OneComposite(The figure below showsTabfolder) There is a location (Location), Customer zone (Clientarea) And modifier (Trim). The size of composite is the size of the customer zone plus the modification zone. In this exampleCompositeTwo sub-components are concurrently discharged. Layout is used to manage the size and position of child components. It can define the distance between child components (Spacing), The space between the child widget and the boundary (Margin).LayoutAndCompositeThe size of the customer zone is the same.

The preferred size of a component (Preferred size) Is the smallest size required to display large content. For a container, its size is the smallest rectangle that contains all child components. If a child component is placed by an application, the container calculates its preferred size based on the size and location of the Child component. If the container uses a layout class to place child components, it requiresLayoutTo calculate the size of the customer area, and then add the modifier area to obtain its preferred size.

Standard Layout 1.2

The standard layout classes in SWT include:

L filllayout: a component that is arranged in single or single rows of the same size;

L rowlayout: arranges components using fill, wrap, spacing, and other options in the form of one or more rows;

L gridlayout: arrange components in the grid;

L formlayout (new feature of SWT 2.0): arranges components by creating "accessories" for each side of the component.

To use the standard layout class, you need to introduce the SWT layout class package:

Import org. Eclipse. SWT. layout .*;

The layout is pluggable. To setCompositeComponent layout, useSetlayout (layout)Method. The Code demonstrates thatShell(CompositeSub-classes) UseRowlayoutTo deploy its child components.

Shell shell = new shell ();

Shell. setlayout (New rowlayout ());

A Layout class can have a corresponding layout data class (ObjectSub-control), you can set a specific layout for the specified sub-control. For ease of use, the layout data class name is obtained by replacing layout of the layout class with data. For example, Standard LayoutRowlayoutThe layout data class of isRowdata,GridlayouttThe layout data class of isGriddata,FormlayoutThe layout data class of isFormdata. The following code sets the layout data of a component:

Button button = new button (shell, SWT. Push );

Button. setlayoutdata (New rowdata (50, 40 ));

1.3 examples used in this document

Most screens in this document run the following sample code based on different variables. You can change the layout type, parameters, and types and quantity of child components:

Import org. Eclipse. SWT .*;

Import org. Eclipse. SWT. Widgets .*;

Import org. Eclipse. SWT. layout .*;

Public class layoutexample {

Public static void main (string [] ARGs ){

Display display = New Display ();

Shell shell = new shell (Display );

// Create a layout

Rowlayout layout = new rowlayout ();

// Set Optional options

Layout. Wrap = true;

// Set it to the container

Shell. setlayout (layout );

// Create a sub-component of the container

New button (shell, SWT. Push). settext ("B1 ");

New button (shell, SWT. Push). settext ("wide button 2 ");

New button (shell, SWT. Push). settext ("button 3 ");

Shell. Pack ();

Shell. open ();

While (! Shell. isdisposed ()){

If (! Display. readanddispatch () display. Sleep ();

}

}

}

Run the above Code and get:

If you adjust the interface above so that button 3 cannot be placed on the rightRowlayoutLines are broken, and button 3 is displayed in the next line,

The layout is greatly restricted by the size adjustment. Therefore, most examples in this document demonstrate how the layout works if the container becomes larger or smaller.

 

(To be continued)

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.