SWT (JFace) Menu, Bar... Experience code

Source: Internet
Author: User

DEMO code:
MenuExamples. java Copy codeThe Code is as follows: package swt_jface.demo5;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. graphics. Image;
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;
Public class MenuExamples {

Display display = new Display ();
Shell shell = new Shell (display );
Public MenuExamples (){
Menu menuBar = new Menu (shell, SWT. BAR );
MenuItem itemHello = new MenuItem (menuBar, SWT. PUSH );
ItemHello. setText ("& Hello ");
ItemHello. addListener (SWT. Selection, new Listener (){
Public void handleEvent (Event event ){
System. out. println ("HELLO ");
}
});
MenuItem itemCascade = new MenuItem (menuBar, SWT. CASCADE );
ItemCascade. setText ("& CASCADE item ");
Menu menu = new Menu (itemCascade );
MenuItem itemPush = new MenuItem (menu, SWT. PUSH );
ItemPush. setText ("& PUSH item \ tCtrl + P ");
ItemPush. setAccelerator (SWT. CTRL + 'P ');
Image icon = new Image (shell. getDisplay (), "C:/icons/new.gif ");
ItemPush. setImage (icon );
ItemPush. addListener (SWT. Selection, new Listener (){
Public void handleEvent (Event event ){
System. out. println ("item selected: PUSH item ");
}
});
Final MenuItem itemCheck = new MenuItem (menu, SWT. CHECK );
ItemCheck. setText ("CHEC & K item \ tCtrl + K ");
ItemCheck. setAccelerator (SWT. CTRL + 'k ');
ItemCheck. addListener (SWT. Selection, new Listener (){
Public void handleEvent (Event event ){
System. out. println ("item selected: CHECK item ");
System. out. println ("Selection:" + itemCheck. getSelection ());
}
});
New MenuItem (menu, SWT. SEPARATOR );
Final MenuItem itemRadio = new MenuItem (menu, SWT. RADIO );
ItemRadio. setText ("& RADIO item \ tCtrl + R ");
ItemRadio. setAccelerator (SWT. CTRL + 'R ');
ItemRadio. addListener (SWT. Selection, new Listener (){
Public void handleEvent (Event event ){
System. out. println ("item selected: RADIO item ");
System. out. println ("Selection:" + itemRadio. getSelection ());
}
});
ItemCascade. setMenu (menu );
// Shell. setMenu (menuBar );
Shell. setMenuBar (menuBar );
MenuBar. setDefaultItem (itemCascade );
Shell. setSize (300,120 );
Shell. open ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
Public static void main (String [] args ){
New MenuExamples ();
}
}

CoolBarExamples. javaCopy codeThe Code is as follows: package swt_jface.demo5;
Import java. io. DataInputStream;
Import java. io. DataOutputStream;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import java. io. IOException;
Import org. eclipse. swt. SWT;
Import org. eclipse. swt. graphics. Point;
Import org. eclipse. swt. layout. GridData;
Import org. eclipse. swt. layout. GridLayout;
Import org. eclipse. swt. widgets. Button;
Import org. eclipse. swt. widgets. Composite;
Import org. eclipse. swt. widgets. Control;
Import org. eclipse. swt. widgets. CoolBar;
Import org. eclipse. swt. widgets. CoolItem;
Import org. eclipse. swt. widgets. Display;
Import org. eclipse. swt. widgets. Event;
Import org. eclipse. swt. widgets. Label;
Import org. eclipse. swt. widgets. Listener;
Import org. eclipse. swt. widgets. Shell;
Import org. eclipse. swt. widgets. Text;
Public class CoolBarExamples {

Display display = new Display ();
Shell shell = new Shell (display );
Public CoolBarExamples (){

Shell. setLayout (new GridLayout ());
Final CoolBar coolBar = new CoolBar (shell, SWT. NONE );
CoolBar. setLayoutData (new GridData (GridData. FILL_HORIZONTAL ));
CoolItem textItem = new CoolItem (coolBar, SWT. NONE );
Text text = new Text (coolBar, SWT. BORDER | SWT. DROP_DOWN );
Text. setText ("TEXT ");
Text. pack ();
Point size = text. getSize ();
TextItem. setControl (text );
TextItem. setSize (textItem. computeSize (size. x, size. y ));
CoolItem labelItem = new CoolItem (coolBar, SWT. NONE );
Label label = new Label (coolBar, SWT. NONE );
Label. setText ("LABEL ");
Label. pack ();
Size = label. getSize ();
LabelItem. setControl (label );
LabelItem. setSize (textItem. computeSize (size. x, size. y ));
CoolItem buttonItem = new CoolItem (coolBar, SWT. NONE | SWT. DROP_DOWN );
Composite composite = new Composite (coolBar, SWT. NONE );
Composite. setLayout (new GridLayout (2, true ));
Button button1 = new Button (composite, SWT. PUSH );
Button1.setText ("Button 1 ");
Button1.pack ();
Button button2 = new Button (composite, SWT. PUSH );
Button2.setText ("Button 2 ");
Button2.pack ();
Composite. pack ();
Size = composite. getSize ();
ButtonItem. setControl (composite );
ButtonItem. setSize (buttonItem. computeSize (size. x, size. y ));
/// Test cool item adding method.
// Label label2 = new Label (coolBar, SWT. NONE );
// Label2.setText ("label2 ");
// AddControlToCoolBar (label2, SWT. DROP_DOWN, coolBar );
Try {
SetState (coolBar, new File ("coolbar. state "));
} Catch (IOException e1 ){
E1.printStackTrace ();
}
Shell. addListener (SWT. Close, new Listener (){
Public void handleEvent (Event event ){
Try {
SaveState (coolBar, new File ("coolbar. state "));
} Catch (IOException e ){
E. printStackTrace ();
}
}
});
Shell. setSize (300,120 );
// Shell. pack ();
Shell. open ();
While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
Public static CoolItem addControlToCoolBar (
Control control,
Int coolItemStyle,
CoolBar coolBar ){
CoolItem coolItem = new CoolItem (coolBar, coolItemStyle );
Point size = control. getSize ();
If (size. x = 0 & size. y = 0 ){
Control. pack ();
Size = control. getSize ();
}
CoolItem. setControl (control );
CoolItem. setSize (coolItem. computeSize (size. x, size. y ));
Return coolItem;
}

Private void saveState (CoolBar coolBar, File file) throws IOException {
DataOutputStream out = new DataOutputStream (new FileOutputStream (file ));
Try {
System. out. println ("Item order:" + intArrayToString (coolBar. getItemOrder ()));
Int [] order = coolBar. getItemOrder ();
Out. writeInt (order. length );
For (int I = 0; I <order. length; I ++)
Out. writeInt (order [I]);
System. out. println ("Wrap indices:" + intArrayToString (coolBar. getWrapIndices ()));
Int [] wrapIndices = coolBar. getWrapIndices ();
Out. writeInt (wrapIndices. length );
For (int I = 0; I <wrapIndices. length; I ++)
Out. writeInt (wrapIndices [I]);
Point [] sizes = coolBar. getItemSizes ();
Out. writeInt (sizes. length );
For (int I = 0; I <sizes. length; I ++ ){
Out. writeInt (sizes [I]. x );
Out. writeInt (sizes [I]. y );
}
} Finally {
Out. close ();
}
}

Private void setState (CoolBar coolBar, File file) throws IOException {
If (! File. exists ())
Throw new IOException ("File does not exist:" + file );
DataInputStream in = new DataInputStream (new FileInputStream (file ));
Try {
Int size = in. readInt ();
Int [] order = new int [size];
For (int I = 0; I <order. length; I ++)
Order [I] = in. readInt ();
Size = in. readInt ();
Int [] wrapIndices = new int [size];
For (int I = 0; I <wrapIndices. length; I ++)
WrapIndices [I] = in. readInt ();
Size = in. readInt ();
Point [] sizes = new Point [size];
For (int I = 0; I <sizes. length; I ++)
Sizes [I] = new Point (in. readInt (), in. readInt ());

CoolBar. setItemLayout (order, wrapIndices, sizes );
} Finally {
In. close ();
}
}
Public static String intArrayToString (int values []) {
StringBuffer sb = new StringBuffer ();
Sb. append ("{");
For (int I = 0; values! = Null & I <values. length; I ++ ){
Sb. append (values [I]);
If (I! = Values. length-1)
Sb. append (",");
}
Sb. append ("}");
Return sb. toString ();
}
Public static void main (String [] args ){
New CoolBarExamples ();
}
}

ToolBarExamples. java Copy codeThe Code is as follows: package swt_jface.demo5;
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. 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 ToolBarExamples {

Display display = new Display ();
Shell shell = new Shell (display );
ToolBar toolBar;

Public ToolBarExamples (){

ToolBar = new ToolBar (shell, SWT. FLAT | SWT. WRAP | SWT. RIGHT );
ToolItem itemPush = new ToolItem (toolBar, SWT. PUSH );
ItemPush. setText ("PUSH item ");
Image icon = new Image (shell. getDisplay (), "icons/new.gif ");
ItemPush. setImage (icon );
ToolItem itemCheck = new ToolItem (toolBar, SWT. CHECK );
ItemCheck. setText ("CHECK item ");
ToolItem itemRadio1 = new ToolItem (toolBar, SWT. RADIO );
ItemRadio1.setText ("RADIO item 1 ");
ToolItem itemRadio2 = new ToolItem (toolBar, SWT. RADIO );
ItemRadio2.setText ("RADIO item 2 ");
ToolItem itemSeparator = new ToolItem (toolBar, SWT. SEPARATOR );
Text text = new Text (toolBar, SWT. BORDER | SWT. SINGLE );
Text. pack ();
ItemSeparator. setWidth (text. getBounds (). width );
ItemSeparator. setControl (text );

Final ToolItem itemDropDown = new ToolItem (toolBar, SWT. DROP_DOWN );
ItemDropDown. setText ("DROP_DOWN item ");
ItemDropDown. 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 ");

ItemDropDown. addListener (SWT. Selection, new Listener (){
Public void handleEvent (Event event ){
If (event. detail = SWT. ARROW ){
Rectangle bounds = itemDropDown. getBounds ();
Point point = toolBar. toDisplay (bounds. x, bounds. y + bounds. height );
Menu. setLocation (point );
Menu. setVisible (true );
}
}
});
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 ());
}
};
ItemPush. addListener (SWT. Selection, selectionListener );
ItemCheck. addListener (SWT. Selection, selectionListener );
ItemRadio1.addListener (SWT. Selection, selectionListener );
ItemRadio2.addListener (SWT. Selection, selectionListener );
ItemDropDown. addListener (SWT. Selection, selectionListener );
ToolBar. pack ();

Shell. addListener (SWT. Resize, new Listener (){
Public void handleEvent (Event event ){
Rectangle clientArea = shell. getClientArea ();
ToolBar. setSize (toolBar. computeSize (clientArea. width, SWT. DEFAULT ));
}
});
Shell. setSize (500,100 );
Shell. open ();

While (! Shell. isDisposed ()){
If (! Display. readAndDispatch ()){
Display. sleep ();
}
}
Display. dispose ();
}
Public static void main (String [] args ){
New ToolBarExamples ();
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.