如何在SWT中使标签背景透明

时间:2014-08-07 17:33:19

标签: java swt

我对Java SWT很陌生,在放置标签时,看起来我不能使标签背景透明。我正在使用eclipse luna SWT desine表格。 这是我的窗口看起来像: enter image description here

如您所见,所有标签背景都是白色的。 我应该怎么做,所以标签背景默认与窗口背景(灰色)相同(意思是 - 不是通过在运行时更改每个标签背景,这对我有用)。

它可能很简单,但我无法在谷歌上找到如何做到这一点。 这是我的代码:

   import java.awt.Color;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.SWT;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Label;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.jface.databinding.swt.WidgetProperties;
import org.eclipse.core.databinding.beans.PojoProperties;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Canvas;


public class WinApp2 {
    private DataBindingContext m_bindingContext;

    protected Shell shell;
    private Text destenation_ip;
    private final FormToolkit formToolkit = new FormToolkit(Display.getDefault());
    private Label lblDestenationPortIp;
    private Label lblNewLabel;
    private Text ammount_of_packets;
    private Label lblNewLabel_1;
    private Text sorce_ip;
    private Text destenation_port;
    private Text sorce_port;
    private Label lblNewLabel_2;
    private Label label;
    private Label label_1;
    private Label ammount_of_packets_info;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        Display display = Display.getDefault();
        Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
            public void run() {
                try {
                    WinApp2 window = new WinApp2();
                    window.open();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shell.open();
        shell.layout();

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

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shell = new Shell();
        shell.setBackgroundMode(SWT.INHERIT_DEFAULT);
        shell.setSize(450, 306);
        shell.setText("LAN Destroyer");
        shell.setLayout(new FormLayout());
        destenation_ip = new Text(shell, SWT.BORDER);
        FormData fd_destenation_ip = new FormData();
        destenation_ip.setLayoutData(fd_destenation_ip);

        lblDestenationPortIp = new Label(shell, SWT.NONE);
        lblDestenationPortIp.setAlignment(SWT.RIGHT);
        FormData fd_lblDestenationPortIp = new FormData();
        fd_lblDestenationPortIp.left = new FormAttachment(0, 351);
        fd_lblDestenationPortIp.right = new FormAttachment(100, -10);
        fd_lblDestenationPortIp.top = new FormAttachment(0, 35);
        lblDestenationPortIp.setLayoutData(fd_lblDestenationPortIp);
        lblDestenationPortIp.setBackground(SWTResourceManager.getColor(SWT.COLOR_LIST_SELECTION_TEXT));
        formToolkit.adapt(lblDestenationPortIp, true, true);
        lblDestenationPortIp.setText(":\u05E9\u05DC \u05D9\u05E2\u05D3 IP");

        lblNewLabel = new Label(shell, SWT.NONE);
        lblNewLabel.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel = new FormData();
        fd_lblNewLabel.right = new FormAttachment(100, -10);
        lblNewLabel.setLayoutData(fd_lblNewLabel);
        formToolkit.adapt(lblNewLabel, true, true);
        lblNewLabel.setText(":\u05DB\u05DE\u05D5\u05EA");

        ammount_of_packets = new Text(shell, SWT.BORDER);
        fd_destenation_ip.left = new FormAttachment(ammount_of_packets, 0, SWT.LEFT);
        fd_destenation_ip.right = new FormAttachment(ammount_of_packets, -1, SWT.RIGHT);
        FormData fd_ammount_of_packets = new FormData();
        fd_ammount_of_packets.right = new FormAttachment(lblNewLabel, -68);
        fd_ammount_of_packets.top = new FormAttachment(lblNewLabel, -3, SWT.TOP);
        ammount_of_packets.setLayoutData(fd_ammount_of_packets);
        formToolkit.adapt(ammount_of_packets, true, true);

        Button nonstop_send = new Button(shell, SWT.CHECK);
        fd_ammount_of_packets.left = new FormAttachment(0, 186);
        nonstop_send.setAlignment(SWT.RIGHT);
        nonstop_send.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                if(nonstop_send.getSelection()){
                    ammount_of_packets_info.setText("שולח ללא הפסקה");
                    ammount_of_packets_info.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GREEN));
                    ammount_of_packets.setEnabled(false);
                }
                else {
                    ammount_of_packets_info.setText("");
                    ammount_of_packets.setEnabled(true);
                }

            }
        });
        FormData fd_nonstop_send = new FormData();
        fd_nonstop_send.top = new FormAttachment(lblNewLabel, 0, SWT.TOP);
        fd_nonstop_send.right = new FormAttachment(ammount_of_packets, -9);
        nonstop_send.setLayoutData(fd_nonstop_send);
        formToolkit.adapt(nonstop_send, true, true);
        nonstop_send.setText("\u05E9\u05DC\u05D7 \u05DC\u05DC\u05D0 \u05D4\u05E4\u05E1\u05E7\u05D4");

        lblNewLabel_1 = new Label(shell, SWT.NONE);
        fd_lblNewLabel.top = new FormAttachment(lblNewLabel_1, 6);
        lblNewLabel_1.setAlignment(SWT.RIGHT);
        FormData fd_lblNewLabel_1 = new FormData();
        fd_lblNewLabel_1.right = new FormAttachment(100, -10);
        fd_lblNewLabel_1.top = new FormAttachment(lblDestenationPortIp, 6);
        lblNewLabel_1.setLayoutData(fd_lblNewLabel_1);
        formToolkit.adapt(lblNewLabel_1, true, true);
        lblNewLabel_1.setText(":\u05E9\u05DC \u05DE\u05E7\u05D5\u05E8 IP");

        sorce_ip = new Text(shell, SWT.BORDER);
        fd_destenation_ip.bottom = new FormAttachment(sorce_ip);
        fd_lblNewLabel_1.left = new FormAttachment(sorce_ip, 29);
        FormData fd_sorce_ip = new FormData();
        fd_sorce_ip.top = new FormAttachment(0, 58);
        fd_sorce_ip.left = new FormAttachment(0, 186);
        fd_sorce_ip.right = new FormAttachment(100, -114);
        sorce_ip.setLayoutData(fd_sorce_ip);
        formToolkit.adapt(sorce_ip, true, true);

        destenation_port = new Text(shell, SWT.BORDER);
        fd_nonstop_send.left = new FormAttachment(destenation_port, 0, SWT.LEFT);
        FormData fd_destenation_port = new FormData();
        fd_destenation_port.top = new FormAttachment(0, 32);
        fd_destenation_port.left = new FormAttachment(0, 40);
        destenation_port.setLayoutData(fd_destenation_port);
        formToolkit.adapt(destenation_port, true, true);

        sorce_port = new Text(shell, SWT.BORDER);
        sorce_port.setForeground(SWTResourceManager.getColor(SWT.COLOR_CYAN));
        sorce_port.setBackground(SWTResourceManager.getColor(SWT.COLOR_YELLOW));
        FormData fd_sorce_port = new FormData();
        fd_sorce_port.top = new FormAttachment(lblNewLabel_1, -3, SWT.TOP);
        fd_sorce_port.left = new FormAttachment(destenation_port, 0, SWT.LEFT);
        sorce_port.setLayoutData(fd_sorce_port);
        formToolkit.adapt(sorce_port, true, true);

        lblNewLabel_2 = new Label(shell, SWT.NONE);
        FormData fd_lblNewLabel_2 = new FormData();
        fd_lblNewLabel_2.top = new FormAttachment(0, 35);
        fd_lblNewLabel_2.left = new FormAttachment(destenation_port, 6);
        lblNewLabel_2.setLayoutData(fd_lblNewLabel_2);
        formToolkit.adapt(lblNewLabel_2, true, true);
        lblNewLabel_2.setText(":\u05E4\u05D5\u05E8\u05D8");

        label = new Label(shell, SWT.NONE);
        label.setText(":\u05E4\u05D5\u05E8\u05D8");
        FormData fd_label = new FormData();
        fd_label.bottom = new FormAttachment(nonstop_send, -6);
        fd_label.left = new FormAttachment(sorce_port, 6);
        label.setLayoutData(fd_label);
        formToolkit.adapt(label, true, true);

        label_1 = new Label(shell, SWT.WRAP | SWT.SHADOW_IN);
        FormData fd_label_1 = new FormData();
        fd_label_1.top = new FormAttachment(ammount_of_packets, 20);
        fd_label_1.left = new FormAttachment(0, 187);
        label_1.setLayoutData(fd_label_1);
        formToolkit.adapt(label_1, true, true);
        label_1.setText(":\u05E1\u05D9\u05DB\u05D5\u05DD");

        ammount_of_packets_info = new Label(shell, SWT.NONE);
        ammount_of_packets_info.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_MAGENTA));
        FormData fd_ammount_of_packets_info = new FormData();
        fd_ammount_of_packets_info.left = new FormAttachment(100, -158);
        fd_ammount_of_packets_info.top = new FormAttachment(ammount_of_packets, 44);
        fd_ammount_of_packets_info.right = new FormAttachment(100, -36);
        ammount_of_packets_info.setLayoutData(fd_ammount_of_packets_info);
        formToolkit.adapt(ammount_of_packets_info, true, true);
        m_bindingContext = initDataBindings();

    }
    protected DataBindingContext initDataBindings() {
        DataBindingContext bindingContext = new DataBindingContext();
        //
        return bindingContext;
    }
}

