[心缘地方]同学录
首页 | 功能说明 | 站长通知 | 最近更新 | 编码查看转换 | 代码下载 | 常见问题及讨论 | 《深入解析ASP核心技术》 | 王小鸭自动发工资条VBA版
登录系统:用户名: 密码: 如果要讨论问题,请先注册。

[备忘]RCP开发,SWT的按钮旁边的向下小箭头的实现方法

上一篇:[整理]几篇JNA的文章---Java里省事调用DLL的东东
下一篇:[整理]Javascript使用“IE默认XSL”格式化XML字符串

添加日期:2011/8/3 16:25:41 快速返回   返回列表 阅读6462次
(1)创建ToolBar的时候,要有SWT.FLAT选项,否则小箭头显示不正确。
new ToolBar(shell,SWT.FLAT)
(2)创建ToolItem时,使用SWT.DROP_DOWN选项,就会自动出小箭头了
new ToolItem(toolBar, SWT.DROP_DOWN);
(3)点击小箭头的动作,要自己添加。一般是显示一个Menu,这个Menu也要自己实现。
举例下,xxx是一个ToolItem。


xxx.addSelectionListener(new SelectionAdapter() {
    public void widgetSelected(SelectionEvent e) {
         if (e.detail == SWT.ARROW) {
                  //这里写点击小箭头时的处理,注意这个判断方法。
                  //以下是计算Menu的显示位置的。
             Rectangle bounds = addMsg.getBounds();
             Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
             xxxMenu.setLocation(point);
             xxxMsgMenu.setVisible(true);

        }else{
            //这里写正常的点击大按钮时的处理。
        }
    }
});



以下文章是转的:
=======================================================================
工具栏通常有两种:  toolbar、coolBar。两者的区分是CoolBar可以自由移动。

toolBar的实现通常有两种方式:
1、使用ToolBar和ToolItem;
2、使用ToolBarManager 、ActionContributionItem、Action组合;

先介绍第一种方式:使用ToolBar和ToolItem


package menu.test;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

public class ToolBarExample {
    Display display = new Display();
    Shell shell = new Shell(display);
    ToolBar toolBar;
    Action forwardAction,homeAction;
    public ToolBarExample() {
        MenuManager menuManager = new MenuManager();
        
        //1.添加工具栏
        toolBar = new ToolBar(shell,SWT.FLAT|SWT.WRAP|SWT.RIGHT |SWT.BORDER);

        //2.添加工具项-push
        ToolItem pushItem = new ToolItem(toolBar,SWT.PUSH);
        pushItem.setText("Push Item");
        Image icon = new Image(shell.getDisplay(),"icons/forward.gif");
        pushItem.setImage(icon);
        
        //3.添加工具项-check,radio,seperator
        ToolItem checkItem = new ToolItem(toolBar,SWT.CHECK);
        checkItem.setText("Check Item");
        ToolItem radioItem1 = new ToolItem(toolBar, SWT.RADIO);
        radioItem1.setText("RADIO item 1");
        ToolItem radioItem2 = new ToolItem(toolBar, SWT.RADIO);
        radioItem2.setText("RADIO item 2");
        ToolItem separatorItem = new ToolItem(toolBar, SWT.SEPARATOR);
        
        //4.下拉DROP_DOWN、
        final ToolItem dropDownItem = new ToolItem(toolBar, SWT.DROP_DOWN);
        dropDownItem.setText("DROP_DOWN item");
        dropDownItem.setToolTipText("Click here to see a drop down menu ...");
        final Menu menu = new Menu(shell, SWT.POP_UP);
        new MenuItem(menu, SWT.PUSH).setText("Menu item 1");
        new MenuItem(menu, SWT.PUSH).setText("Menu item 2");
        new MenuItem(menu, SWT.SEPARATOR);
        new MenuItem(menu, SWT.PUSH).setText("Menu item 3");
        // 设置工具项的事件监听器
        dropDownItem.addListener(SWT.Selection, new Listener() {
            public void handleEvent(Event event) {
                if (event.detail == SWT.ARROW) {
                    Rectangle bounds = dropDownItem.getBounds();
                    Point point = toolBar.toDisplay(bounds.x, bounds.y + bounds.height);
                    // 设置菜单的显示位置
                    menu.setLocation(point);
                    menu.setVisible(true);
                }
            }
        });
        
        // 5.事件处理
        // 设置工具项的事件监听器
        Listener selectionListener = new Listener() {
            public void handleEvent(Event event) {
                ToolItem item = (ToolItem) event.widget;
                System.out.println(item.getText() + " is selected");
                if ((item.getStyle() & SWT.RADIO) != 0
                        || (item.getStyle() & SWT.CHECK) != 0)
                    System.out.println("Selection status: "
                            + item.getSelection());
            }
        };
        pushItem.addListener(SWT.Selection, selectionListener);
        checkItem.addListener(SWT.Selection, selectionListener);
        radioItem1.addListener(SWT.Selection, selectionListener);
        radioItem2.addListener(SWT.Selection, selectionListener);
        dropDownItem.addListener(SWT.Selection, selectionListener);
        
        
        // 6. 其他组件text 、checkbox
        ToolItem textItem = new ToolItem(toolBar,SWT.SEPARATOR);
        Text text = new Text(toolBar, SWT.BORDER | SWT.SINGLE);
        text.pack();
        textItem.setWidth(text.getSize().x);
        textItem.setControl(text);
        
        ToolItem sep = new ToolItem(toolBar, SWT.SEPARATOR);
        Combo combo = new Combo(toolBar, SWT.READ_ONLY);
        for (int i = 0; i < 4; i++) {
          combo.add("Item " + i);
        }
        combo.pack();
        sep.setWidth(combo.getSize().x);
        sep.setControl(combo);
        
        toolBar.pack();
        shell.addListener(SWT.Resize, new Listener(){

            public void handleEvent(Event arg0) {
                Rectangle clientArea = shell.getClientArea();
                toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT));
                
            }
            
        });
        shell.setSize(400, 300);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
    
    public static void main(String[] args) {
        new ToolBarExample();
    }

}



