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

时间:2010-07-22 18:27:02

标签: java user-interface colors swt

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

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

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

7 个答案:

答案 0 :(得分: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);
    }
});

答案 1 :(得分:5)

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

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

答案 2 :(得分:4)

Windows中按钮的背景是从SWT外部设置的。

右键单击桌面,单击“属性”。

转到“外观”标签。

点击“高级”。

我相信“3D对象”决定了按钮背景。这取决于每个用户的主题。

alt text

关于SWT的一个好处是它使用底层系统小部件和主题。关于SWT令人沮丧的是它使用底层系统小部件和主题。

答案 3 :(得分:1)

您可以使用CLabel模拟按钮。添加mouselistener以在鼠标按下和鼠标向上更改背景,并在鼠标向上事件中调度选择侦听器事件,使其行为与按钮相同。例如:

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

当您按下鼠标以显示单击按钮的效果时,会短暂更改按钮的背景。与其他SWT小部件不同,CLabel也可以进行扩展,因此如果您需要经常这样做,可以创建一个子类。

答案 4 :(得分:1)

否不能更改按钮的背景为SWT。您可以在Eclipse SWT文档中找到此信息。

Eclipse SWT Documentation Button

public void setBackground(Color color)

将接收者的背景色设置为参数指定的颜色,如果参数为null,则设置为控件的默认系统颜色。

注意:此操作是提示,平台可能会覆盖它。例如,在Windows上,按钮的背景无法更改。

答案 5 :(得分:0)

难道是Windows的SWT的最后更改已经改变了吗?我在代码中保留了红色背景,并且将SWT库更改为最新版本,现在背景颜色正在更改。

答案 6 :(得分:0)

btnNewButton.setBackground(SWTResourceManager.getColor(SWT.COLOR_BLUE));

您可以使用上述代码在Windows的SWT中更改按钮的背景颜色。 几个先决条件应该是:

  • 使用这两个SWT jar:org.eclipse.swt.win32.win32.x86_64_3.109.0.v20181204-1801.jar和org.eclipse.swt_3.109.0.v20181204-1801.jar。 (109版)
  • 导入org.eclipse.wb.swt.SWTResourceManager 您也可以尝试使用最新版本的罐子,但这对我有用。 通常,您可以转到Windows构建器中的“高级属性”面板来设置背景,但它在运行时不会反映颜色。因此,该问题应消除。