通过事件驱动,创建不同的部件

非常easy的东西,就是先dispose再create。

 

写出来的主要是看看大家有没有更好的实现方法

 

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CCombo;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
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.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
/**
 * 该程序目的:通过事件驱动,简单演示动态创建组件。
 * 选择“是”的时候:供选择的是下拉框
 * 选择“否”的时候:是进行具体的文本配置
 * @author Administrator
 */
public class MyComposite {

	private  Button useOutSourceButton;
	private  Button newSourceButton;
	private  Composite dbSourceComposite;
	private  Shell shell;

	/**
	 * 主程序
	 * @param args
	 */
	public static void main(String[] args) {
		MyComposite myComposite = new MyComposite();

		myComposite.open();
	}

	/**
	 * shell
	 */
	private void open() {
		Display display = Display.getDefault();
		shell = new Shell(display);
		shell.setText("组件替换测试");
		shell.setSize(400, 200);
		shell.setLayout(new GridLayout(3, false));

		final Label label = new Label(shell, SWT.NONE);
		label.setLayoutData(new GridData(100, SWT.DEFAULT));
		label.setText("使用数据库资源");

		useOutSourceButton = new Button(shell, SWT.RADIO);
		useOutSourceButton.setText("是");
		newSourceButton = new Button(shell, SWT.RADIO);
		newSourceButton.setText("否");

		dbSourceComposite = new Composite(shell, SWT.NONE);
		dbSourceComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,true, false,3,1));
		dbSourceComposite.setLayout(new GridLayout(3,false));

		createInnerListeners();

		shell.open();
		while (!display.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();

	}

	/**
	 * 事件监听器
	 */
	protected void createInnerListeners() {
		useOutSourceButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				Button btn = (Button) e.getSource();
				if (btn.getSelection()) {
					initSelectDBSourceUI(dbSourceComposite);
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
		newSourceButton.addSelectionListener(new SelectionListener() {
			public void widgetSelected(SelectionEvent e) {
				Button btn = (Button) e.getSource();
				if (btn.getSelection()) {
					initConfigDBSourceUI(dbSourceComposite);
				}
			}

			public void widgetDefaultSelected(SelectionEvent e) {
			}
		});
	}

	/**
	 * 创建部件组1
	 * @param composite
	 */
	private void initConfigDBSourceUI(final Composite composite) {
		this.disposedComposite(composite);
		Text configureText = new Text(composite, SWT.BORDER);
		configureText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false));
		configureText.setEditable(false);
		Button configureButton = new Button(composite, SWT.NONE);
		configureButton.setText("配置");
		Button resetButton = new Button(composite, SWT.NONE);
		resetButton.setText("重置");
		shell.layout(true, true);
	}

	/**
	 * 创建部件组2
	 * @param composite
	 */
	private void initSelectDBSourceUI(final Composite composite) {
		this.disposedComposite(composite);
		Label dbTypeLabel = new Label(composite, SWT.NONE);
		dbTypeLabel.setText("数据库资源名称");
		dbTypeLabel.setLayoutData(new GridData(100, SWT.DEFAULT));
		dbTypeLabel.setToolTipText("选择已有数据库资源");

		CCombo dbCombo = new CCombo(composite, SWT.BORDER);
		dbCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		dbCombo.setEditable(false);

		Button resetButton = new Button(composite, SWT.NONE);
		resetButton.setText("重置");
		shell.layout(true, true);
	}
	
	/**
	 * 销毁原有部件
	 * @param composite
	 */
	private void disposedComposite(Composite composite) {
		Control[] arrays = composite.getChildren();
		for (Control control : arrays) {
			if (control != null)
				control.dispose();
		}
	}
}

 

 效果图:



 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值