This article mainly implements menu, toolbar and coolbar, which are commonly used in the interface, compared with the previous simple controls, these controls are more flexible and difficult to use.
Menu implements many common types, such as single choice, check menu, and right-click menu. toolbar implements single choice, check, and drop-down, while coolbar is a flexible control, you can place controls on them and drag them as needed.
Implementation Code:
Import Org. eclipse. SWT. SWT; <br/> Import Org. eclipse. SWT. events. menuevent; <br/> Import Org. eclipse. SWT. events. menulistener; <br/> Import Org. eclipse. SWT. events. selectionevent; <br/> Import Org. eclipse. SWT. events. selectionlistener; <br/> Import Org. eclipse. SWT. graphics. color; <br/> Import Org. eclipse. SWT. graphics. point; <br/> Import Org. eclipse. SWT. graphics. rectangle; <br/> Import Org. eclipse. SWT. layout. fi Lllayout; <br/> Import Org. eclipse. SWT. widgets. button; <br/> Import Org. eclipse. SWT. widgets. combo; <br/> Import Org. eclipse. SWT. widgets. composite; <br/> Import Org. eclipse. SWT. widgets. coolbar; <br/> Import Org. eclipse. SWT. widgets. coolitem; <br/> Import Org. eclipse. SWT. widgets. display; <br/> Import Org. eclipse. SWT. widgets. label; <br/> Import Org. eclipse. SWT. widgets. menu; <br/> Import Org. eclipse. SWT. widgets. Menuitem; <br/> Import Org. eclipse. SWT. widgets. shell; <br/> Import Org. eclipse. SWT. widgets. toolbar; <br/> Import Org. eclipse. SWT. widgets. toolitem; </P> <p>/** <br/> * @ author xiangjie <br/> */<br/> public class test_menuandtoolbar {</P> <p> Public void test_menuandtoolbar (composite parent) {<br/> show_controlmenu (parent); <br/> show_toolbar (parent); <br/> showcoolbar (parent ); <br/>}</P> <p> private Void showcoolbar (composite parent) {<br/> composite tempcom = new composite (parent, SWT. border); <br/> tempcom. setlayout (New filllayout (); </P> <p> coolbar = new coolbar (tempcom, SWT. border); <br/> // coolbar. setlayout (New filllayout (); </P> <p> coolitem Item1 = new coolitem (coolbar, SWT. none); </P> <p> combo = new combo (coolbar, SWT. none | SWT. read_only); <br/> combo. setitems (New String [] {" Test1 "," Test2 "}); </P> <p> point P = combo. computesize (SWT. default, SWT. default); <br/> combo. setsize (p); <br/> point P2 = item1.computesize (P. x, p. y); <br/> item1.setsize (P2); <br/> item1.setcontrol (Combo); </P> <p> coolitem item2 = new coolitem (coolbar, SWT. none); </P> <p> button testbutton = new button (coolbar, SWT. none); <br/> testbutton. settext ("test! "); </P> <p> P = testbutton. computesize (SWT. default, SWT. default); <br/> combo. setsize (p); <br/> P2 = item2.computesize (P. x, p. y); <br/> item2.setsize (P2); <br/> item2.setcontrol (testbutton ); </P> <p >}</P> <p> private void show_toolbar (composite parent) {</P> <p> composite tempcom = new composite (parent, SWT. border); <br/> tempcom. setlayout (New filllayout (); </P> <p> final toolbar = new toolbar (tempcom, SW T. Wrap); <br/> toolitem pitem = new toolitem (toolbar, SWT. Push); <br/> pitem. settext ("push me! "); </P> <p> New toolitem (toolbar, SWT. separator); </P> <p> toolitem citem1 = new toolitem (toolbar, SWT. check); <br/> citem1.settext ("Check me1! "); </P> <p> toolitem citem2 = new toolitem (toolbar, SWT. Check); <br/> citem2.settext (" Check me2! "); </P> <p> New toolitem (toolbar, SWT. separator); </P> <p> toolitem ritem1 = new toolitem (toolbar, SWT. radio); <br/> ritem1.settext ("Radio me1! "); </P> <p> toolitem ritem2 = new toolitem (toolbar, SWT. Radio); <br/> ritem2.settext (" Radio me2! "); </P> <p> final toolitem droitem = new toolitem (toolbar, SWT. drop_down); <br/> droitem. settext ("drop down me"); </P> <p> final menu testmenu = new menu (tempcom. getshell (), SWT. pop_up); <br/> (New menuitem (testmenu, SWT. none )). settext ("just to test! "); </P> <p> droitem. addselectionlistener (New selectionlistener () {</P> <p> @ override <br/> Public void widgetselected (selectionevent E) {<br/> // todo auto-generated method stub <br/> Boolean flat = testmenu. getvisible (); </P> <p> If (! Flat) {<br/> showmenu (); <br/>} else {<br/> testmenu. setvisible (false); <br/>}</P> <p> private void showmenu () {<br/> rectangle RDBMS = droitem. getbounds (); </P> <p> point = toolbar. todisplay (RDBMS. x, RDBMS. Y + CTS. height); </P> <p> testmenu. setlocation (point); </P> <p> testmenu. setvisible (true); </P> <p >}</P> <p> @ override <br/> Public void widgetdefaselecselected (selectionevent E) {<br/> // todo auto-Genera Ted method stub </P> <p >}< br/>}); </P> <p >}</P> <p> private void show_controlmenu (composite parent) {<br/> composite tempcom = new composite (parent, SWT. border); <br/> tempcom. setlayout (New filllayout (); </P> <p> final label testlabel = new label (tempcom, SWT. border | SWT. WRAP); <br/> testlabel. settext ("Please use right click! "); </P> <p> testlabel. setbackground (new color (null, 255, 0, 0); </P> <p> final menu = new menu (testlabel); <br/> testlabel. setmenu (menu); </P> <p> menuitem Item1 = new menuitem (menu, SWT. none); <br/> item1.settext ("test! "); </P> <p> menu. addmenulistener (New menulistener () {</P> <p> @ override <br/> Public void menushown (menuevent E) {<br/> // todo auto-generated method stub <br/> // Boolean flat = menu. getvisible (); <br/> testlabel. settext ("menu is visiable! "); <Br/> menu. setvisible (true); <br/>}</P> <p> @ override <br/> Public void menuhidden (menuevent E) {<br/> // todo auto-generated method stub <br/> testlabel. settext ("menu is hidden! "); <Br/> menu. setvisible (false); <br/>}< br/>}); <br/>}</P> <p> Public void shellmenutest (shell) {<br/> menu = new menu (shell, SWT. bar | SWT. drop_down); </P> <p> menuitem Item1 = new menuitem (menu, SWT. cascade); <br/> item1.settext ("& Open"); </P> <p> menu sumenu = new menu (shell, SWT. drop_down); // new menu (Item1); <br/> item1.setmenu (sumenu); </P> <p> menuitem item2 = new menuitem (sumenu, SWT. radio ); <Br/> item2.settext ("& Save"); </P> <p> menuitem item3 = new menuitem (sumenu, SWT. radio); <br/> item3.settext ("& test"); </P> <p> New menuitem (sumenu, SWT. separator); </P> <p> menuitem item4 = new menuitem (sumenu, SWT. check); <br/> item4.settext ("& check"); </P> <p> menuitem item5 = new menuitem (sumenu, SWT. check); <br/> item5.settext ("& Scheck"); </P> <p> New menuitem (sumenu, SWT. separator); </P> <p> menuitem item6 = new Menuitem (sumenu, SWT. cascade); <br/> item6.settext ("& menu"); </P> <p> menu sumenu2 = new menu (shell, SWT. drop_down); // new menu (Item1); <br/> item6.setmenu (sumenu2); </P> <p> menuitem item7 = new menuitem (sumenu2, SWT. cascade); <br/> item7.settext ("& First"); </P> <p> shell. setmenubar (menu); <br/>}</P> <p>/** <br/> * create data: 2010-1-14 </br> <br/> * @ author xiangjie <br/> * @ Param ARGs <br/> * <br/> */<Br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> display = New Display (); <br/> shell = new shell (Display ); <br/> shell. settext ("SWT menu and toolbar:"); <br/> shell. setlayout (New filllayout (SWT. vertical); </P> <p> test_menuandtoolbar test = new test_menuandtoolbar (); <br/> test. shellmenutest (Shell); <br/> test. test_menuandtoolbar (Shell); </P> <p> Shell. Pack (); <br/> shell. open (); <br/> while (! Shell. isdisposed () {<br/> If (! Display. readanddispatch () <br/> display. sleep (); <br/>}< br/> display. dispose (); <br/>}< br/>
Demo:
This article is the original article of eclipselight.org (eclipse light). For more information, see the source.
Fixed Link: http://www.eclipselight.org/swt-jface/616/