Java Tutorial/SWT/StackLayout

Материал из Java эксперт
Перейти к: навигация, поиск

Configure the margin: marginHeight and marginWidth

/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Jan 31, 2004 1:05:58 AM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.ru/swt
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StackLayoutControlMargin {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final StackLayout stackLayout = new StackLayout();
    stackLayout.marginHeight=20;
    stackLayout.marginWidth=20;
    shell.setLayout(stackLayout);
    final Button[] buttons = new Button[3];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new Button(shell, SWT.NULL);
      buttons[i].setText("Button #" + i);
      buttons[i].addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
          // Flip to next button.
          Button nextButton = null;
          for (int i = 0; i < buttons.length; i++) {
            if (buttons[i] == e.widget) {
              if (i == buttons.length - 1)
                nextButton = buttons[0];
              else
                nextButton = buttons[i + 1];
            }
          }
          stackLayout.topControl = nextButton;
          shell.layout();
        }
        public void widgetDefaultSelected(SelectionEvent e) {
        }
      });
    }
    stackLayout.topControl = buttons[0];
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Use a StackLayout to switch between Composites

/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
//package org.eclipse.swt.snippets;
/*
 * StackLayout example snippet: use a StackLayout to switch between Composites.
 *
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class StackLayoutSwitchComposites {
  static int pageNum = -1;
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 300, 200);
    // create the composite that the pages will share
    final Composite contentPanel = new Composite(shell, SWT.BORDER);
    contentPanel.setBounds(100, 10, 190, 90);
    final StackLayout layout = new StackLayout();
    contentPanel.setLayout(layout);
    // create the first page"s content
    final Composite page0 = new Composite(contentPanel, SWT.NONE);
    page0.setLayout(new RowLayout());
    Label label = new Label(page0, SWT.NONE);
    label.setText("Label on page 1");
    label.pack();
    // create the second page"s content
    final Composite page1 = new Composite(contentPanel, SWT.NONE);
    page1.setLayout(new RowLayout());
    Button button = new Button(page1, SWT.NONE);
    button.setText("Button on page 2");
    button.pack();
    // create the button that will switch between the pages
    Button pageButton = new Button(shell, SWT.PUSH);
    pageButton.setText("Push");
    pageButton.setBounds(10, 10, 80, 25);
    pageButton.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        pageNum = ++pageNum % 2;
        layout.topControl = pageNum == 0 ? page0 : page1;
        contentPanel.layout();
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Using StackLayout

  1. All controls are the same size and are put in the same location.
  2. They"re all stacked atop each other.
  3. only the topmost control is visible.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StackLayoutTest {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    StackLayout layout = new StackLayout();
    shell.setLayout(layout);
    StackLayoutSelectionAdapter adapter = new StackLayoutSelectionAdapter(shell, layout);
    Button one = new Button(shell, SWT.PUSH);
    one.setText("one");
    one.addSelectionListener(adapter);
    Button two = new Button(shell, SWT.PUSH);
    two.setText("two");
    two.addSelectionListener(adapter);
    Button three = new Button(shell, SWT.PUSH);
    three.setText("three");
    three.addSelectionListener(adapter);
    layout.topControl = one;
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
class StackLayoutSelectionAdapter extends SelectionAdapter {
  Shell shell;
  StackLayout layout;
  public StackLayoutSelectionAdapter(Shell shell, StackLayout layout) {
    this.shell = shell;
    this.layout = layout;
  }
  public void widgetSelected(SelectionEvent event) {
    Control control = layout.topControl;
    Control[] children = shell.getChildren();
    int i = 0;
    for (int n = children.length; i < n; i++) {
      Control child = children[i];
      if (child == control) {
        break;
      }
    }
    i = i+1;
    if (i >= children.length)
      i = 0;
    layout.topControl = children[i];
    shell.layout();
  }
}





Using StackLayouts

To set a particular control on the top, you should first set the topControl field of the StackLayout with that control and then call the layout method of the parent composite.



import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class StackLayoutButtons {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final StackLayout stackLayout = new StackLayout();
    shell.setLayout(stackLayout);
    final Button[] buttons = new Button[3];
    for (int i = 0; i < buttons.length; i++) {
      buttons[i] = new Button(shell, SWT.NULL);
      buttons[i].setText("Button #" + i);
      buttons[i].addSelectionListener(new SelectionListener() {
        public void widgetSelected(SelectionEvent e) {
          // Flip to next button.
          Button nextButton = null;
          for (int i = 0; i < buttons.length; i++) {
            if (buttons[i] == e.widget) {
              if (i == buttons.length - 1)
                nextButton = buttons[0];
              else
                nextButton = buttons[i + 1];
            }
          }
          stackLayout.topControl = nextButton;
          shell.layout();
        }
        public void widgetDefaultSelected(SelectionEvent e) {
        }
      });
    }
    stackLayout.topControl = buttons[0];
    shell.setSize(450, 400);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}