在Windows中更改org.eclipse.swt.widgets背景颜色

Dor*_*own 7 java user-interface swt colors

现在我正在尝试使用以下代码更改org.eclipse.swt.widgets.Button的背景颜色:

    Button sceneButton = new Button(border, SWT.TOGGLE | SWT.FLAT);  
    sceneButton.setBackground(Color.RED);

这在我在Solaris中运行程序时工作正常,但在Windows中运行代码时什么都不做.这可能吗?如果没有,是否有某种解决方法可以让我更改背景颜色(即使"颜色"是图像),同时仍然在按钮中显示文字?谢谢!

小智 13

在Windows操作系统button.setBackGround上不能直接工作.一小段代码可以提供帮助.覆盖按钮的绘制事件,如下所示: -

----- obj是下面代码段中的按钮名称------------

obj.addPaintListener(new PaintListener() {
@Override
    public void paintControl(PaintEvent arg0) {
    // TODO Auto-generated method stub
    obj.setBackground(display.getSystemColor(SWT.COLOR_BLACK));
    org.eclipse.swt.graphics.Pattern pattern;
    pattern = new org.eclipse.swt.graphics.Pattern(arg0.gc.getDevice(), 0,0,0,100, arg0.gc.getDevice().getSystemColor(SWT.COLOR_GRAY),230, arg0.gc.getDevice().getSystemColor(SWT.COLOR_BLACK),230);
    arg0.gc.setBackgroundPattern(pattern);
    arg0.gc.fillGradientRectangle(0, 0, obj.getBounds().width, obj.getBounds().height, true);
    }
});
Run Code Online (Sandbox Code Playgroud)


Tru*_*oft 5

你不能.在方法的文档中Control.setBackground(),提到:

For example, on Windows the background of a Button cannot be changed.

  • 也许带有按钮图像的标签用您想要的颜色着色.当您点击"按钮"时会有额外的图像. (2认同)