在下面的代码中,单击三个显示列表按钮之一将首先处理复合中的任何标签,然后为字符串列表中的每一行创建标签。
问题是如果你第二次点击同一个按钮,文本就会全部散开。
当您切换到其他按钮之一并显示文本时。
代码每次都做同样的事情,将复合材料中的所有标签都处理掉,然后在复合材料中添加新的标签,似乎发生的情况是,如果你将具有完全相同内容的标签添加回复合材料,那么它由于某种原因,它不再显示在屏幕上。
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
public class BasicMapLabelTestDialog extends Dialog
{
List<Label> displaylabels = new ArrayList<>();
Composite content, list;
public BasicMapLabelTestDialog(Shell parentShell)
{
super(parentShell);
}
@Override
protected void configureShell(Shell shell)
{
super.configureShell(shell);
shell.setSize(new Point(700, 500));
shell.setText("FML"); //$NON-NLS-1$
}
@Override
public Control createDialogArea(final Composite comp)
{
content = (Composite) super.createDialogArea(comp);
content.setLayout(new GridLayout(1, false));
content.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
final List<String> rows1 = new ArrayList<>(3);
rows1.add("Hello");
rows1.add("Baz");
rows1.add("Please");
rows1.add("Help");
final List<String> rows2 = new ArrayList<>(3);
rows2.add("You're");
rows2.add("My");
rows2.add("Only");
rows2.add("Hope");
final List<String> rows3 = new ArrayList<>(3);
rows3.add("Or");
rows3.add("Maybe");
rows3.add("greg?");
Button set1 = new Button(content, SWT.PUSH);
set1.setText("Display List 1");
set1.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows1);
}
});
Button set2 = new Button(content, SWT.PUSH);
set2.setText("Display List 2");
set2.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows2);
}
});
Button set3 = new Button(content, SWT.PUSH);
set3.setText("Display List 3");
set3.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
updateList(rows3);
}
});
Button test = new Button(content, SWT.PUSH);
test.setText("Print Label Content");
test.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
for (Label l : displaylabels) {
System.out.println(l.getText());
}
}
});
list = new Composite(content, SWT.NONE);
list.setLayout(new GridLayout(1, true));
new Label(content, SWT.HORIZONTAL | SWT.SEPARATOR);
return content;
}
private void updateList(List<String> rows) {
if (this.displaylabels == null) {
this.displaylabels = new ArrayList<>();
}
for (Label l : displaylabels) {
l.dispose();
}
this.displaylabels.clear();
for (String item : rows) {
addListLabel(item);
}
content.layout();
content.redraw();
}
private void addListLabel(String whoText) {
Label a = new Label(list, SWT.NONE);
a.setText(whoText);
this.displaylabels.add(a);
}
public static void main(String[] args)
{
Display d = new Display();
Shell s = new Shell();
BasicMapLabelTestDialog fml = new BasicMapLabelTestDialog(s);
fml.open();
}
}
谢谢,
Glen x
答案 0 :(得分:2)
调用
content.layout();
不足以在这里进行完整布局。你需要打电话
content.layout(true, true);
强制所有控件布局。
您不需要redraw
来电。