SWT应用程序切换Shell中的Composite

为了这东西,搞的我头都疼了,找了好多书,看了很多贴子,都没说这个的。后来还是不小心弄出来的。

下面的一个例子,有一个Shell和两个Composite组成,每个Composite中各方一个Button,当点Button 时就切换到另一个Composte。过程是这样,在事件处理中创建待显示的Composite,然后摧毁当前的Composite,最后调用Shell的Layout()方法。

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

public class G {
	public static void main(String[] args) {
		Display d = Display.getDefault();
		Shell s = new Shell(d);
		new T(s, SWT.NONE);
		s.setLayout(new FillLayout());
		s.open();
		s.pack();

		while (!s.isDisposed()) {
			if (!d.readAndDispatch()) {
				d.sleep();
			}
		}
		d.dispose();
	}
}



class T extends Composite {
	public T(Composite p, int style) {
		super(p, style);
		this.setLayout(new FillLayout());
		final Button b = new Button(this, SWT.NONE);
		b.setText("dd");
		b.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell s = b.getShell();
				new R(s, SWT.NONE);
				System.out.println("yes");
				b.getParent().dispose();
				s.layout();
			}
		});
	}
}

class R extends Composite {
	public R(Composite p, int style) {
		super(p, style);
		this.setLayout(new FillLayout());
		final Button b = new Button(this, SWT.NONE);
		b.setText("ee");
		b.addSelectionListener(new SelectionAdapter() {
			
			@Override
			public void widgetSelected(SelectionEvent e) {
				Shell s = b.getShell();
				new T(s, SWT.NONE);
				b.getParent().dispose();
				s.layout();
			}
		});
	}
}



今天有人跟我说,用StackLayout布局会更简单方便,事实却是如此。


要有其他更好的方法,麻烦留个言,谢谢

你可能感兴趣的:(eclipse)