Java SWT - 设置画布背景颜色

时间:2013-06-29 00:07:50

标签: java eclipse-plugin swt

我正在为我的项目使用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);

2 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

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

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

这篇文章对你很有启发性

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