【发布时间】:2014-01-26 12:35:44
【问题描述】:
我有以下代码:
final Canvas canvas = new Canvas(mainshell, SWT.NO_REDRAW_RESIZE);
canvas.setBounds(0, 0, mainshell.getSize().x, mainshell.getSize().y);
canvas.setBackgroundImage( new Image(display, "BlueBack.jpg" ));
canvas.setFont(font);
GC gc = new GC(canvas);
gc.drawText("Test", 0, 0, true);
gc.dispose();
canvas.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent e) {
e.gc.drawText("String", 170, 30, true);
e.gc.drawText("Another Unimportant String", 80, 90, true);
int I = 140;
int i = 1;
String[] strings = Stuff.getUnimportantStringArray();
if(strings != null)
for(String string : strings){
e.gc.drawText( i + " " + string , 120, I, true);
I += 50;
i++;
}
}
});
我遇到问题的代码是这样的:
GC gc = new GC(canvas);
gc.drawText("Test", 0, 0, true);
gc.dispose();
gc.drawText(); 没有像我预期的那样在画布上绘制字符串"Test"。
这是我的问题:
为什么gc.drawText("Test", 0, 0, true"); 不工作,但PaintListener 中的e.gc.drawText("String", 170, 30, true); 工作?
【问题讨论】:
-
首先,这与垃圾收集无关,而是与 SWT 的 GraphicsContext 有关。其次,文本似乎被听众绘制的内容或画布的背景所覆盖。也许您可以尝试此introduction to SWT 中提供的
SWT标志之一
标签: java canvas garbage-collection swt drawtext