Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Java GUI 设计 » SWT  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 请教SWT中的一个问题
starc





发贴: 3
积分: 0
于 2006-05-18 22:33 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
package GUI;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class Starc {

  Composite cp1,cp2;
  
  /**
   * Launch the application
   * @param args
   */
  public static void main(String[] args) {
    try {
      Starc window = new Starc();
      window.open();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * Open the window
   */
  public void open() {
    final Display display = Display.getDefault();
    final Shell shell = new Shell();
    shell.setSize(500, 375);
    shell.setLayout(new FillLayout());

    SashForm sashForm = new SashForm(shell, SWT.NONE);
    cp1= new Composite(sashForm, SWT.BORDER);
    cp1.setLayout(new FillLayout());
    cp2 = new Composite(sashForm, SWT.BORDER);
    cp2.setLayout(new FillLayout());
    sashForm.setWeights(new int[] { 1, 5 });

    Button button=new Button(cp1,SWT.NONE);
    button.setText("按钮1");
    button.addSelectionListener(new SelectionAdapter(){
      public void widgetSelected(SelectionEvent e){
        Button button2=new Button(cp2,SWT.NONE);
        button2.setText("按钮2");
        cp2.layout();
      }
    });

    shell.open();
    shell.layout();
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }
  }
  
}
求教一下
上面代码当我每点击一次按钮1则在cp2中新增加一个按钮
有没有方法使单击一次按钮1时 cp2先清空即删掉原来的按钮,再增加一个按钮



作者 Re:请教SWT中的一个问题 [Re:starc]
yipsilon





发贴: 248
积分: 31
于 2006-05-19 07:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你可以在创建按钮时把按钮对象缓存, 然后在新建按钮前先把以前的按钮释放即可, 例如:

button.addSelectionListener(new SelectionAdapter(){

private Button button;

public void widgetSelected(SelectionEvent e){

//释放按钮资源.
if(button != null){
button.dispose();
button = null;
}

button = new Button(cp2,SWT.NONE);
button.setText("按钮2");
cp2.layout();
}

});



中国Eclipse社区,打造中国最专业的Eclipse开发者社区!!

地址:http://www.eclipseworld.org
作者 Re:请教SWT中的一个问题 [Re:starc]
starc





发贴: 3
积分: 0
于 2006-05-19 12:19 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
解决问题了
谢谢二楼的




flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923