Java 使用SWT创建GUI时发生Event.gc错误
我在使用PaintListeners和 SWT中的GCs。如果有必要,我也可以发布主方法,但该方法所做的只是生成一个输入屏幕,然后打开下面的shell。我认为错误在SimDisp中 使用下面网站上的类,使用相同的过程,实际显示了一个矩形 有人知道为什么GC=event.GC;是否产生错误?-gc无法解析或不是字段Java 使用SWT创建GUI时发生Event.gc错误,java,events,user-interface,garbage-collection,swt,Java,Events,User Interface,Garbage Collection,Swt,我在使用PaintListeners和 SWT中的GCs。如果有必要,我也可以发布主方法,但该方法所做的只是生成一个输入屏幕,然后打开下面的shell。我认为错误在SimDisp中 使用下面网站上的类,使用相同的过程,实际显示了一个矩形 有人知道为什么GC=event.GC;是否产生错误?-gc无法解析或不是字段 import java.awt.event.PaintEvent; import org.eclipse.swt.SWT; import org.eclipse.swt.eve
import java.awt.event.PaintEvent;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import java.util.ResourceBundle;
public static Shell SimDisp (Shell s1){
final Shell shell2 = new Shell(s1.getDisplay());
shell2.setText("Linear Magnetic Accelerator Simulation");
/*
Canvas canvas = new Canvas(shell2, SWT.NONE);
LightweightSystem lws = new LightweightSystem(canvas);
RectangleFigure rect = new RectangleFigure();
rect.setBounds(new org.eclipse.draw2d.geometry.Rectangle(20,20,100,100));
rect.setBackgroundColor(ColorConstants.green);
rect.setForegroundColor(ColorConstants.gray);
lws.setContents(rect);
*/
/*
Canvas canvas = new Canvas(shell2, SWT.NONE);
Rectangle rect = new Rectangle (20,20,100,100);
GC gc = new GC (canvas);
gc.drawRectangle(rect);
*/
final Color red = new Color(s1.getDisplay(), 0xFF, 0, 0);
shell2.addPaintListener(new PaintListener() {
public void paintControl(PaintEvent event) {
GC gc = event.gc; // This line produces an error - gc cannot be resolved or is not a field.
gc.setForeground(red);
Rectangle rect = shell2.getClientArea();
gc.drawRectangle(rect.x + 10, rect.y + 10, rect.width - 20,
rect.height - 20);
gc.drawString("Hello_world", rect.x + 20,
rect.y + 20);}
@Override
public void paintControl(org.eclipse.swt.events.PaintEvent e) {
// TODO Auto-generated method stub
}});
return shell2;}
因为您导入了
java.awt.event.PaintEvent
而不是org.eclipse.swt.events.PaintEvent
谢谢!我完全错过了。