Java Tutorial/SWT/Widget

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

Add ControlListener to control

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
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.Shell;
public class ControlListenerAdd {
  static int count = 0;
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new RowLayout());
    final Composite composite = new Composite(shell, SWT.BORDER);
    composite.setLayout(new RowLayout());
    composite.setBackground(display.getSystemColor(SWT.COLOR_YELLOW));
    composite.addControlListener(new ControlListener() {
      public void controlMoved(ControlEvent e) {
      }
      public void controlResized(ControlEvent e) {
        System.out.println("Composite resize.");
      }
    });    
    
    Button buttonAdd = new Button(shell, SWT.PUSH);
    buttonAdd.setText("Add new button");
    buttonAdd.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        Button button = new Button(composite, SWT.PUSH);
        button.setText("Button #" + (count++));
        composite.layout(true);
        composite.pack();
      }
    });
    shell.setSize(450, 100);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Compute Widget Size

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WidgetSizeCompute {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH | SWT.LEFT);
    button.setText("Button");
    System.out.println("Bounds: " + button.getBounds());
    System.out.println("computeSize: " + button.ruputeSize(100, SWT.DEFAULT));
    System.out.println("computeSize: " + button.ruputeSize(40, SWT.DEFAULT));
    System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 100));
    System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 20));
    System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, 15));
    System.out.println("computeSize: " + button.ruputeSize(100, 200));
    System.out.println("computeSize: " + button.ruputeSize(SWT.DEFAULT, SWT.DEFAULT));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Control: set a background image (a dynamic gradient)

/*******************************************************************************
 * Copyright (c) 2000, 2005 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
 *******************************************************************************/
/*
 * Control example snippet: set a background image (a dynamic gradient)
 * 
 * For a list of all SWT example snippets see
 * http://www.eclipse.org/swt/snippets/
 * 
 * @since 3.2
 */
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
public class BackgroundImageControl {
  static Image oldImage;
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
    FillLayout layout1 = new FillLayout(SWT.VERTICAL);
    layout1.marginWidth = layout1.marginHeight = 10;
    shell.setLayout(layout1);
    Group group = new Group(shell, SWT.NONE);
    group.setText("Group ");
    RowLayout layout2 = new RowLayout(SWT.VERTICAL);
    layout2.marginWidth = layout2.marginHeight = layout2.spacing = 10;
    group.setLayout(layout2);
    for (int i = 0; i < 8; i++) {
      Button button = new Button(group, SWT.RADIO);
      button.setText("Button " + i);
    }
    shell.addListener(SWT.Resize, new Listener() {
      public void handleEvent(Event event) {
        Rectangle rect = shell.getClientArea();
        Image newImage = new Image(display, Math.max(1, rect.width), 1);
        GC gc = new GC(newImage);
        gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));
        gc.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
        gc.fillGradientRectangle(rect.x, rect.y, rect.width, 1, false);
        gc.dispose();
        shell.setBackgroundImage(newImage);
        if (oldImage != null)
          oldImage.dispose();
        oldImage = newImage;
      }
    });
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    if (oldImage != null)
      oldImage.dispose();
    display.dispose();
  }
}





Control Size and location

Control is the abstract superclass of all windowed user interface classes.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlSizeLocation {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new FillLayout());
    Button button = new Button(shell, SWT.PUSH);
    shell.setSize(260, 120);
    shell.open();
    System.out.println("------------------------------");
    System.out.println("getBounds: " + button.getBounds());
    System.out.println("getLocation: " + button.getLocation());
    System.out.println("getSize: " + button.getSize());
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Get Preferred size

