Java/SWT JFace Eclipse/SashForm

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

Create a sash form with three children

/*
 * SashForm example snippet: create a sash form with three children
 *
 * For a list of all SWT example snippets see
 * http://dev.eclipse.org/viewcvs/index.cgi/%7Echeckout%7E/platform-swt-home/dev.html#snippets
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
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.Label;
import org.eclipse.swt.widgets.Shell;
public class Snippet109 {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    SashForm form = new SashForm(shell, SWT.HORIZONTAL);
    form.setLayout(new FillLayout());
    Composite child1 = new Composite(form, SWT.NONE);
    child1.setLayout(new FillLayout());
    new Label(child1, SWT.NONE).setText("Label in pane 1");
    Composite child2 = new Composite(form, SWT.NONE);
    child2.setLayout(new FillLayout());
    new Button(child2, SWT.PUSH).setText("Button in pane2");
    Composite child3 = new Composite(form, SWT.NONE);
    child3.setLayout(new FillLayout());
    new Label(child3, SWT.PUSH).setText("Label in pane3");
    form.setWeights(new int[] { 30, 40, 30 });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Demonstrates SashForm

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates SashForm
 */
public class SashFormTest {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("SashForm Test");
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  /**
   * Creates the main window"s contents
   * 
   * @param parent the parent window
   */
  private void createContents(Composite parent) {
    // Fill the parent window with the buttons and sash
    parent.setLayout(new FillLayout());
    // Create the SashForm and the buttons
    SashForm sashForm = new SashForm(parent, SWT.HORIZONTAL);
    new Button(sashForm, SWT.PUSH).setText("Left");
    new Button(sashForm, SWT.PUSH).setText("Right");
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new SashFormTest().run();
  }
}





Demonstrates SashForm Advanced

//Send questions, comments, bug reports, etc. to the authors:
//Rob Warner (rwarner@interspatial.ru)
//Robert Harris (rbrt_harris@yahoo.ru)
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates SashForm
 */
public class SashFormAdvanced {
  /**
   * Runs the application
   */
  public void run() {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("SashForm Advanced");
    createContents(shell);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  /**
   * Creates the main window"s contents
   * 
   * @param parent the parent window
   */
  private void createContents(Composite parent) {
    // Our layout will have a row of buttons, and
    // then a SashForm below it.
    parent.setLayout(new GridLayout(1, false));
    // Create the row of buttons
    Composite buttonBar = new Composite(parent, SWT.NONE);
    buttonBar.setLayout(new RowLayout());
    Button flip = new Button(buttonBar, SWT.PUSH);
    flip.setText("Switch Orientation");
    Button weights = new Button(buttonBar, SWT.PUSH);
    weights.setText("Restore Weights");
    // Create the SashForm
    Composite sash = new Composite(parent, SWT.NONE);
    sash.setLayout(new FillLayout());
    sash.setLayoutData(new GridData(GridData.FILL_BOTH));
    final SashForm sashForm = new SashForm(sash, SWT.HORIZONTAL);
    // Change the width of the sashes
    sashForm.SASH_WIDTH = 20;
    // Change the color used to paint the sashes
    sashForm.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_GREEN));
    // Create the buttons and their event handlers
    final Button one = new Button(sashForm, SWT.PUSH);
    one.setText("One");
    one.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        maximizeHelper(one, sashForm);
      }
    });
    final Button two = new Button(sashForm, SWT.PUSH);
    two.setText("Two");
    two.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        maximizeHelper(two, sashForm);
      }
    });
    final Button three = new Button(sashForm, SWT.PUSH);
    three.setText("Three");
    three.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        maximizeHelper(three, sashForm);
      }
    });
    // Set the relative weights for the buttons
    sashForm.setWeights(new int[] { 1, 2, 3});
    // Add the Switch Orientation functionality
    flip.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        switch (sashForm.getOrientation()) {
        case SWT.HORIZONTAL:
          sashForm.setOrientation(SWT.VERTICAL);
          break;
        case SWT.VERTICAL:
          sashForm.setOrientation(SWT.HORIZONTAL);
          break;
        }
      }
    });
    // Add the Restore Weights functionality
    weights.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        sashForm.setWeights(new int[] { 1, 2, 3});
      }
    });
  }
  /**
   * Helper method for our maximize behavior. If the passed control is already
   * maximized, restore it. Otherwise, maximize it
   * 
   * @param control the control to maximize or restore
   * @param sashForm the parent SashForm
   */
  private void maximizeHelper(Control control, SashForm sashForm) {
    // See if the control is already maximized
    if (control == sashForm.getMaximizedControl()) {
      // Already maximized; restore it
      sashForm.setMaximizedControl(null);
    } else {
      // Not yet maximized, so maximize it
      sashForm.setMaximizedControl(control);
    }
  }
  /**
   * The application entry point
   * 
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    new SashFormAdvanced().run();
  }
}





SashForm Example

/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-4-8 15:34:30 by JACK
 * $Id$
 * 
 *****************************************************************************/
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class SashFormExample {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  SashForm sashForm;
  SashForm sashForm2;
  public SashFormExample() {
    
    shell.setLayout(new FillLayout());
    
    sashForm = new SashForm(shell, SWT.HORIZONTAL);
    
    Text text1 = new Text(sashForm, SWT.CENTER);
    text1.setText("Text in pane #1");
    Text text2 = new Text(sashForm, SWT.CENTER);
    text2.setText("Text in pane #2");
    
    sashForm2 = new SashForm(sashForm, SWT.VERTICAL);
    
    final Label labelA = new Label(sashForm2, SWT.BORDER | SWT.CENTER);
    labelA.setText("Label in pane A");
    final Label labelB = new Label(sashForm2, SWT.BORDER |SWT.CENTER);
    labelB.setText("Label in pane B");
    
    text1.addControlListener(new ControlListener() {
      public void controlMoved(ControlEvent e) {
      }
      public void controlResized(ControlEvent e) {
        System.out.println("Resized");
        
      }
    });
    
    sashForm.setWeights(new int[]{1, 2, 3});
    
    labelA.addMouseListener(new MouseListener() {
      public void mouseDoubleClick(MouseEvent e) {
        if(sashForm2.getMaximizedControl() == labelA)
          sashForm2.setMaximizedControl(null);
        else
          sashForm2.setMaximizedControl(labelA);
      }
      public void mouseDown(MouseEvent e) {
      }
      public void mouseUp(MouseEvent e) {
      }
    });
    
    
    shell.setSize(450, 200);
    shell.open();
    //textUser.forceFocus();
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }
    display.dispose();
  }
  private void init() {
  }
  public static void main(String[] args) {
    new SashFormExample();
  }
}