我在shell中加了一个list,还有两个composite(composite_1,composite_2),其中composite是用stackLayout布局的,然后在list中我加了两个选项:xxxxxx和yyyyyy,我想实现以下功能:
      用鼠标点xxxxxx,composite_1出现;点yyyyyy,composite_2出现。
      请问这个怎么实现?
还有个问题?
在eclipse中,SWT的Designed能加上鼠标的Listener吗?
谢谢!

解决方案 »

  1.   

    兄弟 我的问题跟你差不多,一起等高人出现吧,你可以看看我的贴子,虽然还没解决,可能对你有些帮助
    http://community.csdn.net/Expert/topic/4350/4350473.xml?temp=.1800501
      

  2.   

    /*
    * 创建日期 2005-10-25
    *
    * TODO 要更改此生成的文件的模板,请转至
    * 窗口 - 首选项 - Java - 代码样式 - 代码模板
    */
    package eclipse_study;import org.eclipse.swt.widgets.Composite;
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.custom.StackLayout;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Button;
    import org.eclipse.swt.layout.RowLayout;
    import org.eclipse.swt.widgets.Text;
    /**
    * @author stt
    *
    * TODO 要更改此生成的类型注释的模板,请转至
    * 窗口 - 首选项 - Java - 代码样式 - 代码模板
    */
    public class StackLayout1 {  private org.eclipse.swt.widgets.Shell sShell = null;
      private Composite composite = null;
      private Composite composite1 = null;
      private Button button1 = null;
      private Button button2 = null;
      private Text text1 = null;
      private Text text2 = null;
      private StackLayout stackLayout = new StackLayout();
      /**
       * This method initializes sShell
       */
      private void createSShell() {
        sShell = new org.eclipse.swt.widgets.Shell();     
        createComposite();
        createComposite1();
        sShell.setText("Shell");
        sShell.setLayout(new FillLayout());
        sShell.setSize(new org.eclipse.swt.graphics.Point(300,200));
      }
      /**
       * This method initializes composite  
       *
       */ 
      private void createComposite() {
        composite = new Composite(sShell, SWT.NONE);
        composite.setLayout(stackLayout);
        text1 = new Text(composite, SWT.BORDER);
        text2 = new Text(composite, SWT.BORDER);
        text1.setBounds(new org.eclipse.swt.graphics.Rectangle(28,91,72,20));
        text1.setText("text1");
        text2.setBounds(new org.eclipse.swt.graphics.Rectangle(35,123,72,20));
        text2.setText("text2");
        
      }
      /**
       * This method initializes composite1  
       *
       */ 
      private void createComposite1() {
        composite1 = new Composite(sShell, SWT.NONE);     
        button1 = new Button(composite1, SWT.NONE);
        button2 = new Button(composite1, SWT.NONE);
        button1.setText("button1");
        button2.setText("button2");
        composite1.setLayout(new RowLayout());
        button1.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() { 
          public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) { 
            System.out.println("widgetSelected()"); // TODO Auto-generated Event stub widgetSelected()
             stackLayout.topControl = text1; //将text1框显示
             composite.layout(); //将界面刷新一下,否则显示上会没任何反映的
          }
        });
      }
    }
      

  3.   

    为你的list添加一个MouseListener不就OK了.
      

  4.   

    TO:callzf()
         这个我好像在《eclipse入门与精通》里看过,我想实现的是list的,不是button的
    list怎么用mouselistener实现?
      

  5.   

    把这段代码换我楼上那段就OK了,自我感觉方法有点土    private void createComposite1() {
            composite1 = new Composite(sShell, SWT.NONE);
            list = new List(composite1, SWT.NONE);
            list.setBounds(new org.eclipse.swt.graphics.Rectangle(19, 89, 67, 64));
            list.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
                        public void widgetSelected( org.eclipse.swt.events.SelectionEvent e) {
                           
                           System.out.println(list.getSelectionIndex());//打印选择列表后的数值
                           int aaa=list.getSelectionIndex();
                           if (aaa==0) {//如果选择LIST第一项... 
                               stackLayout.topControl = text; //将text1框显示
                               composite.layout(); 
                           }
                           else{
                               stackLayout.topControl = text1; //将text1框显示
                               composite.layout(); 
                           }
                        }
                    });
            list.add("123", 0); //增加列表内容
            list.add("456", 1);
        }