在 (a)syncExec 中绘制新元素是不可能的吗?

发布于 2024-12-10 20:23:03 字数 820 浏览 0 评论 0原文

考虑到以下情况:

// 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2024-12-17 20:23:03

我不知道这是否是唯一的问题,但您至少必须重新布局组合及其子项。否则,标签的大小将为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文