Java 如何将控件设置为透明背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20975980/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:40:38  来源:igfitidea点击:

How to set a control to a transparent background

javaswtcontrolstransparent

提问by Sarah Weinberger

How do I set the background of a control to be transparent?

如何将控件的背景设置为透明?

I am speaking of Labeland Textcontrols at the moment, but can be any of the standard controls that I see in the GUI.

我所说的LabelText此刻的控制,但可以是任何的标准控件,我在GUI中看到的。

采纳答案by Baz

shell.setBackgroundMode(SWT.INHERIT_FORCE);

will do what you want.

会做你想做的。

The Compositeconstant to indicate that an attribute (such as background) is inherited by all children.

Composite指示属性(例如背景)由所有子项继承的常量。

public static void main(String[] args)
{
    Display display = new Display();
    Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));
    shell.setText("StackOverflow");

    shell.setBackground(display.getSystemColor(SWT.COLOR_BLUE));
    shell.setBackgroundMode(SWT.INHERIT_FORCE);

    new Button(shell, SWT.PUSH).setText("Button");
    new Label(shell, SWT.NONE).setText("Label");

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
        {
            display.sleep();
        }
    }

    display.dispose();
}

Looks like this:

看起来像这样:

enter image description here

在此处输入图片说明

回答by Konstantin Komissarchik

You need to make the following call on your composite before child controls, such as Labels will inherit the background from the composite.

您需要在子控件之前对您的复合进行以下调用,例如标签将从复合中继承背景。

composite.setBackgroundMode( SWT.INHERIT_DEFAULT );

回答by Thomas S.

According to my knowledge you can'tset a control (except of a shell on some operating systems) transparent or semitransparent in SWT, e.g. show a panel in front of a table control where the table will show through the panel. As the other posters wrote, one only can inherit the background.

据我所知,您不能在 SWT 中设置透明或半透明控件(某些操作系统上的外壳除外),例如在表格控件前面显示面板,表格将通过面板显示。正如其他海报所写,一个只能继承背景

回答by thSoft

If you add a Composite and specify the following flags, it will be transparent: new Composite(shell, SWT.TRANSPARENT | SWT.NO_BACKGROUND);

如果您添加一个 Composite 并指定以下标志,它将是透明的: new Composite(shell, SWT.TRANSPARENT | SWT.NO_BACKGROUND);