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布局会更简单方便,事实却是如此。


要有其他更好的方法,麻烦留个言,谢谢
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值