指定组件, 在其下方弹出Popup

比如有一个Button, 点击以后在button的正下方弹出一个Popup
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);
}

}
);
}

}


另外监听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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值