如何更改 SWT 按钮背景颜色或使其透明 [英] How to change SWT Button background color or make it transparent

查看:123
本文介绍了如何更改 SWT 按钮背景颜色或使其透明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Button(无文本)上有一个 透明 图像,该图像位于 Composite 上.由于 Composite 是白色的(使用 FormToolkit#createComposite(parent, SWT.NONE)),我希望 Button 背景颜色相同.我该怎么做?

I have a transparent image on a Button (no text), which is placed on a Composite. Since the Composite is white (created with FormToolkit#createComposite(parent, SWT.NONE)), I'd like the Button background to be the same color. How do I do it?

Label 可以解决问题,但没有像 Button 那样的阴影,当我点击它时..

The Label does the trick, but doesn't have the shadows like Button does when I'm clicking on it..

推荐答案

Button 的背景颜色由操作系统决定.事实上,Control.setBackground() 声明:

The background color of a Button is determined by the OS. In fact, the documentation for Control.setBackground() states that:

注意:此操作是一个提示,可能会被平台覆盖.例如,在 Windows 上,无法更改 Button 的背景.

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 中更改 org.eclipse.swt.widgets 背景颜色.当我尝试这个时,结果有点不稳定.

That said, one possible way to circumvent this is to override the paint event as shown here: Changing org.eclipse.swt.widgets background color in Windows. When I tried this out the results were a bit wonky.

最安全和最一致的方法是在第二张图片中使用标签,但在各种鼠标事件上显示不同的图片以模拟按钮的行为.

The safest and most consistent approach would be to use a label like in your second image, but have different images to display on various mouse events to emulate how a button behaves.

这些图像可以通过向图像本身添加任何您想要的阴影形状来模拟阴影.每个图像的阴影也会发生变化,以给人一种按钮是否被按下的印象.

Those images can emulate the shadow just by adding whatever shape of shadow you want to the image itself. That shadow can also change for each image to give the impression that the button is being pressed or not.

例如,我在思考以下问题:

For example, I'm thinking something along the lines of:

public class MyButton { 

    private final Label buttonLabel;

    public MyButton(final Composite parent, final Theme theme) {
        buttonLabel = new Label(parent, SWT.NONE);
        buttonLabel.setImage(theme.getUpImage());
        buttonLabel.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonPressedImage());
            }
            @Override
            public void mouseUp(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonUpImage());
            }
        });
        buttonLabel.addMouseTrackListener(new MouseTrackAdapter() {
            @Override
            public void mouseEnter(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonHoverImage());
            }
            @Override
            public void mouseExit(final MouseEvent mouseEvent) {
                buttonLabel.setImage(theme.getButtonUpImage());
            }
        });
    }

}

Theme 只是方便地加载了所有图像.

Where the Theme just has all of the images already conveniently loaded.

您还需要确保父 Composite 将背景模式设置为强制其背景颜色:

You'll also need to make sure that the parent Composite has the background mode set to force its background color:

parent.setBackgroundMode(SWT.INHERIT_FORCE);

显然,这种方法的缺点是您必须自己处理鼠标点击逻辑(即,在释放鼠标之前,mouseDown 不会真正被点击,因此您必须处理每个侦听器中按钮的状态方法).

Obviously the drawback to this approach is that you have to handle the mouse click logic yourself (ie. mouseDown isn't really clicked until the mouse is released, so you'll have to handle the state of the button in each listener method).

这篇关于如何更改 SWT 按钮背景颜色或使其透明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