指定组件, 在其下方弹出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;

/**
 * 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(300200));
        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(
00, listRect.width + 1, listRect.height + 1);
            
break;
        
case SWT.Close:
            event.doit 
= false;
            popup.setVisible (
false);
            
break;
        
case SWT.Deactivate:
            popup.setVisible (
false);
            
break;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值