swt composite 上控件之间有没有上下层的关系

 我来答
藤举0jG
2016-09-27 · TA获得超过110个赞
知道小有建树答主
回答量:237
采纳率:50%
帮助的人:118万
展开全部
try this

public class TestEvent {
Image image;
static int count;
static int colorindex = SWT.COLOR_BLUE;

public void runTest() {
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setMinimumSize(500,500);
shell.setLayout(new FillLayout());
final Composite composite = new Composite(shell, SWT.NONE);
final StackLayout layout = new StackLayout();
composite.setLayout(layout);
final Canvas canvasImage = new Canvas(composite, SWT.NONE);
final Canvas canvasCross = new Canvas(composite, SWT.NO_BACKGROUND);
layout.topControl = canvasCross;
composite.layout();
canvasImage.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent ex) {
System.out.println("Draw");
count++;
if(image==null || count>100){
if(colorindex==SWT.COLOR_BLUE)
colorindex = SWT.COLOR_RED;
else
colorindex=SWT.COLOR_BLUE;
if(count>100)
count=0;
GC myGC = new GC(canvasImage);
System.out.println(myGC.handle);
myGC.setBackground(display.getSystemColor(colorindex));
myGC.fillOval(30,30,200,100);
image = new Image(display, canvasImage.getClientArea().width, canvasImage.getClientArea().height);
image.type = SWT.BITMAP;
myGC.copyArea(image, 0,0);
myGC.drawImage(image, 0, 0);
myGC.dispose();
canvasImage.setBackgroundImage(image);
}
}
});

canvasCross.addMouseMoveListener(new MouseMoveListener() {
public void mouseMove(final MouseEvent e) {
System.out.println("moved");
layout.topControl = canvasImage;
composite.layout();
canvasImage.redraw();
canvasImage.update();
composite.update();
layout.topControl = canvasCross;
composite.layout();
GC gc = new GC(canvasCross);
gc.drawLine(e.x, e.y - 2000, e.x, e.y + 2000);
gc.drawLine(e.x - 2000, e.y, e.x + 2000, e.y);
gc.dispose();
}
});

shell.pack();
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式