[英]Java SWT GC not drawing text on Canvas
I have the following code: 我有以下代码:
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++;
}
}
});
The code that I am having the problem with is this: 我遇到问题的代码是这样的:
GC gc = new GC(canvas);
gc.drawText("Test", 0, 0, true);
gc.dispose();
The gc.drawText();
gc.drawText();
is not drawing the String "Test"
on the canvas as I expected it would. 没有像我预期的那样在画布上绘制字符串
"Test"
。
Here is my Question: 这是我的问题:
Why is the gc.drawText("Test", 0, 0, true");
not working, but the e.gc.drawText("String", 170, 30, true);
inside the PaintListener
is working ? 为什么
gc.drawText("Test", 0, 0, true");
不起作用,但是e.gc.drawText("String", 170, 30, true);
在PaintListener
里面工作?
The paintControl()
method is called whenever the canvas needs to be redrawn which could happen any number of times. 每当需要重
paintControl()
布时调用paintControl()
方法,这可能会发生任何次数。 For example, the PaintListener
is used when the canvas is resized (try putting a breakpoint on paintControl()
, resize the window, and see for yourself). 例如,在调整画布大小时使用
PaintListener
(尝试在paintControl()
上放置断点,调整窗口大小,并亲自查看)。 All drawing you need to do should be in the PaintListener
. 您需要做的所有绘图应该在
PaintListener
。
Also, you shouldn't use setBounds()
here. 此外,您不应在此处使用
setBounds()
。 Use a layout instead: 改为使用布局:
mainShell.setLayout(new FillLayout());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.