创建ViewForm : ViewForm « SWT « Java 教程

En
Java 教程
1. 语言基础
2. 数据类型
3. 操作符
4. 流程控制
5. 类定义
6. 开发相关
7. 反射
8. 正则表达式
9. 集合
10. 线
11. 文件
12. 泛型
13. 本土化
14. Swing
15. Swing事件
16. 二维图形
17. SWT
18. SWT 二维图形
19. 网络
20. 数据库
21. Hibernate
22. JPA
23. JSP
24. JSTL
25. Servlet
26. Web服务SOA
27. EJB3
28. Spring
29. PDF
30. 电子邮件
31. 基于J2ME
32. J2EE应用
33. XML
34. 设计模式
35. 日志
36. 安全
37. Apache工具
38. 蚂蚁编译
39. JUnit单元测试
Java
Java 教程 » SWT » ViewForm 
17. 87. 1. 创建ViewForm
  1. ViewForm creates three controls in a row across the top of a Composite, with a content area below the controls.
  2. The first control contains the image in the upper left and the text Outline.
  3. The second control contains the toolbar buttons to the right of the text Outline, up to, but not including, the close button in the upper right.
  4. The third control is the close button in the upper right.
  5. Everything else is the content area.

You create a ViewForm by calling its only constructor:

ViewForm(Composite parent, int style)

The applicable styles for ViewForm are

  1. SWT.BORDER, which draws a visible border and a drop shadow around the ViewForm, and
  2. SWT.FLAT, which eliminates the drop shadow.
  3. SWT.FLAT must be combined with SWT.BORDER (using the bitwise OR operator) to have any effect.
创建ViewForm
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.custom.ViewForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ViewFormCreate {
  private static int count = 0;

  static Display display = new Display();

  private static void createViewFormHelper(final Composite parent, String text) {
    final ViewForm vf = new ViewForm(parent, SWT.BORDER);

    CLabel label = new CLabel(vf, SWT.NONE);
    label.setText(text);
    label.setImage(new Image(display, "yourFile.gif"));
    label.setAlignment(SWT.LEFT);
    vf.setTopLeft(label);

    final ToolBar tbMenu = new ToolBar(vf, SWT.FLAT);
    final ToolItem itemMenu = new ToolItem(tbMenu, SWT.PUSH);
    vf.setTopCenter(tbMenu);

    ToolBar tbClose = new ToolBar(vf, SWT.FLAT);
    ToolItem itemClose = new ToolItem(tbClose, SWT.PUSH);
    itemClose.setImage(new Image(display, "yourFile.gif"));
    itemClose.setText("X");
    itemClose.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        vf.dispose();
        parent.layout();
      }
    });
    vf.setTopRight(tbClose);

    final Text textArea = new Text(vf, SWT.MULTI | SWT.WRAP | SWT.V_SCROLL);
    vf.setContent(textArea);

    final Menu menu = new Menu(tbMenu);
    MenuItem clear = new MenuItem(menu, SWT.NONE);
    clear.setText("Clear");
    clear.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        textArea.setText("");
      }
    });

    itemMenu.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        Rectangle rect = itemMenu.getBounds();
        menu.setLocation(tbMenu.toDisplay(rect.x, rect.y + rect.height));
        menu.setVisible(true);
      }
    });
  }

  public static void main(String[] args) {

    Shell shell = new Shell(display);
    shell.setText("Look");

    shell.setLayout(new GridLayout(1false));

    Button button = new Button(shell, SWT.PUSH);
    button.setText("New Document");

    final Composite composite = new Composite(shell, SWT.NONE);
    composite.setLayoutData(new GridData(GridData.FILL_BOTH));
    composite.setLayout(new FillLayout());

    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        createViewFormHelper(composite, "Document " (++count));
        composite.layout();
      }
    });

    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}
17. 87. ViewForm
17. 87. 1. 创建ViewForm创建ViewForm
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.