现在我正在尝试使用以下代码更改 org.eclipse.swt.widgets.Button 的背景颜色:
Button sceneButton = new Button(border, SWT.TOGGLE | SWT.FLAT); sceneButton.setBackground(Color.RED);
当我在 Solaris 中运行该程序时,这工作正常,但当我在 Windows 中运行代码时,这不起作用。这可能吗?如果没有,是否有某种解决方法可以让我更改背景颜色(即使“颜色”是图像),同时仍在按钮中显示文本?谢谢!
最佳答案
在 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);
}
});
关于java - 在 Windows 中更改 org.eclipse.swt.widgets 背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21988996/