gpt4 book ai didi

java - SWT 将 ICON_WARNING 与标签对齐

转载 作者:行者123 更新时间:2023-11-30 04:24:57 24 4
gpt4 key购买 nike

我想向 Eclipse 添加一个在选中某个复选框时出现的 Composite。该组合将由两部分组成:一个 WARNING_ICON 和一个带有一些文本的标签。

我似乎无法完全正确地布局 - 我想并排显示图像和标签。这是我的代码部分:

        final Composite warningComposite = new Composite(parent, SWT.NONE);
warningComposite.setLayout(new GridLayout(2, false));
warningComposite.setLayoutData(new GridData(0, 0, true, false));

Label l = SWTUtilities.createLabel(composite, "", -1, -1, 1, GridData.BEGINNING);
final Image img = PlatformUI.getWorkbench().getDisplay().getSystemImage(SWT.ICON_WARNING);
l.setImage(img);
l.setLayoutData(new GridData());

l = SWTUtilities.createLabel(composite, "Wizards Gone Wild", -1, -1, 1, GridData.END);

以及 SWTUtilities.createLabel 方法:

public static Label createLabel(final Composite parent, final String label, final int width, final int indent, final int span, final int style) {
Assert.isNotNull(parent);
Assert.isNotNull(label);
final Label control = new Label(parent, SWT.WRAP);
control.setText(label);
if (width > 0 || indent > 0 || span > 0) {
final GridData data = new GridData(style);
if (width > 0)
data.widthHint = width;
if (indent > 0)
data.verticalIndent = indent;
if (span > 1)
data.horizontalSpan = span;
control.setLayoutData(data);
}
return control;
}

最佳答案

给你:

public static void main(String[] args)
{
Display d = new Display();
final Shell shell = new Shell(d);
shell.setLayout(new GridLayout(2, false));

Label image = new Label(shell, SWT.NONE);
image.setImage(d.getSystemImage(SWT.ICON_WARNING));
image.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

Label text = new Label(shell, SWT.NONE);
text.setText("SOME TEXT HERE");
text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));


shell.pack();
shell.open();
while (!shell.isDisposed())
while (!d.readAndDispatch())
d.sleep();
}

看起来像这样:

enter image description here

<小时/>

解释如下:SWT 不支持 Label 内文本的垂直对齐(因为并非所有操作系统都支持)。解决方案是将标签对齐到父级的中心,而不是标签内的文本。

关于java - SWT 将 ICON_WARNING 与标签对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16174428/

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