在 (a)syncExec 中绘制新元素是不可能的吗?
考虑到以下情况:
// Initializing a new composite within a parent UI container
Composite composite = new Composite(parent, SWT.NONE);
Label label = new Label(composite, SWT.NONE);
label.setText("Hi. I am a label and I'm drawn correctly.");
Display.getDefault().syncExec(new Runnable() {
// Here I'm trying to draw a new label onto this composite
Label newLabel = new Label(composite, SWT.NONE);
Label.setText("I am a test label. You should see me now.");
// Change the text of 'label' here
label.setText("Uh-oh. My text has been altered.");
// Let's redraw the parent UI component to see the new label drawn
parent.redraw();
});
尽管 label
的文本已在视觉上发生了更改,但 newLabel
从未绘制。同样,在 syncExec()
中处理 UI 元素会导致其视觉删除。这是为什么?
我看不出有什么理由不能做到这一点。
Given the following situation:
// Initializing a new composite within a parent UI container
Composite composite = new Composite(parent, SWT.NONE);
Label label = new Label(composite, SWT.NONE);
label.setText("Hi. I am a label and I'm drawn correctly.");
Display.getDefault().syncExec(new Runnable() {
// Here I'm trying to draw a new label onto this composite
Label newLabel = new Label(composite, SWT.NONE);
Label.setText("I am a test label. You should see me now.");
// Change the text of 'label' here
label.setText("Uh-oh. My text has been altered.");
// Let's redraw the parent UI component to see the new label drawn
parent.redraw();
});
newLabel
is never drawn, although label
's text has been visually altered. Likewise, disposing an UI element within syncExec()
consequently leads to its visual deletion. Why is that?
I cannot see a valid reason why it is impossible to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道这是否是唯一的问题,但您至少必须重新布局组合及其子项。否则,标签的大小将为 0,因此不可见。您通常通过调用
composite.layout()
来完成此操作。除非组合没有布局管理器;那么你就必须手动设置它的边界。I don't know if that's the only problem but you have to at least relayout the composite and its children. Otherwise the label will just have a size of 0 and will therefore not be visible. You typically do this by calling
composite.layout()
. Unless the composite doesn't have a layout manager; then you would have to manually set its bounds.