(From SWT API) The preferred size of a control is the size that it would best be displayed at. The width hint and height hint arguments allow the caller to ask a control questions such as "Given a particular width, how high does the control need to be to show all of the contents?" To indicate that the caller does not wish to constrain a particular dimension, the constant SWT.DEFAULT is passed for the hint.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class GetPreferredSize {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH | SWT.LEFT);
    button.setText("Button");
    System.out.println("toControl: " + button.toControl(100, 200));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Get Widget Style

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class WidgetStyle {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER);
    shell.setLayout(new GridLayout());
    if ((label.getStyle() & SWT.CENTER) != 1) {
      System.out.println("center");
    } else {
      System.out.println("not center");
    }
    shell.setSize(260, 120);
    shell.open();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Inherit a background color or image

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;
public class BackgroundColorImageInherit {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setLayout(new RowLayout(SWT.VERTICAL));
    Color color = display.getSystemColor(SWT.COLOR_CYAN);
    Group group = new Group(shell, SWT.NONE);
    group.setText("SWT.INHERIT_NONE");
    group.setBackground(color);
    group.setBackgroundMode(SWT.INHERIT_NONE);
    shell.pack();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





Relative position to Control

(From ST API) Returns a point which is the result of converting the argument, which is specified in display relative coordinates, to coordinates relative to the receiver.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlRelativePosition {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH | SWT.LEFT);
    button.setText("Button");
    System.out.println("toControl: " + button.toControl(100, 200));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Relative position to Display

Returns a point which is the result of converting the argument, which is specified in coordinates relative to the receiver, to display relative coordinates.



import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class DisplayPositionRelative {
  public static void main(String[] args) {
    final Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout());
    Button button = new Button(shell, SWT.PUSH | SWT.LEFT);
    button.setText("Button");
    System.out.println("toDisplay: " + button.toDisplay(100, 200));
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Set Bounds

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlBounds {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    Button button = new Button(shell, SWT.PUSH);
    button.setBounds(20,20,200,20);
    
    shell.setSize(260, 120);
    shell.open();
    System.out.println("------------------------------");
    System.out.println("getBounds: " + button.getBounds());
    System.out.println("getLocation: " + button.getLocation());
    System.out.println("getSize: " + button.getSize());
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Set Control Size

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlSizeSetting {
  public static void main(String[] args) {
    final Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(150, 150);
    final Cursor[] cursor = new Cursor[1];
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Change cursor");
    Point size = button.ruputeSize(SWT.DEFAULT, SWT.DEFAULT);
    button.setSize(size);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    if (cursor[0] != null)
      cursor[0].dispose();
    display.dispose();
  }
}





Set Location and Size

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class ControlLocationSize {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    
    Button button = new Button(shell, SWT.PUSH);
    button.setLocation(new Point(20,20));
    button.setSize(new Point(200,20));
    
    shell.setSize(260, 120);
    shell.open();
    System.out.println("------------------------------");
    System.out.println("getBounds: " + button.getBounds());
    System.out.println("getLocation: " + button.getLocation());
    System.out.println("getSize: " + button.getSize());
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}





Setting Widget bounds

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WidgetBoundsSetting {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setBounds(10, 10, 200, 200);
    Button button1 = new Button(shell, SWT.PUSH);
    button1.setText("&Typical button");
    button1.setBounds(10, 10, 180, 30);
    Button button2 = new Button(shell, SWT.PUSH);
    button2.setText("&Overidden button");
    button2.setBounds(10, 50, 180, 30);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





The Widget Class

  1. org.eclipse.widgets.Widget class is the top-level class of all the UI components in SWT/JFace.
  2. A Widget can be created and disposed of, and it is capable of issuing notifications to listeners when events occur.
  3. The Constructor: public Widget(Widget parent, int style)
  4. Almost all kinds of UI components in SWT/JFace have similar constructors as this one.


Widget can be disposed: addDisposeListener(DisposeListener listener)

import org.eclipse.swt.events.DisposeEvent;
import org.eclipse.swt.events.DisposeListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class WidgetDisposeListener {
  public static void main(String[] args) {
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.addDisposeListener(new DisposeListener() {
      public void widgetDisposed(DisposeEvent e) {
        System.out.println(e.widget + " disposed");
      }
    });
    shell.setSize(260, 120);
    shell.open();
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
  }
}