使用SWT创建GUI时出现Event.gc错误

时间:2012-01-10 14:33:20

标签: java events user-interface garbage-collection swt

我在使用PaintListeners打印矩形到shell时遇到问题 SWT中的GC。如果有必要,我也可以发布main方法,但所有方法都会产生一个输入屏幕然后打开下面的shell。我认为错误发生在SimDisp中。

使用下面网站上的类,使用了相同的过程,实际显示了一个矩形。

http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/HowtodrawdirectlyonanSWTControl.htm

有谁知道为什么GC gc = event.gc;产生错误? - gc无法解析或不是字段。

 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;}

1 个答案:

答案 0 :(得分:2)

因为您导入了java.awt.event.PaintEvent而不是org.eclipse.swt.events.PaintEvent