3

我正在为我的项目使用 SWT Canvas。我的问题是我无法为其设置背景颜色。这是我的代码。无论我为背景提供什么颜色,当我运行我的插件时,我只会得到浅灰色的默认背景颜色。有人可以帮我吗?

谢谢!

@Override
public void createPartControl(Composite parent) {

    scParent = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scParent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    scParent.setExpandHorizontal(true);
    scParent.setExpandVertical(true);

    // Create Canvas to hold the table
    tableCanvas = new Canvas(scParent, SWT.H_SCROLL | SWT.V_SCROLL);
        tableCanvas.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
    gd.heightHint = 116;
    gd.horizontalSpan = 3;
    tableCanvas.setLayoutData(gd);
4

2 回答 2

1

这是我为滚动合成正常工作所做的解决方法。早些时候,滚动机制也不起作用+我无法设置背景。这是更新的代码:

@Override
public void createPartControl(Composite parent) {
    parent.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    parent.setLayout(new FillLayout(SWT.HORIZONTAL));

    ScrolledComposite scrolledComposite = new ScrolledComposite(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
    scrolledComposite.setBackground(SWTResourceManager.getColor(SWT.COLOR_WHITE));
    scrolledComposite.setExpandHorizontal(true);
    scrolledComposite.setExpandVertical(true);
    scrolledComposite.setMinWidth(400);
    scrolledComposite.setMinHeight(400);

    Composite myViewParent = new Composite(scrolledComposite, SWT.NONE);
    myViewParent.setBackground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
    myViewParent.setLayout(null);

    Button btnNewButton = new Button(myViewParent, SWT.NONE);
    btnNewButton.setBounds(45, 237, 90, 30);
    btnNewButton.setText("New Button");

    scrolledComposite.setContent(myViewParent);
    scrolledComposite.setMinSize(myViewParent.computeSize(SWT.DEFAULT, SWT.DEFAULT));
    parent.setSize(600, 300);
于 2013-07-01T18:38:35.860 回答
0

像这样例如将背景颜色设置为蓝色。

tableCanvas.setBackground(tableCanvas.getDisplay().getSystemColor(SWT.COLOR_BLUE)); 

这篇文章对你很有指导意义

http://eclipse.org/articles/Article-SWT-graphics/SWT_graphics.html#Canvas

于 2013-06-29T12:17:37.230 回答