Java Tutorial/SWT/TabItem

Материал из Java эксперт
Версия от 15:22, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Add Controls to TabItem

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabItemWithControls {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 6; i++) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText("TabItem " + i);
      Button button = new Button(tabFolder, SWT.PUSH);
      button.setText("Page " + i);
      item.setControl(button);
    }
    tabFolder.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Create a tab with a label, an image, a tool tip, and a control

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabItemWithToolTipImage {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 6; i++) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText("TabItem " + i);
      item.setToolTipText("This is my tab" + i);
      item.setImage(new Image(display, "yourFile.gif"));
      Button button = new Button(tabFolder, SWT.PUSH);
      button.setText("Page " + i);
      item.setControl(button);
    }
    tabFolder.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Create tabs that run along the top or the bottom of the parent composite

Pass SWT.TOP (the default) or SWT.BOTTOM to TabFolder"s constructor to create tabs that run along the top or the bottom of the parent composite, respectively.



import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabItemAddPushButton {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER|SWT.BOTTOM);
    for (int i = 0; i < 6; i++) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText("TabItem " + i);
      Button button = new Button(tabFolder, SWT.PUSH);
      button.setText("Page " + i);
      item.setControl(button);
    }
    tabFolder.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Pass the Composite to the tab"s setControl() method

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.ruposite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabFolder;
import org.eclipse.swt.widgets.TabItem;
public class TabItemWithComposite {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    final TabFolder tabFolder = new TabFolder(shell, SWT.BORDER);
    for (int i = 0; i < 6; i++) {
      TabItem item = new TabItem(tabFolder, SWT.NONE);
      item.setText("TabItem " + i);
      item.setToolTipText("This is my tab" + i);
      item.setImage(new Image(display, "yourFile.gif"));
      
      Composite composite = new Composite(tabFolder, SWT.NONE);
      composite.setLayout(new FillLayout());
      new Button(composite, SWT.PUSH).setText("Button One");
      new Button(composite, SWT.PUSH).setText("Button Two");
      new Button(composite, SWT.PUSH).setText("Button Three");
      item.setControl(composite);
    }
    tabFolder.pack();
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}