kooyee ‘s blog

开源软件, 众人努力的结晶, 全人类的共同财富
posts - 103, comments - 55, trackbacks - 0, articles - 66
   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

[SWT] swt中右键菜单

Posted on 2007-07-20 21:24 kooyee 阅读(1787) 评论(0)  编辑  收藏 所属分类: GUI骨衣

加入右键菜单到CTabFolder中的item, 同理其他的control也可以加入右键菜单。

public static void main(String[] args) {
    
final Display display = new Display();
    Shell shell 
= new Shell(display);
    shell.setLayout(
new FillLayout());
    
final CTabFolder f = new CTabFolder(shell, SWT.BORDER);
    
for (int i = 0; i < 10; i++{
        CTabItem item 
= new CTabItem(f, SWT.NONE);
        item.setText(
"item "+i);
    }

    
final Menu menu = new Menu (shell, SWT.POP_UP);
    
final MenuItem menuItem = new MenuItem (menu, SWT.PUSH);
    f.setMenu(menu);
    menu.addMenuListener(
new MenuAdapter() {
        
public void menuShown(MenuEvent e) {
            Point p 
= display.getCursorLocation();
            p 
= f.toControl(p);
            CTabItem item 
= f.getItem(p);
            
if (item == null{
                menu.setVisible(
false);
                
return;
           }

           menuItem.setText(
"action for "+item.getText());
        }

    }
);
    shell.setSize(
800600);
    shell.open();
    
while (!shell.isDisposed()) {
        
if (!display.readAndDispatch())
            display.sleep();
    }

    display.dispose();
}


只有注册用户登录后才能发表评论。


网站导航: