假设性问题:
制作自定义复合小部件的SWT设计决策怎么样我必须继承Composite?这真的很明智吗?
如果SWT有一个类似于Win Forms的UserControl类,那会不会更好?
当我继承Composite时,我的自定义小部件获得了Composite接口,即使它不打算被客户端用作Composite。那有点不好。对于某些SWT小部件也是如此,例如Spinner。
这有什么好方法吗?
而且,最有趣的是:有人知道这个设计决定的动机吗?
答案 0 :(得分:2)
您可以查看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;
}
答案 1 :(得分:1)
您可以创建自己的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
}