比如有一个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;
/**
*Createat2007-4-29,下午03:20:05<br>
*
*@authorBrad.Wu
*@version1.0
*/
public class TestShell {
privateShellsShell=null;
privateButtonbutton=null;
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
/*
*Beforethisisrun,besuretosetupthelaunchconfiguration(Arguments->VMArguments)forthecorrectSWT
*librarypathinordertorunwiththeSWTdlls.ThedllsarelocatedintheSWTpluginjar.Forexample,on
*WindowstheEclipseSWT3.1pluginjaris:installation_directorypluginsorg.eclipse.swt.win32_3.1.0.jar
*/
Displaydisplay=Display.getDefault();
TestShellthisClass=newTestShell();
thisClass.createSShell();
thisClass.sShell.open();
while(!thisClass.sShell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
*ThismethodinitializessShell
*/
privatevoidcreateSShell(){
sShell=newShell();
sShell.setText("Shell");
sShell.setSize(newPoint(300,200));
sShell.setLayout(newGridLayout());
Compositecomp=newComposite(sShell,SWT.BORDER);
GridLayoutlayout=newGridLayout();
layout.horizontalSpacing=20;
comp.setLayout(layout);
button=newButton(comp,SWT.NONE);
button.setText("ClickMe");
button.addSelectionListener(neworg.eclipse.swt.events.SelectionAdapter(){
publicvoidwidgetSelected(org.eclipse.swt.events.SelectionEvente){
Shelldialog=newShell(sShell,SWT.NO_TRIM|SWT.ON_TOP);
dialog.setBackground(button.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
Displaydisplay=button.getDisplay();
RectangleparentRect=display.map(button.getParent(),null,button.getBounds());
PointcomboSize=button.getSize();
RectangledisplayRect=button.getMonitor().getClientArea();
intwidth=200;
intheight=100;
intx=parentRect.x;
inty=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;
/**
*Createat2007-4-29,下午03:20:05<br>
*
*@authorBrad.Wu
*@version1.0
*/
public class TestShell {
privateShellsShell=null;
privateButtonbutton=null;
/**
*@paramargs
*/
publicstaticvoidmain(String[]args){
//TODOAuto-generatedmethodstub
/*
*Beforethisisrun,besuretosetupthelaunchconfiguration(Arguments->VMArguments)forthecorrectSWT
*librarypathinordertorunwiththeSWTdlls.ThedllsarelocatedintheSWTpluginjar.Forexample,on
*WindowstheEclipseSWT3.1pluginjaris:installation_directorypluginsorg.eclipse.swt.win32_3.1.0.jar
*/
Displaydisplay=Display.getDefault();
TestShellthisClass=newTestShell();
thisClass.createSShell();
thisClass.sShell.open();
while(!thisClass.sShell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
/**
*ThismethodinitializessShell
*/
privatevoidcreateSShell(){
sShell=newShell();
sShell.setText("Shell");
sShell.setSize(newPoint(300,200));
sShell.setLayout(newGridLayout());
Compositecomp=newComposite(sShell,SWT.BORDER);
GridLayoutlayout=newGridLayout();
layout.horizontalSpacing=20;
comp.setLayout(layout);
button=newButton(comp,SWT.NONE);
button.setText("ClickMe");
button.addSelectionListener(neworg.eclipse.swt.events.SelectionAdapter(){
publicvoidwidgetSelected(org.eclipse.swt.events.SelectionEvente){
Shelldialog=newShell(sShell,SWT.NO_TRIM|SWT.ON_TOP);
dialog.setBackground(button.getDisplay().getSystemColor(SWT.COLOR_DARK_GREEN));
Displaydisplay=button.getDisplay();
RectangleparentRect=display.map(button.getParent(),null,button.getBounds());
PointcomboSize=button.getSize();
RectangledisplayRect=button.getMonitor().getClientArea();
intwidth=200;
intheight=100;
intx=parentRect.x;
inty=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)
{
caseSWT.Paint:
//drawblackrectanglearoundlist
//这个list是Popup里面的组件
RectanglelistRect=list.getBounds();
Colorblack=getDisplay().getSystemColor(SWT.COLOR_BLACK);
event.gc.setForeground(black);
event.gc.drawRectangle(0,0,listRect.width+1,listRect.height+1);
break;
caseSWT.Close:
event.doit=false;
popup.setVisible(false);
break;
caseSWT.Deactivate:
popup.setVisible(false);
break;
}
caseSWT.Paint:
//drawblackrectanglearoundlist
//这个list是Popup里面的组件
RectanglelistRect=list.getBounds();
Colorblack=getDisplay().getSystemColor(SWT.COLOR_BLACK);
event.gc.setForeground(black);
event.gc.drawRectangle(0,0,listRect.width+1,listRect.height+1);
break;
caseSWT.Close:
event.doit=false;
popup.setVisible(false);
break;
caseSWT.Deactivate:
popup.setVisible(false);
break;
}