gpt4 book ai didi

java - 可见性设置为 false 后的 SWT 组件重新布局

转载 作者:搜寻专家 更新时间:2023-10-30 21:19:02 25 4
gpt4 key购买 nike

假设我有一个列 = 1 的 GridLayout 组合。(类似于垂直流布局)

我已将标签 1、标签 2、标签 3 添加到此组合中,它们将相应地出现。

----------
Label 1 |
Label 2 |
Label 3 |
----------

那么有没有可能,如果我将标签 2 的可见性设置为 false,标签 3 是否可以向上移动以取代标签 2?如果标签 2 的可见性设置回 true,标签 3 会向下移动吗?

最佳答案

一个非常简单的解决方案可以使用 GridData::exclude 属性。例如,

代码

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

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

Label label = new Label(shell, SWT.NONE);
label.setText("Label 1");

final Label bHidden = new Label(shell, SWT.NONE);
bHidden.setText("Label 2");
GridData data = new GridData();
data.exclude = false;
data.horizontalAlignment = SWT.FILL;
bHidden.setLayoutData(data);

label = new Label(shell, SWT.NONE);
label.setText("Label 3");

Button button = new Button(shell, SWT.CHECK);
button.setText("hide");
button.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event e) {
Button b = (Button) e.widget;
GridData data = (GridData) bHidden.getLayoutData();
data.exclude = b.getSelection();
bHidden.setVisible(!data.exclude);
shell.layout(false);
}
});
shell.setSize(200, 200);
shell.open();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

关于java - 可见性设置为 false 后的 SWT 组件重新布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12189543/

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