2013-06-29 363 views
3

我正在为我的项目使用SWT画布。我的问题是我无法为它设置背景颜色。这是我的代码。无论我给背景添加什么颜色,运行我的插件时,我都只能获得浅灰色的默认背景色。有人可以帮助我吗?Java SWT - 设置画布背景颜色

谢谢!

@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); 

回答

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); 
0

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

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

这篇文章将是非常有启发你

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

+0

感谢您的回答。该教程确实非常有用。不幸的是,它并不适合我。我做了一个工作,最终效果很好。我会把它作为答案。 – user2033666