2011-03-20 82 views
2

如何使可拖动/可停靠的工具栏与Eclipse的JFace/SWT有?你可以发布一个ApplicationWindow的简单例子,或链接如何使它成为好的源码。Draggable工具栏

谢谢。

回答

3

SWT有一个叫酷工具栏组件,您可以通过使用 CoolBarManager很容易地创建CoolBars,或者您也可以手动使用只是其中(API Doc

+0

嗨,谢谢你建议'CoolBar' /'CoolBarManager',但我无法得到这个工作。你可以用'CoolBar'发布一个简单的'ApplicationWindow'的例子,或者链接如何使用它的好例子吗? – 2011-03-20 15:38:17

3

万一有人发现了这个问题,我已经准备了小例子。我的问题是错误地使用add方法。您必须使用add(IToolBarManager toolBarManager)方法CoolBarManager不是基类ContributionManager中的一种。

import org.eclipse.jface.action.Action; 
import org.eclipse.jface.action.CoolBarManager; 
import org.eclipse.jface.action.IToolBarManager; 
import org.eclipse.jface.action.ToolBarManager; 
import org.eclipse.jface.window.ApplicationWindow; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Control; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 

public class App extends ApplicationWindow { 

    public App(Shell parent) { 
    super(parent); 
    } 

    @Override 
    protected Control createContents(Composite parent) { 
    getShell().setText("CoolBarManager example"); 

    return super.createContents(parent); 
    } 

    @Override 
    public void create() { 
    addCoolBar(SWT.FLAT); 
    super.create(); 
    } 

    @Override 
    protected CoolBarManager createCoolBarManager(int style) { 
    CoolBarManager cbm = new CoolBarManager(style); 

    IToolBarManager tb1 = new ToolBarManager(style); 
    IToolBarManager tb2 = new ToolBarManager(style); 

    tb1.add(new Action() { 
     { 
     setText("&Button1"); 
     } 
    }); 
    tb1.add(new Action() { 
     { 
     setText("&Button2"); 
     } 
    }); 
    tb1.add(new Action() { 
     { 
     setText("&Button3"); 
     } 
    }); 

    tb2.add(new Action() { 
     { 
     setText("&Button4"); 
     } 
    }); 

    tb2.add(new Action() { 
     { 
     setText("&Button5"); 
     } 
    }); 

    cbm.add(tb1); 
    cbm.add(tb2); 

    return cbm; 
    } 

    public static void main(String[] args) { 
    App app = new App(null); 

    app.setBlockOnOpen(true); 
    app.open(); 

    Display.getCurrent().dispose(); 
    } 
}