比如有一个Button, 点击以后在button的正下方弹出一个Popup
另外监听dialog的SWT.Close, SWT.Paint, SWT.Deactivate三个事件如下:
import
org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Create at 2007-4-29,下午03:20:05<br>
*
* @author Brad.Wu
* @version 1.0
*/
public class TestShell {
private Shell sShell = null;
private Button button = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments) for the correct SWT
* library path in order to run with the SWT dlls. The dlls are located in the SWT plugin jar. For example, on
* Windows the Eclipse SWT 3.1 plugin jar is: installation_directorypluginsorg.eclipse.swt.win32_3.1.0.jar
*/
Display display = Display.getDefault();
TestShell thisClass = new TestShell();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new Shell();
sShell.setText("Shell");
sShell.setSize(new Point(300, 200));
sShell.setLayout(new GridLayout());
Composite comp = new Composite(sShell, SWT.BORDER);
GridLayout layout = new GridLayout();
layout.horizontalSpacing = 20;
comp.setLayout(layout);
button = new Button(comp, SWT.NONE);
button.setText("Click Me");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
Shell dialog = new Shell(sShell, SWT.NO_TRIM | SWT.ON_TOP);
dialog.setBackground(button.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
Display display = button.getDisplay();
Rectangle parentRect = display.map(button.getParent(), null, button.getBounds());
Point comboSize = button.getSize();
Rectangle displayRect = button.getMonitor().getClientArea();
int width = 200;
int height = 100;
int x = parentRect.x;
int y = parentRect.y + comboSize.y;
if (y + height > displayRect.y + displayRect.height)
y = parentRect.y - height;
if (x + width > displayRect.x + displayRect.width)
x = displayRect.x + displayRect.width - width;
dialog.setBounds(x, y, width, height);
dialog.setVisible(true);
}
});
}
}
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
/**
* Create at 2007-4-29,下午03:20:05<br>
*
* @author Brad.Wu
* @version 1.0
*/
public class TestShell {
private Shell sShell = null;
private Button button = null;
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
/*
* Before this is run, be sure to set up the launch configuration (Arguments->VM Arguments) for the correct SWT
* library path in order to run with the SWT dlls. The dlls are located in the SWT plugin jar. For example, on
* Windows the Eclipse SWT 3.1 plugin jar is: installation_directorypluginsorg.eclipse.swt.win32_3.1.0.jar
*/
Display display = Display.getDefault();
TestShell thisClass = new TestShell();
thisClass.createSShell();
thisClass.sShell.open();
while (!thisClass.sShell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
* This method initializes sShell
*/
private void createSShell() {
sShell = new Shell();
sShell.setText("Shell");
sShell.setSize(new Point(300, 200));
sShell.setLayout(new GridLayout());
Composite comp = new Composite(sShell, SWT.BORDER);
GridLayout layout = new GridLayout();
layout.horizontalSpacing = 20;
comp.setLayout(layout);
button = new Button(comp, SWT.NONE);
button.setText("Click Me");
button.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
Shell dialog = new Shell(sShell, SWT.NO_TRIM | SWT.ON_TOP);
dialog.setBackground(button.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
Display display = button.getDisplay();
Rectangle parentRect = display.map(button.getParent(), null, button.getBounds());
Point comboSize = button.getSize();
Rectangle displayRect = button.getMonitor().getClientArea();
int width = 200;
int height = 100;
int x = parentRect.x;
int y = parentRect.y + comboSize.y;
if (y + height > displayRect.y + displayRect.height)
y = parentRect.y - height;
if (x + width > displayRect.x + displayRect.width)
x = displayRect.x + displayRect.width - width;
dialog.setBounds(x, y, width, height);
dialog.setVisible(true);
}
});
}
}
另外监听dialog的SWT.Close, SWT.Paint, SWT.Deactivate三个事件如下:
switch
(event.type)
{
case SWT.Paint:
// draw black rectangle around list
// 这个list是Popup里面的组件
Rectangle listRect = list.getBounds();
Color black = getDisplay().getSystemColor(SWT.COLOR_BLACK);
event.gc.setForeground(black);
event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1);
break;
case SWT.Close:
event.doit = false;
popup.setVisible (false);
break;
case SWT.Deactivate:
popup.setVisible (false);
break;
}
case SWT.Paint:
// draw black rectangle around list
// 这个list是Popup里面的组件
Rectangle listRect = list.getBounds();
Color black = getDisplay().getSystemColor(SWT.COLOR_BLACK);
event.gc.setForeground(black);
event.gc.drawRectangle(0, 0, listRect.width + 1, listRect.height + 1);
break;
case SWT.Close:
event.doit = false;
popup.setVisible (false);
break;
case SWT.Deactivate:
popup.setVisible (false);
break;
}