在swt中,我有两个类,用popmenu和label做成一个组合的memu,想要达到windows menu的效果。但是现在有一个问题,就是在一个label上click之后,popmenu会显示,但是在鼠标进入下一个label的时候,下一个label应该变亮,但是本人不知道如何实现,请高手帮忙。
请访问[email protected] conger下载两个类。

解决方案 »

  1.   

    import org.eclipse.swt.graphics.Image;
    import org.eclipse.swt.graphics.Point;
    import org.eclipse.swt.widgets.*;
    import org.eclipse.swt.SWT;
    //import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.widgets.MenuItem;
    public class swtmenu {
     private Shell sShell = null; // @jve:decl-index=0:visual-constraint="46,5"
     private Text text = null;
     private Display display = null;
     // private Menu menuBar = null;
     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      /*
       * Before this is run, be sure to set up the launch configuration
       * (Arguments->VM Arguments) for the correct SWT library path in order
       * to run with the SWT dlls. The dlls are located in the SWT plugin jar.
       * For example, on Windows the Eclipse SWT 3.1 plugin jar is:
       * installation_directory\plugins\org.eclipse.swt.win32_3.1.0.jar
       */
      Display display = Display.getDefault();
      swtmenu thisClass = new swtmenu();
      thisClass.createSShell();
      thisClass.sShell.open();
      while (!thisClass.sShell.isDisposed()) {
       if (!display.readAndDispatch())
        display.sleep();
      }
      display.dispose();
     }
     /**
      * This method initializes sShell
      */
     private void createSShell() {
      sShell = new Shell();
      sShell.setText("menu");
      sShell.setSize(new Point(300, 200));
      text = new Text(sShell, SWT.BORDER);
      text.setBounds(new org.eclipse.swt.graphics.Rectangle(151, 62, 71, 19));
      // 菜单头
      Menu menuBar = new Menu(sShell, SWT.BAR);
      menuBar.setVisible(true);
      MenuItem fileMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
      fileMenuHeader.setText("&File");
      MenuItem editMenuHeader = new MenuItem(menuBar, SWT.CASCADE);
      editMenuHeader.setText("&Edit");
      // 对应的下拉菜单
      Menu dropMenu1 = new Menu(sShell, SWT.DROP_DOWN);
      fileMenuHeader.setMenu(dropMenu1);
      MenuItem dropitem1 = new MenuItem(dropMenu1, SWT.PUSH);
      dropitem1.setText("open");
      MenuItem dropitem2 = new MenuItem(dropMenu1, SWT.PUSH);
      dropitem2.setText("save");
      sShell.setMenuBar(menuBar);
      // 工具栏
      ToolBar toolbar = new ToolBar(sShell, SWT.FLAT);
      toolbar.setSize(300, 65);
      toolbar.setLocation(0, 0);
      ToolItem item1 = new ToolItem(toolbar, SWT.PUSH);
    /*  Image icon1 = new Image(display, "d:\\gif\\1.gif");
      item1.setImage(icon1);*/
      item1.setToolTipText("write");
      ToolItem item2 = new ToolItem(toolbar, SWT.PUSH);
     /* Image icon2 = new Image(display, "d:\\gif\\1.gif");
      item2.setImage(icon2);*/
      item2.setToolTipText("list");
      // 添加的菜单事件
      dropitem1
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
         public void widgetSelected(
           org.eclipse.swt.events.SelectionEvent e) {
          text.setText("has opened"); // TODO Auto-generated Event
                 // stub widgetSelected()
         }
        });
      dropitem2
        .addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
         public void widgetSelected(
           org.eclipse.swt.events.SelectionEvent e) {
          text.setText("has saved"); // TODO Auto-generated Event
                 // stub widgetSelected()
         }
        });
     }
    }