【问题标题】:Eclipse/SWT: Rectangle taking up entire canvasEclipse/SWT:矩形占据整个画布
【发布时间】:2010-07-26 18:04:33
【问题描述】:

我正在编写一个基于 Eclipse RCP 的应用程序,并试图在 ViewPart 上绘制一个矩形。但是,即使指定边界,矩形似乎也占据了整个屏幕。以下是我的代码。

public void createPartControl(Composite parent) {
        Shell shell = parent.getShell();

        Canvas canvas = new Canvas(parent, SWT.NONE);
        LightweightSystem lws = new LightweightSystem(canvas);
        RectangleFigure rectangle = new RectangleFigure();
        rectangle.setBounds(new Rectangle(0, 0, 10, 10));
        rectangle.setBackgroundColor(ColorConstants.green);
        lws.setContents(rectangle);
}

【问题讨论】:

    标签: java eclipse swt rcp draw2d


    【解决方案1】:

    我没有使用过 Draw2D,但我尝试通过创建另一个矩形图形并将其添加到第一个图形来修改您的示例,然后出现了。即

    // from your code
    rectangle.setBackgroundColor(ColorConstants.green);
    
    // new code
    RectangleFigure r2 = new RectangleFigure();
    r2.setBounds(new Rectangle(0,0,10,10));
    r2.setBackgroundColor(ColorConstants.blue);
    rectangle.add(r2);
    
    // back to your code
    lws.setContents(rectangle);
    

    对我来说看起来不错 - 在全绿色画布的左上角有一个蓝色的小矩形。我猜你用作画布内容的图形默认情况下(并且可能是必要的)占据了整个画布。

    【讨论】:

    • 感谢您的帮助,Ladlestein。你似乎是对的。无论如何,第一个矩形占据了整个画布。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-19
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-03
    相关资源
    最近更新 更多