对于第二种方法:


package menu;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.ActionContributionItem;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.action.ToolBarManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Decorations;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;

public class ToolBarMangerExample {
  Display display = new Display();
  Shell shell = new Shell(display);
  Action openAction,exitAction;
  ToolBar toolBar;

  public ToolBarMangerExample() {
    MenuManager menuManager = new MenuManager();
    
    toolBar = new ToolBar(shell, SWT.FLAT | SWT.RIGHT | SWT.BORDER);
    final ToolBarManager toolBarManager = new ToolBarManager(toolBar);
    initActions();// 初始化Action
    toolBarManager.add(openAction);
    ActionContributionItem item = new ActionContributionItem(exitAction);
    item.setMode(ActionContributionItem.MODE_FORCE_TEXT);
    toolBarManager.add(item);
    toolBarManager.update(true);
    toolBar.pack();
    
    MenuManager fileMenuManager = new MenuManager("&File");
    fileMenuManager.add(openAction);
    fileMenuManager.add(exitAction);
    menuManager.add(fileMenuManager);
    menuManager.updateAll(true);
  
    shell.setMenuBar(menuManager.createMenuBar((Decorations)shell));
    shell.addListener(SWT.Resize, new Listener(){

        public void handleEvent(Event arg0) {
            Rectangle clientArea = shell.getClientArea();
            toolBar.setSize(toolBar.computeSize(clientArea.width, SWT.DEFAULT));
        }
    });
    shell.setSize(300,200);
    shell.open();

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

    display.dispose();
  }
  private void initActions(){
      openAction =
          new Action("&openAction",
            ImageDescriptor.createFromFile(null, "icons/Open.gif")) {
          public void run() {
            System.out.println("OPEN");
          }
        };
        openAction.setAccelerator(SWT.CTRL + 'O');


        exitAction =
          new Action("&Exit",
            ImageDescriptor.createFromFile(null, "icons/Exit.gif")) {
          public void run() {
            System.out.println("Exit");
          }
        };
        exitAction.setAccelerator(SWT.CTRL + 'E');
  }
  public static void main(String[] args) {
    new ToolBarMangerExample();
  }
}

 

评论 COMMENTS
没有评论 No Comments.

添加评论 Add new comment.
昵称 Name:
评论内容 Comment:
验证码(不区分大小写)
Validation Code:
(not case sensitive)
看不清?点这里换一张!(Change it here!)
 
评论由管理员查看后才能显示。the comment will be showed after it is checked by admin.
CopyRight © 心缘地方 2005-2999. All Rights Reserved