2011-03-20 83 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(); 
    } 
}