java 在 Windows 中更改 org.eclipse.swt.widgets 背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3312102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Changing org.eclipse.swt.widgets background color in Windows
提问by Dorrene Brown
Right now I am trying to change the background color of a org.eclipse.swt.widgets.Button with the following code:
现在我正在尝试使用以下代码更改 org.eclipse.swt.widgets.Button 的背景颜色:
Button sceneButton = new Button(border, SWT.TOGGLE | SWT.FLAT);
sceneButton.setBackground(Color.RED);
This works fine when I run the program in Solaris, but does nothing when I run the code in Windows. Is this possible? If not, is there some kind of workaround that would allow me to change the background color (even if the "color" is an image) while still displaying text in the button? Thanks!
当我在 Solaris 中运行程序时,这工作正常,但当我在 Windows 中运行代码时,它什么也没做。这可能吗?如果没有,是否有某种解决方法可以让我在仍然在按钮中显示文本的同时更改背景颜色(即使“颜色”是图像)?谢谢!
采纳答案by True Soft
You can't. In the documentation of method Control.setBackground(), it is mentioned:
你不能。在 method 的文档中Control.setBackground(),提到了:
For example, on Windows the background of a Button cannot be changed.
For example, on Windows the background of a Button cannot be changed.
回答by Shishir Pandey
On windows operating systems button.setBackGrounddoesn't work directly. A small snippet of code can help. Override the paint event of button as shown below:-
在 Windows 操作系统button.setBackGround上不能直接工作。一小段代码可以提供帮助。覆盖按钮的绘制事件,如下所示:-
-----obj is button name in below snippet------------
-----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);
}
});
回答by Zoot
The background of a button in Windows is set from outside of SWT.
Windows 中按钮的背景是从 SWT 外部设置的。
Right-click your desktop, click Properties.
右键单击您的桌面,单击“属性”。
Go to the "Appearance" tab.
转到“外观”选项卡。
Click "Advanced".
单击“高级”。
I believe "3D objects" determines the button background. This is determined by each user's theme.
我相信“3D 对象”决定了按钮背景。这是由每个用户的主题决定的。


One great thing about SWT is it uses the underlying system widgets and themes. A frustrating thing about SWT is it uses the underlying system widgets and themes.
SWT 的一大优点是它使用底层系统小部件和主题。SWT 令人沮丧的一点是它使用了底层系统小部件和主题。
回答by ctg
You can simulate a button using CLabel. Add a mouselistener to change the background on mouse down and mouse up, and in the mouse up event dispatch a selection listener event so that it behaves the same as a button. For example:
您可以使用 CLabel 模拟按钮。添加鼠标侦听器以更改鼠标按下和抬起时的背景,并在鼠标抬起事件中调度选择侦听器事件,使其行为与按钮相同。例如:
Color bg = ...
Color shadow = ...
CLabel simulatedButton = new CLabel(parent, SWT.PUSH);
simulatedButton.setBackground(bg);
simulatedButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseUp(MouseEvent e) {
simulatedButton.setBackground(bg);
notifyListeners(SWT.Selection, new Event());
}
@Override
public void mouseDown(MouseEvent e) {
simulatedButton.setBackground(shadow);
}
});
This briefly changes the background of the button while you are pressing the mouse to give the effect of clicking a button. CLabel can also be extended, unlike other SWT widgets, so you can create a subclass if you need to do this often.
这会在您按下鼠标以产生单击按钮的效果时短暂地更改按钮的背景。与其他 SWT 小部件不同,CLabel 也可以扩展,因此如果您需要经常这样做,您可以创建一个子类。
回答by Mateen
No You cannot change the Background of a Button is SWT. You can find this information in Eclipse SWT documentation.
否 您不能更改按钮的背景是 SWT。您可以在 Eclipse SWT 文档中找到此信息。
Eclipse SWT Documentation Button
public void setBackground(Color color)
Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.
将接收器的背景颜色设置为参数指定的颜色,如果参数为空,则设置为控件的默认系统颜色。
Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.
注意:此操作是一个提示,可能会被平台覆盖。例如,在 Windows 上,不能更改 Button 的背景。
回答by Rafael Díaz y Díaz-Caneja
Could it be that the last change in SWT for Windows has changed this? I left the RED background in the code and I have changed the SWT library to the latest and now the background color is changing.
会不会是 Windows 版 SWT 的最后一次更改改变了这一点?我在代码中留下了红色背景,我已经将 SWT 库更改为最新的,现在背景颜色正在改变。

