[英]Spec - I want to replace the first presenter in a SpPanedLayout
这可能不需要重建整个演示者吗?
我正在尝试这段代码,但它不能正常工作:
self layout remove: (self layout children first).
self layout add: aNewPresenter.
它实际上删除了演示者,但新的演示者不是放在第一位而是放在最后。
在 Spec2 中,所有布局都是动态的(意味着它们可以在运行时修改),但是每个布局都有不同的 API,因此需要以不同的方式进行修改。 特别是, SpPanedLayout
只有两个孩子:第一个和第二个(它们可以垂直显示-从上到下-或水平-从左到右-)。
这意味着与SpBoxLayout
不同,在SpPanedLayout
中, #add:
和#remove:
消息的使用不是必需的,并且不会总是产生所需的结果,并且由于#add:
将尝试将演示者添加到列表的末尾,在如果成功,它将永远是第二个。
相反,您可以只设置孩子,您将在您想要的地方有效地替换演示者。
假设您安装了 gtk 后端,此代码:
presenter := SpPresenter new.
presenter application: (SpApplication new useBackend: #Gtk).
presenter layout: (SpPanedLayout newHorizontal
first: (presenter newLabel label: 'I will replace this');
second: (presenter newLabel label: 'Powered by Pharo');
yourself).
presenter openWithSpec title: 'Example replace paned presenter'.
presenter layout
first: (presenter newImage image: (presenter application iconNamed: #pharoBig)).
将在运行时用 pharo 徽标替换 label “我将替换这个”。
它将产生这个 output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.