谢谢!

2 个答案:

答案 0 :(得分:7)

您的问题是由FormToolkit#adapt(Control, boolean, boolean)引起的。在内部,此方法将背景颜色设置为工具箱的背景颜色。

您可以通过以下方式阻止:

formToolkit.setBackground(null);

参考:Source of FormToolkit#adapt(Control, boolean, boolean)

答案 1 :(得分:1)

如果您仍想要文字,请使用我上面提到的内容:shell.setBackground(SWT.INHERIT_FORCE);。这使得标签的背景与背景颜色相匹配。

但是,您也可以选择更简单的选项。如果您想“隐藏”文字,但需要将其保留以供日后使用并在需要时显示,则可以将特定标签的文字设置为null

当我需要文本消失时,我经常使用它。唯一的问题是,您需要将文本存储到数组(列表)中 - 或者您想要的任何内容(即数据库或类似的数据库),以便您可以保存文本以供日后使用。您甚至不需要使用SWT.INHERIT_FORCE。这是一个可能有用的完整示例:

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;


/**
 *
 * @author Dylan
 */

public class Test {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setLayout(new GridLayout(1, false));
        shell.setText("Stackoverflow Test");

        shell.setBackground(display.getSystemColor(SWT.COLOR_WHITE));
//        shell.setBackgroundMode(SWT.INHERIT_FORCE);

        Button button = new Button(shell, SWT.PUSH);
        button.setText("Button that's fun to click - but there is no text :D");
        Label label = new Label(shell, SWT.NONE);
        label.setText("Horray, you entered text to stdin!!");

        ArrayList<String> labelText = new ArrayList<>();
        labelText.add(label.getText());
        label.setText("");

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

        Boolean flag = false;
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
                if (!flag) {
                    try(BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                        System.out.println("Please enter some text");
                        char[] cbuf = new char[40];

                        String text = reader.readLine();

                        if (text.length()> 0) {
                            flag = true;

                            label.setText(labelText.remove(0));
                            button.setText(button.getText() + "... and now there is. Yay!");
                            shell.pack();
                        } else {
                            break;
                        }
                    } catch (IOException io) {
                        System.err.println(io.toString());
                    }
                }
            }
        }

        display.dispose();
    }

}