用于uifigure组件的addChild [英] addChild for uifigure components
问题描述
我正在尝试使用uifigure
(以编程方式,而不是appdesigner
来开发我的第一个GUI [但是由于它与之相关,所以我将其添加为关键字]),并且(如预期的那样)我缺少了一些 GUI布局工具箱和窗口小部件工具箱用于标准Java图形.
I'm trying to develop my first GUIs with uifigure
(programmatically, not with appdesigner
[but I have added it as a keyword since it is related]), and (as expected) I'm missing some of the extended features and widgets as provided by the GUI Layout Toolbox and the Widgets Toolbox for standard java figures.
因此,我尝试将一些开发的小部件更改为uifigure
,并且uigridlayout
似乎非常方便,可以从GUI布局工具箱中替换uix.VBox
和uix.HBox
.
Thus, I tried to change some widgets that I had developed to uifigure
, and uigridlayout
seems to be quite handy to replace the uix.VBox
and uix.HBox
from the GUI Layout Toolbox.
对于标准的Java图形,假设我有一个类MyWidget
和它的对应实例mywidget
.最后,MyWidget
是matlab.ui.container.internal.UIContainer
的祖先,它提供了addChild
方法,可以将其重写以自定义
For standard java figures, assume I have a class MyWidget
and a corresponding instance mywidget
of it. MyWidget
would be, finally, an ancestor of matlab.ui.container.internal.UIContainer
which provides an addChild
method which can be overriden to customize the behaviour of
uicontrol(mywidget)
我正在为uifigure
组件寻找相同的东西.假定从matlab.ui.container.GridLayout
派生的跟随类,这是uigridlayout
调用结果的类.
I'm looking for the same for uifigure
components. Assume the follwing class derived from matlab.ui.container.GridLayout
, which is the class of the result of a uigridlayout
call.
classdef MyGrid < matlab.ui.container.GridLayout
methods
function self = MyGrid(varargin)
self = self@matlab.ui.container.GridLayout(varargin{:});
end
end
methods ( Access = protected )
function addChild(self, child)
disp('hooray');
addChild@matlab.ui.container.GridLayout(self, child);
end
end
end
现在我启动一个MyGrid
实例
g = MyGrid()
一切看起来不错:
g =
MyGrid with properties:
RowHeight: {'1x' '1x'}
ColumnWidth: {'1x' '1x'}
但向其添加子项不会调用addChild
方法:
but adding a child to it does not call the addChild
method:
>> uibutton(g)
ans =
Button (Button) with properties:
Text: 'Button'
Icon: ''
ButtonPushedFcn: ''
Position: [100 100 100 22]
注意:上面的hooray
没有输出. Parent
属性正确:
Note: No output of hooray
above. The Parent
property is correct:
>> ans.Parent
ans =
MyGrid with properties:
RowHeight: {'1x' '1x'}
ColumnWidth: {'1x' '1x'}
Show all properties
据此,我猜想addChild
不是添加孩子的方法(至少是matlab.ui.container.GridLayout
).
From this I guess that addChild
is not the method used (at least by matlab.ui.container.GridLayout
) to add a child.
有人知道将子项添加到uifigure
组件中的容器的机制吗?
Does anyone know the mechanism of adding a child to a container in an uifigure
component?
推荐答案
我不知道为什么昨天没去那里,但是matlab.ui.container.GridLayout
的代码具有受保护的方法
I don't know why I didn't look there yesterday, but the code of matlab.ui.container.GridLayout
has the (protected) method
function handleChildAdded(obj, childAdded)
obj.processChildAdded(childAdded);
obj.addChildLayoutPropChangedListener(childAdded);
obj.updateImplicitGridSize('childAdded', childAdded);
obj.updateLastCell('childAdded', childAdded);
end
对于我来说,方法processChildAdded
可能更好,但它是私有的.无论如何,handleChildAdded
起作用:
The method processChildAdded
might be better for my purposes, but is private. Anyway, handleChildAdded
works:
classdef MyGrid < matlab.ui.container.GridLayout
methods
function self = MyGrid(varargin)
self = self@matlab.ui.container.GridLayout(varargin{:});
end
end
methods ( Access = protected )
function handleChildAdded(self, child)
disp('hooray');
handleChildAdded@matlab.ui.container.GridLayout(self, child);
end
end
end
>> g=MyGrid();
>> uibutton(g);
hooray
这篇关于用于uifigure组件的addChild的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!