gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:58 28 4
gpt4 key购买 nike

我是 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;
}
}

谢谢!

最佳答案

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

你可以通过调用来阻止:

formToolkit.setBackground(null);

引用:Source of FormToolkit#adapt(Control, boolean, boolean)

关于java - 如何在 SWT 中使标签背景透明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25188687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com