Java Tutorial/SWT/Shell
Содержание
- 1 Allow escape to close a shell
- 2 Center a shell on the primary monitor
- 3 Create a non-rectangular shell to simulate transparency
- 4 Create a non-rectangular window
- 5 Create GC from Shell and paint on window
- 6 Create non-rectangular shell from an image with transparency
- 7 Creating Shells
- 8 DIALOG_TRIM = (CLOSE | TITLE | BORDER): dialog shells.
- 9 Get Default Button
- 10 Getting Controls on a shell
- 11 ON_TOP: This style bit causes the shell to always float on the top of the desktop.
- 12 Open a shell maximized (full screen)
- 13 Open a shell minimized (iconified)
- 14 Prevent a shell from closing (prompt the user)
- 15 Ring Shell
- 16 Set default Button for Shell
- 17 Set icons with different resolutions
- 18 Set left upper corner image
- 19 Setting the Title
- 20 Shell States
- 21 SHELL_TRIM = (CLOSE | TITLE | MIN | MAX | RESIZE): top-level shells.
- 22 Shell without Title bar
- 23 Shell with SWT.SHELL_TRIM|SWT.TOOL style
- 24 The modal state of shells: APPLICATION_MODAL, MODELESS, PRIMARY_MODAL, SYSTEM_MODAL
- 25 TOOL: the shell is a tool window(floating toolbars)
- 26 Understanding the Shell Object
Allow escape to close a shell
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2006 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 *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Shell example snippet: allow escape to close a shell * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */
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.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class ShellEscapeClose {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); shell.addListener(SWT.Traverse, new Listener() { public void handleEvent(Event event) { switch (event.detail) { case SWT.TRAVERSE_ESCAPE: shell.close(); event.detail = SWT.TRAVERSE_NONE; event.doit = false; break; } } }); Button button = new Button(shell, SWT.PUSH); button.setText("A Button (that doesn"t process Escape)"); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Center a shell on the primary monitor
<source lang="java">
import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Monitor; import org.eclipse.swt.widgets.Shell; public class ShellCenter {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(200, 200); Monitor primary = display.getPrimaryMonitor(); Rectangle bounds = primary.getBounds(); Rectangle rect = shell.getBounds(); int x = bounds.x + (bounds.width - rect.width) / 2; int y = bounds.y + (bounds.height - rect.height) / 2; shell.setLocation(x, y); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Create a non-rectangular shell to simulate transparency
<source lang="java">
/*******************************************************************************
* 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 *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Create a non-rectangular shell to simulate transparency * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.0 */
import org.eclipse.swt.SWT; import org.eclipse.swt.events.PaintEvent; import org.eclipse.swt.events.PaintListener; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.Region; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class NonRectangularTransparency {
public static void main(String[] args) { Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_WARNING); // Shell must be created with style SWT.NO_TRIM final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); shell.setBackground(display.getSystemColor(SWT.COLOR_RED)); // define a region Region region = new Region(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < 200; y += 2) { for (int x = 0; x < 200; x += 2) { pixel.x = x; pixel.y = y; region.add(pixel); } } // define the shape of the shell using setRegion shell.setRegion(region); Rectangle size = region.getBounds(); shell.setSize(size.width, size.height); shell.addPaintListener(new PaintListener() { public void paintControl(PaintEvent e) { Rectangle bounds = image.getBounds(); Point size = shell.getSize(); e.gc.drawImage(image, 0, 0, bounds.width, bounds.height, 10, 10, size.x - 20, size.y - 20); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); display.dispose(); }
}</source>
Create a non-rectangular window
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 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 *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* Shell example snippet: create a non-rectangular window * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.0 */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.Region; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class NonRectangularWindow {
static int[] circle(int r, int offsetX, int offsetY) { int[] polygon = new int[8 * r + 4]; // x^2 + y^2 = r^2 for (int i = 0; i < 2 * r + 1; i++) { int x = i - r; int y = (int) Math.sqrt(r * r - x * x); polygon[2 * i] = offsetX + x; polygon[2 * i + 1] = offsetY + y; polygon[8 * r - 2 * i - 2] = offsetX + x; polygon[8 * r - 2 * i - 1] = offsetY - y; } return polygon; } public static void main(String[] args) { final Display display = new Display(); // Shell must be created with style SWT.NO_TRIM final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); shell.setBackground(display.getSystemColor(SWT.COLOR_RED)); // define a region that looks like a key hole Region region = new Region(); region.add(circle(67, 67, 67)); region.subtract(circle(20, 67, 50)); region.subtract(new int[] { 67, 50, 55, 105, 79, 105 }); // define the shape of the shell using setRegion shell.setRegion(region); Rectangle size = region.getBounds(); shell.setSize(size.width, size.height); // add ability to move shell around Listener l = new Listener() { Point origin; public void handleEvent(Event e) { switch (e.type) { case SWT.MouseDown: origin = new Point(e.x, e.y); break; case SWT.MouseUp: origin = null; break; case SWT.MouseMove: if (origin != null) { Point p = display.map(shell, null, e.x, e.y); shell.setLocation(p.x - origin.x, p.y - origin.y); } break; } } }; shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseUp, l); shell.addListener(SWT.MouseMove, l); // add ability to close shell Button b = new Button(shell, SWT.PUSH); b.setBackground(shell.getBackground()); b.setText("close"); b.pack(); b.setLocation(10, 68); b.addListener(SWT.Selection, new Listener() { public void handleEvent(Event e) { shell.close(); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); display.dispose(); }
}</source>
Create GC from Shell and paint on window
<source lang="java">
import org.eclipse.swt.graphics.GC; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class PaintWindowGCShell {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.open(); GC gc = new GC(shell); gc.setLineWidth(4); gc.drawRectangle(20, 20, 100, 100); gc.dispose(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Create non-rectangular shell from an image with transparency
<source lang="java">
/*******************************************************************************
* 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 *******************************************************************************/
//package org.eclipse.swt.snippets; import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.graphics.Region; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; /**
* Region snippet: Create non-rectangular shell from an image with transparency * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.2 */
public class NonRectangularShellFromTransparencyImage {
public static void main(String[] args) { final Display display = new Display(); final Image image = display.getSystemImage(SWT.ICON_INFORMATION); final Shell shell = new Shell(display, SWT.NO_TRIM); Region region = new Region(); final ImageData imageData = image.getImageData(); if (imageData.alphaData != null) { Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < imageData.height; y++) { for (int x = 0; x < imageData.width; x++) { if (imageData.getAlpha(x, y) == 255) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } else { ImageData mask = imageData.getTransparencyMask(); Rectangle pixel = new Rectangle(0, 0, 1, 1); for (int y = 0; y < mask.height; y++) { for (int x = 0; x < mask.width; x++) { if (mask.getPixel(x, y) != 0) { pixel.x = imageData.x + x; pixel.y = imageData.y + y; region.add(pixel); } } } } shell.setRegion(region); Listener l = new Listener() { int startX, startY; public void handleEvent(Event e) { if (e.type == SWT.KeyDown && e.character == SWT.ESC) { shell.dispose(); } if (e.type == SWT.MouseDown && e.button == 1) { startX = e.x; startY = e.y; } if (e.type == SWT.MouseMove && (e.stateMask & SWT.BUTTON1) != 0) { Point p = shell.toDisplay(e.x, e.y); p.x -= startX; p.y -= startY; shell.setLocation(p); } if (e.type == SWT.Paint) { e.gc.drawImage(image, imageData.x, imageData.y); } } }; shell.addListener(SWT.KeyDown, l); shell.addListener(SWT.MouseDown, l); shell.addListener(SWT.MouseMove, l); shell.addListener(SWT.Paint, l); shell.setSize(imageData.x + imageData.width, imageData.y + imageData.height); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } region.dispose(); image.dispose(); display.dispose(); }
}</source>
Creating Shells
- Shells can be separated into two categories: top-level shells and secondary/dialog shells.
- Shells that do not have a parent are top-level shells.
- All dialog shells have a parent.
Use the following constructors to create top-level shells:
<source lang="java">
Shell()
Shell(int style) Shell(Display display) Shell(Display display, int style)</source>
DIALOG_TRIM = (CLOSE | TITLE | BORDER): dialog shells.
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellDialogTrimCloseTitle {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display,SWT.DIALOG_TRIM); shell.setLayout(new FillLayout()); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
Get Default Button
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellDefaultButton {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); Button cancelButton = new Button(shell, SWT.PUSH); cancelButton.setText("Canel"); Button rateButton = new Button(shell, SWT.PUSH); rateButton.setText("OK"); shell.setDefaultButton(rateButton); System.out.println(shell.getDefaultButton()); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } }
}</source>
Getting Controls on a shell
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Control; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellControlsGetting {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.setLayout(new FillLayout()); for (int i = 0; i < 20; i++) { Button button = new Button(shell, SWT.TOGGLE); button.setText("B" + i); } Control[] children = shell.getChildren(); for (int i = 0; i < children.length; i++) { Control child = children[i]; System.out.println(child); } shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
ON_TOP: This style bit causes the shell to always float on the top of the desktop.
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellOnTop {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display,SWT.ON_TOP); shell.setLayout(new FillLayout()); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
Open a shell maximized (full screen)
<source lang="java">
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellMax {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setMaximized(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Open a shell minimized (iconified)
<source lang="java">
import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellMini {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setMinimized(true); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Prevent a shell from closing (prompt the user)
<source lang="java">
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.MessageBox; import org.eclipse.swt.widgets.Shell; public class ShellClosePrevent {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); shell.addListener(SWT.Close, new Listener() { public void handleEvent(Event event) { int style = SWT.APPLICATION_MODAL | SWT.YES | SWT.NO; MessageBox messageBox = new MessageBox (shell, style); messageBox.setText ("Information"); messageBox.setMessage ("Close the shell?"); event.doit = messageBox.open () == SWT.YES; } }); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Ring Shell
You can create an irregular-shaped shell with the setRegion method:
<source lang="java">
public void setRegion(Region region)</source>
Set default Button for Shell
The default button is the button that gains the focus and is pressed automatically when a SWT.TRAVERSE_RETURN type traversal happens.
<source lang="java">
public Button getDefaultButton()
public void setDefaultButton(Button defaultButton)</source>
Set icons with different resolutions
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 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 *******************************************************************************/
//package org.eclipse.swt.snippets; /*
* example snippet: set icons with different resolutions * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.0 */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.GC; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class IconResolutions {
public static void main(String[] args) { Display display = new Display(); Image small = new Image(display, 16, 16); GC gc = new GC(small); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillArc(0, 0, 16, 16, 45, 270); gc.dispose(); Image large = new Image(display, 32, 32); gc = new GC(large); gc.setBackground(display.getSystemColor(SWT.COLOR_RED)); gc.fillArc(0, 0, 32, 32, 45, 270); gc.dispose(); /* * Provide different resolutions for icons to get high quality rendering * wherever the OS needs large icons. For example, the ALT+TAB window on * certain systems uses a larger icon. */ Shell shell = new Shell(display); shell.setText("Small and Large icons"); shell.setImages(new Image[] { small, large }); /* * No large icon: the OS will scale up the small icon when it needs a large * one. */ Shell shell2 = new Shell(display); shell2.setText("Small icon"); shell2.setImage(small); shell.open(); shell2.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } small.dispose(); large.dispose(); display.dispose(); }
}</source>
Set left upper corner image
Change the icon in the Alt+Tab window (Windows).
<source lang="java">
public void setImage(Image image)</source>
Setting the Title
The setText method can be used to set a shell"s title:
<source lang="java">
public void setText(String text)</source>
Shell States
A shell is always displayed in one of the following states: Normal, Maximized, Minimized.
To minimize a shell, you can use the following method:
<source lang="java">
public void setMinimized(boolean minimized)</source>
SHELL_TRIM = (CLOSE | TITLE | MIN | MAX | RESIZE): top-level shells.
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellTrimCloseTitleMinMax {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display,SWT.SHELL_TRIM); shell.setLayout(new FillLayout()); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
Shell without Title bar
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellWithoutTitleBar {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display, SWT.NO_TRIM | SWT.ON_TOP); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
Shell with SWT.SHELL_TRIM|SWT.TOOL style
<source lang="java">
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 ShellStyleTrimTool {
public static void main(String[] args) { final Display display = new Display(); Shell shell = new Shell(display, SWT.SHELL_TRIM | SWT.TOOL); shell.setLayout(new GridLayout()); Button button = new Button(shell, SWT.PUSH | SWT.LEFT); button.setText("Button"); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } }
}</source>
The modal state of shells: APPLICATION_MODAL, MODELESS, PRIMARY_MODAL, SYSTEM_MODAL
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellApplicationModal {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display,SWT.APPLICATION_MODAL); shell.setLayout(new FillLayout()); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
TOOL: the shell is a tool window(floating toolbars)
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class ShellToolWindow {
public static void main(String[] args) { final Display display = new Display(); final Shell shell = new Shell(display,SWT.SHELL_TRIM | SWT.TOOL); shell.setLayout(new FillLayout()); shell.open(); // Set up the event loop. while (!shell.isDisposed()) { if (!display.readAndDispatch()) { // If no more entries in event queue display.sleep(); } } display.dispose(); }
}</source>
Understanding the Shell Object
The Shell object represents a window-either a top-level window or a dialog window. Shell Styles:
StyleDescriptionBORDERAdds a border.CLOSEAdds a close button.MINAdds a minimize button.MAXAdds a maximize button.NO_TRIMCreates a Shell that has no border and can"t be moved, closed, resized, minimized, or maximized. (Useful for splash screens).RESIZEAdds a resizable border.TITLEAdds a title bar.DIALOG_TRIMConvenience style, equivalent to TITLE | CLOSE | BORDER.SHELL_TRIMConvenience style, equivalent to CLOSE | TITLE | MIN | MAX | RESIZE.APPLICATION_MODALCreates a Shell that"s modal to the application. Note that you should specify only one of APPLICATION_MODAL, PRIMARY_MODAL, SYSTEM_MODAL, or MODELESS; you can specify more, but only one is applied. The order of preference is SYSTEM_MODAL, APPLICATION_MODAL, PRIMARY_MODAL, then MODELESS.PRIMARY_MODALCreates a primary modal Shell.SYSTEM_MODALCreates a Shell that"s modal system-wide.MODELESSCreates a modeless Shell.