关闭

stackLayout

标签: stringlayoutbuttonswteclipseshell
816人阅读 评论(0) 收藏 举报

StackLayout stackLayout = new StackLayout();
Composite clabels = createComposite(body, SWT.BORDER,
                                    stackLayout);
Label[] labels = new Label[5];
:
for (int i = 0; i < labels.length; i++) {
    Label xlabel = new Label(clabels, SWT.CENTER);
    xlabel.setText("Stack " + i);
    labels[i] = xlabel;
}
stackLayout.topControl = labels[0];
:
protected Composite createComposite(Composite parent,
                                    int style,
                                    Layout layout) {
    Composite c =  new Composite(parent, style);
    if (layout != null) {
        c.setLayout(layout);
    }
    return c;
}
protected Composite createComposite(Composite parent,
                                    Layout layout) {
    return createComposite(parent, SWT.NONE, layout);
}

////////////////////////////

another example

//////////////////

/*******************************************************************************
* Copyright (c) 2000, 2003 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
*     IBM Corporation - initial API and implementation
*******************************************************************************/

package org.eclipse.swt.custom;


import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

/**
* This Layout stacks all the controls one on top of the other and resizes all controls
* to have the same size and location.
* The control specified in topControl is visible and all other controls are not visible.
* Users must set the topControl value to flip between the visible items and then call
* layout() on the composite which has the StackLayout.
*
* <p> Here is an example which places ten buttons in a stack layout and
* flips between them:
*
* <pre><code>
* public static void main(String[] args) {
* Display display = new Display();
* Shell shell = new Shell(display);
* shell.setLayout(new GridLayout());
*
* final Composite parent = new Composite(shell, SWT.NONE);
* parent.setLayoutData(new GridData(GridData.FILL_BOTH));
* final StackLayout layout = new StackLayout();
* parent.setLayout(layout);
* final Button[] bArray = new Button[10];
* for (int i = 0; i &lt; 10; i++) {
* bArray[i] = new Button(parent, SWT.PUSH);
* bArray[i].setText("Button "+i);
* }
* layout.topControl = bArray[0];
*
* Button b = new Button(shell, SWT.PUSH);
* b.setText("Show Next Button");
* final int[] index = new int[1];
* b.addListener(SWT.Selection, new Listener(){
* public void handleEvent(Event e) {
* index[0] = (index[0] + 1) % 10;
* layout.topControl = bArray[index[0]];
* parent.layout();
* }
* });
*
* shell.open();
* while (shell != null && !shell.isDisposed()) {
* if (!display.readAndDispatch())
* display.sleep();
* }
* }
* </code></pre>
*/


public class StackLayout extends Layout {

/**
* marginWidth specifies the number of pixels of horizontal margin
* that will be placed along the left and right edges of the layout.
*
* The default value is 0.
*/

public int marginWidth = 0;
/**
* marginHeight specifies the number of pixels of vertical margin
* that will be placed along the top and bottom edges of the layout.
*
* The default value is 0.
*/

public int marginHeight = 0;

/**
* topControl the Control that is displayed at the top of the stack.
* All other controls that are children of the parent composite will not be visible.
*/

public Control topControl;

protected Point computeSize(Composite composite, int wHint, int hHint, boolean flushCache) {
Control children[] = composite.getChildren();

int maxWidth = 0;
int maxHeight = 0;
for (int i = 0; i < children.length; i++) {
Point size = children[i].computeSize(wHint, hHint, flushCache);
maxWidth = Math.max(size.x, maxWidth);
maxHeight = Math.max(size.y, maxHeight);
}

int width = wHint, height = hHint;
if (wHint == SWT.DEFAULT) width = maxWidth;
if (hHint == SWT.DEFAULT) height = maxHeight;
return new Point(width + 2 * marginWidth, height + 2 * marginHeight);
}

protected void layout(Composite composite, boolean flushCache) {
Control children[] = composite.getChildren();
Rectangle rect = composite.getClientArea();
rect.x += marginWidth;
rect.y += marginHeight;
rect.width -= 2 * marginWidth;
rect.height -= 2 * marginHeight;
for (int i = 0; i < children.length; i++) {
children[i].setBounds(rect);
children[i].setVisible(children[i] == topControl);

}
}
String getName () {
String string = getClass ().getName ();
int index = string.lastIndexOf ('.');
if (index == -1) return string;
return string.substring (index + 1, string.length ());
}
public String toString () {
String string = getName ()+" {";
if (marginWidth != 0) string += "marginWidth="+marginWidth+" ";
if (marginHeight != 0) string += "marginHeight="+marginHeight+" ";
if (topControl != null) string += "topControl="+topControl+" ";
string = string.trim();
string += "}";
return string;
}
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10138次
    • 积分:165
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章存档
    阅读排行
    最新评论
  • SWT(2)

    MangDong: 不知CSDN的评论发布系统对于评论大小有多少限制,我发的评论竟然只有开头部分,唉真是遗憾。算了。我发...

  • SWT(2)

    MangDong: SWT程序的组件构造惯用法跟Swing程序很不一样。Swing程序是第一次使用构造法,跟单态模式很想...