Java/SWT JFace Eclipse/Slider

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

Demonstrates Sliders

//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.layout.*;
import org.eclipse.swt.widgets.*;
/**
 * This class demonstrates Sliders
 */
public class SliderExample {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    // Create a horizontal slider, accepting the defaults
    new Slider(shell, SWT.HORIZONTAL);
    
    // Create a vertical slider and set its properties
    Slider slider = new Slider(shell, SWT.VERTICAL);
    slider.setMinimum(0);
    slider.setMaximum(100);
    slider.setIncrement(5);
    slider.setPageIncrement(20);
    slider.setSelection(75);
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Sample Slider

/******************************************************************************
 * All Right Reserved. 
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * 
 * Created on 2004-3-28 20:25:15 by JACK
 * $Id$
 * 
 *****************************************************************************/

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
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;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
public class SampleSlider {
  Display display = new Display();
  Shell shell = new Shell(display);
  Slider slider;
  Text value;
  
  public SampleSlider() {
    shell.setLayout(new GridLayout(1, true));
    
    Label label = new Label(shell, SWT.NULL);
    label.setText("Volume:");
    
    slider = new Slider(shell, SWT.VERTICAL);
    slider.setBounds(0, 0, 40, 200);
    slider.setMaximum(24);
    slider.setMinimum(0);
    slider.setIncrement(1);
    slider.setPageIncrement(5);
    
    slider.setThumb(4);
    
    slider.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        int perspectiveValue = slider.getMaximum() - slider.getSelection() + slider.getMinimum() - slider.getThumb();
        value.setText("Vol: " + perspectiveValue);
      }
    });
    
    value = new Text(shell, SWT.BORDER | SWT.SINGLE);
    value.setEditable(false);
    slider.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
    value.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_CENTER));
    shell.pack();
    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();
  }
  public static void main(String[] args) {
    new SampleSlider();
  }
}





Slider Example

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
public class SliderExample {
  Display d;
  Shell s;
  SliderExample() {
    d = new Display();
    s = new Shell(d);
    s.setSize(250, 250);
    
    s.setText("A Slider Example");
    final Slider slide = new Slider(s, SWT.HORIZONTAL);
    slide.setBounds(115, 50, 25, 15);
    slide.setMinimum(0);
    slide.setMaximum(100);
    slide.setIncrement(1);
    final Text t = new Text(s, SWT.BORDER);
    t.setBounds(115, 25, 25, 25);
    t.setText("0");
    slide.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        t.setText(new Integer(slide.getSelection()).toString());
      }
    });
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
  public static void main() {
    new SliderExample();
  }
}





SWT Slider: print scroll event details

/*
 * Slider example snippet: print scroll event details
 *
 * 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.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Slider;
public class Snippet17 {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Slider slider = new Slider(shell, SWT.HORIZONTAL);
    slider.setBounds(10, 10, 200, 32);
    slider.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        String string = "SWT.NONE";
        switch (event.detail) {
        case SWT.DRAG:
          string = "SWT.DRAG";
          break;
        case SWT.HOME:
          string = "SWT.HOME";
          break;
        case SWT.END:
          string = "SWT.END";
          break;
        case SWT.ARROW_DOWN:
          string = "SWT.ARROW_DOWN";
          break;
        case SWT.ARROW_UP:
          string = "SWT.ARROW_UP";
          break;
        case SWT.PAGE_DOWN:
          string = "SWT.PAGE_DOWN";
          break;
        case SWT.PAGE_UP:
          string = "SWT.PAGE_UP";
          break;
        }
        System.out.println("Scroll detail -> " + string);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





SWT Sliders

/*
 * Created on Nov 17, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
import org.eclipse.swt.SWT;
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;
import org.eclipse.swt.widgets.Slider;
import org.eclipse.swt.widgets.Text;
/**
 * @author Steven Holzner
 * 
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class SWTSliders {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setText("Sliders");
    shell.setSize(300, 200);
    final Label label = new Label(shell, SWT.NONE);
    label.setText("Move the slider");
    label.setBounds(0, 20, 150, 15);
    final Slider slider = new Slider(shell, SWT.HORIZONTAL);
    slider.setBounds(0, 40, 200, 20);
    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(0, 100, 200, 25);
    slider.addListener(SWT.Selection, new Listener() {
      public void handleEvent(Event event) {
        String outString = "Event: SWT.NONE";
        switch (event.detail) {
        case SWT.ARROW_DOWN:
          outString = "Event: SWT.ARROW_DOWN";
          break;
        case SWT.ARROW_UP:
          outString = "Event: SWT.ARROW_UP";
          break;
        case SWT.DRAG:
          outString = "Event: SWT.DRAG";
          break;
        case SWT.END:
          outString = "Event: SWT.END";
          break;
        case SWT.HOME:
          outString = "Event: SWT.HOME";
          break;
        case SWT.PAGE_DOWN:
          outString = "Event: SWT.PAGE_DOWN";
          break;
        case SWT.PAGE_UP:
          outString = "Event: SWT.PAGE_UP";
          break;
        }
        outString += " Position: " + slider.getSelection();
        text.setText(outString);
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}