java 可见性设置为 false 后的 SWT 组件重新布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12189543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SWT components relayout after visibility set to false
提问by humansg
Lets say if I have a GridLayout
composite with column = 1. (Something like a vertical flow layout)
假设我有一个GridLayout
列 = 1的组合。(类似于垂直流布局)
I have added Label 1, Label 2, Label 3 to this composite, and they will appear accordingly.
我已经将标签 1、标签 2、标签 3 添加到这个组合中,它们会相应地出现。
----------
Label 1 |
Label 2 |
Label 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?
那么是否有可能如果我将标签 2 的可见性设置为false
,标签 3 可以向上移动以替换标签 2 吗?如果标签 2 的可见性设置回true
,标签 3 会向下移动吗?
回答by Favonius
A very simple solution could use GridData::exclude
property. For example,
一个非常简单的解决方案可以使用GridData::exclude
属性。例如,
Code
代码
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();
}
}