我正在为我的项目使用 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);
最佳答案
以下是我为使滚动复合正常工作而采取的解决方法。早些时候,滚动机制也不起作用+我无法设置背景。这是更新后的代码:
@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);
关于Java SWT - 设置 Canvas 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17375368/