SWT 用户控件设计决策

发布于 2024-11-30 14:11:34 字数 298 浏览 1 评论 0原文

假设问题:

为了制作自定义复合小部件,我必须将 Composite 子类化,这个 SWT 设计决策怎么样?这真的明智吗?

如果 SWT 有一个像 Win Forms 之类的 UserControl 类不是更好吗?

当我对 Composite 进行子类化时,我的自定义小部件将获得 Composite 接口,即使它不打算被客户端用作 Composite。这有点糟糕。对于某些 SWT 小部件(例如 Spinner)也是如此。

有没有好的办法解决这个问题?

而且,最有趣的是:有人知道这个设计决定的动机吗?

Hypothetical question:

What about this SWT design decision that to make a custom compound widget I have to subclass Composite? Is that really wise?

Wouldn't it be better if SWT had a UserControl class like in Win Forms or something?

When I subclass Composite my custom widget get the Composite interface, even if it is not intended to be used as a Composite by clients. That's kind of bad. The same is true for some SWT widgets, like Spinner.

Is there a good way around this?

And, most interesting: Do anyone know the motivation for this design decision?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

巾帼英雄 2024-12-07 14:11:34

您可以查看 org.eclipse.swt.custom.CCombo 的源代码,了解 SWT 如何在内部处理此问题,因为 CCombo 扩展自 Composite< /代码>。以下是他们处理 setLayout() 的方式,例如:

/**
 * Sets the layout which is associated with the receiver to be
 * the argument which may be null.
 * <p>
 * Note: No Layout can be set on this Control because it already
 * manages the size and position of its children.
 * </p>
 *
 * @param layout the receiver's new layout or null
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void setLayout (Layout layout) {
    checkWidget ();
    return;
}

You could look at the source code for org.eclipse.swt.custom.CCombo to see how SWT deals with this problem internally, as CCombo extends from Composite. Here's how they handle setLayout(), for example:

/**
 * Sets the layout which is associated with the receiver to be
 * the argument which may be null.
 * <p>
 * Note: No Layout can be set on this Control because it already
 * manages the size and position of its children.
 * </p>
 *
 * @param layout the receiver's new layout or null
 *
 * @exception SWTException <ul>
 *    <li>ERROR_WIDGET_DISPOSED - if the receiver has been disposed</li>
 *    <li>ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver</li>
 * </ul>
 */
public void setLayout (Layout layout) {
    checkWidget ();
    return;
}
空气里的味道 2024-12-07 14:11:34

您可以创建自己的 NonCompositeUserControl 类:

public class NonCompositeUserControl extends Composite {
    @Override
    public void setLayout(Layout layout) {
        throw new UnsupportedOperationException("This control is not really a composite")
    }
    // similarly for other methods
}

You could create your own NonCompositeUserControl class:

public class NonCompositeUserControl extends Composite {
    @Override
    public void setLayout(Layout layout) {
        throw new UnsupportedOperationException("This control is not really a composite")
    }
    // similarly for other methods
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文