swt composite 上控件之间有没有上下层的关系
1个回答
展开全部
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();
}
}
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();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询