可见性设置为false后,SWT组件重新布局 [英] SWT components relayout after visibility set to false
本文介绍了可见性设置为false后,SWT组件重新布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个GridLayout复合,其中column = 1.(类似于垂直流布局)
我已将Label 1,Label 2,Label 3添加到此合成中,它们将相应地显示。
Lets say if I have a GridLayout composite with column = 1. (Something like a vertical flow layout) I have added Label 1, Label 2, Label 3 to this composite, and they will appear accordingly.
----------
Label 1 |
Label 2 |
Label 3 |
----------
如果我有可能将Label 2的可见性设置为 false
,Label 3可以向上移动以替换Label 2吗?如果Label 2的可见性设置回 true
,标签3将向下移动?
So is it possible that if I set the visibility of Label 2 to be false
, can Label 3 move up to replace Label 2? And if Label 2 visibility is set back to true
, Label 3 will move down?
推荐答案
一个非常简单的解决方案可以使用 GridData :: exclude
属性。例如,
A very simple solution could use GridData::exclude
property. For example,
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();
}
}
这篇关于可见性设置为false后,SWT组件重新布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文