Java by API/org.eclipse.swt.widgets/MenuItem

Материал из Java эксперт
Перейти к: навигация, поиск

MenuItem: addListener(int state, Listener lis)

import org.eclipse.swt.SWT;
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.Text;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
public class MainClass {
  public static void main(String[] args) {
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setSize(300, 200);
    ToolBar toolbar = new ToolBar(shell, SWT.NONE);
    ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    item1.setText("Now");
    ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
    item2.setText("is");
    ToolItem item3 = new ToolItem(toolbar, SWT.PUSH);
    item3.setText("the");
    ToolItem item4 = new ToolItem(toolbar, SWT.PUSH);
    item4.setText("time");
    toolbar.setBounds(0, 0, 200, 70);
    final Text text = new Text(shell, SWT.BORDER);
    text.setBounds(0, 100, 200, 25);
    Listener listener = new Listener() {
      public void handleEvent(Event event) {
        ToolItem item = (ToolItem) event.widget;
        String string = item.getText();
        if (string.equals("Now"))
          text.setText("You selected: Now");
        else if (string.equals("is"))
          text.setText("You selected: is");
        else if (string.equals("the"))
          text.setText("You selected: the");
        else if (string.equals("time"))
          text.setText("You selected: time");
      }
    };
    item1.addListener(SWT.Selection, listener);
    item2.addListener(SWT.Selection, listener);
    item3.addListener(SWT.Selection, listener);
    item4.addListener(SWT.Selection, listener);
    shell.open();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
    display.dispose();
  }
}





MenuItem: addSelectionListener(SelectionListener lis)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s, SWT.BAR);
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("&Open");
    openItem.setAccelerator(SWT.CTRL + "O");
    openItem.addSelectionListener(new Open());
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}
class Open implements SelectionListener {
  public void widgetSelected(SelectionEvent event) {
    System.out.println(event);
  }
  public void widgetDefaultSelected(SelectionEvent event) {
  }
}





MenuItem: setAccelerator(int key)

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s, SWT.BAR);
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("&Open");
    openItem.setAccelerator(SWT.CTRL + "O");
    openItem.addSelectionListener(new Open());
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}
class Open implements SelectionListener {
  public void widgetSelected(SelectionEvent event) {
    System.out.println(event);
  }
  public void widgetDefaultSelected(SelectionEvent event) {
  }
}





MenuItem: setMenu(Menu menu)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s,SWT.BAR );
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("Open");
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    
    s.setMenuBar(m);
    s.open();
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}





MenuItem: setText(String s)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s,SWT.BAR );
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("Open");
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    
    s.setMenuBar(m);
    s.open();
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}





new MenuItem(MenuItem filemenu, SWT.SEPARATOR)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s, SWT.BAR);
    // create a file menu and add an exit item
    final MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    final MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("Open");
    final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
    final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new MenuItem(Menu menu, int style)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    s.setText("A Shell Menu Example");
    Menu m = new Menu(s,SWT.BAR );
    MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    MenuItem openItem = new MenuItem(filemenu, SWT.PUSH);
    openItem.setText("Open");
    MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    
    s.setMenuBar(m);
    s.open();
    while(!s.isDisposed()){
        if(!d.readAndDispatch())
            d.sleep();
    }
    d.dispose();
  }
}





new MenuItem(Menu m, SWT.CASCADE)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Menu m = new Menu(s, SWT.BAR);
    // create a file menu and add an exit item
    final MenuItem file = new MenuItem(m, SWT.CASCADE);
    file.setText("File");
    final Menu filemenu = new Menu(s, SWT.DROP_DOWN);
    file.setMenu(filemenu);
    // create an open menu and to sub-menu items
    final MenuItem openItem = new MenuItem(filemenu, SWT.CASCADE);
    openItem.setText("Open");
    final Menu submenu = new Menu(s, SWT.DROP_DOWN);
    openItem.setMenu(submenu);
    final MenuItem childItem = new MenuItem(submenu, SWT.PUSH);
    childItem.setText("Child");
    final MenuItem dialogItem = new MenuItem(submenu, SWT.PUSH);
    dialogItem.setText("Dialog");
    // add a separator
    final MenuItem separator = new MenuItem(filemenu, SWT.SEPARATOR);
    // create an exit menu item
    final MenuItem exitItem = new MenuItem(filemenu, SWT.PUSH);
    exitItem.setText("Exit");
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}





new MenuItem(Menu optionsmenu, SWT.RADIO)

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
public class MainClass {
  public static void main(String[] a) {
    Display d = new Display();
    Shell s = new Shell(d);
    Menu m = new Menu(s, SWT.BAR);
    final MenuItem options = new MenuItem(m, SWT.CASCADE);
    options.setText("&Options");
    final Menu optionsmenu = new Menu(s, SWT.DROP_DOWN);
    options.setMenu(optionsmenu);
    final MenuItem checkItem = new MenuItem(optionsmenu, SWT.CHECK);
    checkItem.setText("&Checked Option");
    final MenuItem optionsseparator = new MenuItem(optionsmenu, SWT.SEPARATOR);
    final MenuItem radioItem1 = new MenuItem(optionsmenu, SWT.RADIO);
    radioItem1.setText("Radio &One");
    final MenuItem radioItem2 = new MenuItem(optionsmenu, SWT.RADIO);
    radioItem2.setText("Radio &Two");
    s.setMenuBar(m);
    s.open();
    while (!s.isDisposed()) {
      if (!d.readAndDispatch())
        d.sleep();
    }
    d.dispose();
  }
}