Java by API/org.eclipse.swt.widgets/Shell — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:43, 31 мая 2010

new Shell(Display arg0)

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(500, 500);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new Shell(Display display, int style)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
      Display d = new Display();
      Shell s = new Shell(d, SWT.CLOSE | SWT.RESIZE);
      s.setSize(300,300);
      s.open();
      while(!s.isDisposed()){
          if(!d.readAndDispatch())
              d.sleep();
      }
      d.dispose();
    }
}





new Shell(Shell parent)

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(500,500);
    s.open();
    ChildShell cs = new ChildShell(s);
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}
class ChildShell {
  
  ChildShell(Shell parent){
      Shell child = new Shell(parent);
      child.setSize(200,200);
      child.open();        
  }
}





new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM) (Make shell a dialog)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Button;
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;

public class MainClass {
  public static void main(String[] a) {
    
    Shell shell = new Shell(new Display());
    final Shell dialog = new Shell(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
    dialog.setText("Delete File");
    dialog.setSize(250, 150);
    final Button buttonOK = new Button(dialog, SWT.PUSH);
    buttonOK.setText("OK");
    buttonOK.setBounds(20, 55, 80, 25);
    Button buttonCancel = new Button(dialog, SWT.PUSH);
    buttonCancel.setText("Cancel");
    buttonCancel.setBounds(120, 55, 80, 25);
    final Label label = new Label(dialog, SWT.NONE);
    label.setText("Delete the file?");
    label.setBounds(20, 15, 100, 20);
    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        if (event.widget == buttonOK) {
          System.out.println("OK");
        } else {
          System.out.println("Cancel");
        }
        dialog.close();
      }
    };
    buttonOK.addListener(SWT.Selection, listener);
    buttonCancel.addListener(SWT.Selection, listener);
    
    dialog.open();
  }
}





Shell: getClientArea()

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    Shell shell = new Shell(display);
    Label label = new Label(shell, SWT.CENTER);
    label.setText("Hello, World");
    label.setBounds(shell.getClientArea());
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Shell: getDisplay()

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FontDialog;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    // Create the main window
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(2, false));
    final Label fontLabel = new Label(shell, SWT.NONE);
    fontLabel.setText("The selected font");
    Button button = new Button(shell, SWT.PUSH);
    button.setText("Font...");
    button.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent event) {
        // Create the color-change dialog
        FontDialog dlg = new FontDialog(shell);
        Font font = null;
        Color color = null;
        if (font != null)
          dlg.setFontList(fontLabel.getFont().getFontData());
        if (color != null)
          dlg.setRGB(color.getRGB());
        if (dlg.open() != null) {
          if (font != null)
            font.dispose();
          if (color != null)
            color.dispose();
          font = new Font(shell.getDisplay(), dlg.getFontList());
          fontLabel.setFont(font);
          color = new Color(shell.getDisplay(), dlg.getRGB());
          fontLabel.setForeground(color);
          shell.pack();
        }
      }
    });
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
}





Shell: open()

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(500,500);
    s.open();
    ChildShell cs = new ChildShell(s);
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}
class ChildShell {
  
  ChildShell(Shell parent){
      Shell child = new Shell(parent);
      child.setSize(200,200);
      child.open();        
  }
}





Shell: setImage(Image img)

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
      Display d = new Display();
      Shell s = new Shell(d);
      s.setSize(500,500);
      s.setImage(new Image(d, "c:\\icons\\yourico.ico"));
      s.setText("A Shell Example");
      s.open();
      while(!s.isDisposed()){
          if(!d.readAndDispatch())
              d.sleep();
      }
      d.dispose();  
    }
}





Shell: setMenuBar(Menu menu)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s,SWT.BAR );
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("Open");
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    
    s.setMenuBar(m);
    s.open();
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}





Shell: setRegion(Region reg)

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Region;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    Region region = new Region();
    region.add(createCircle(50, 50, 50));
    region.subtract(createCircle(50, 50, 20));
    shell.setRegion(region);
    shell.setSize(region.getBounds().width, region.getBounds().height);
    shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        display.sleep();
      }
    }
    display.dispose();
  }
  static int[] createCircle(int xOffset, int yOffset, int radius) {
    int[] circlePoints = new int[10 * radius];
    for (int loopIndex = 0; loopIndex < 2 * radius + 1; loopIndex++) {
      int xCurrent = loopIndex - radius;
      int yCurrent = (int) Math.sqrt(radius * radius - xCurrent * xCurrent);
      int doubleLoopIndex = 2 * loopIndex;
      
      circlePoints[doubleLoopIndex] = xCurrent + xOffset;
      circlePoints[doubleLoopIndex + 1] = yCurrent + yOffset;
      circlePoints[10 * radius - doubleLoopIndex - 2] = xCurrent + xOffset;
      circlePoints[10 * radius - doubleLoopIndex - 1] = -yCurrent + yOffset;
    }
    
    return circlePoints;
  }  
}





Shell: setSize(int width, int height)

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
public class MainClass {

  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setSize(500, 500);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}