setup
类: matlab.ui.componentcontainer.ComponentContainer
包: matlab.ui.componentcontainer
语法
setup(obj)
说明
setup(
设置 UI 组件的初始状态。它在 UI 组件的父组件赋值之后,创建 UI 组件对象时执行一次。执行 obj
)setup
方法后,将对作为名称-值参数传递给 UI 组件的构造函数方法的任何其他属性进行赋值。
定义此方法以便为类的每个新实例执行初始化代码。例如,您可以使用此方法创建底层图形对象,并设置这些对象的初始属性值。
输入参数
obj
— 类的对象
UI 组件对象
从 matlab.graphics.componentcontainer.ComponentContainer
基类继承的类的对象。
示例
IP 地址输入
定义名为 IPAddressComponent
的类,该类创建一个自定义组件,用于输入四个值来构成 IP 地址。
要定义该类,请创建包含以下类定义的名为 IPAddressComponent.m
的文件,该文件具有如下特性:
存储 IP 地址的
Value
公共属性。NumericField
和GridLayout
私有属性,它们在一个水平行中放置四个数值编辑字段。一个
setup
方法,用于初始化NumericField
和GridLayout
。一个
update
方法,用于在 IP 地址发生变化时更新NumericField
值。一个
handleNewValue
方法,它基于 4 个数值编辑字段的值设置Value
属性。
classdef IPAddressComponent < matlab.ui.componentcontainer.ComponentContainer % IPAddressComponent a set of 4 edit fields for IP Address input properties Value (1,4) {mustBeNonnegative, mustBeInteger, mustBeLessThanOrEqual(Value, 255)} = [192 168 1 2]; end events (HasCallbackProperty, NotifyAccess = protected) ValueChanged % ValueChangedFcn callback property will be generated end properties (Access = private, Transient, NonCopyable) NumericField (1,4) matlab.ui.control.NumericEditField GridLayout matlab.ui.container.GridLayout end methods (Access=protected) function setup(obj) % Set the initial position of this component obj.Position = [100 100 150 22]; % Layout obj.GridLayout = uigridlayout(obj,[1,5], ... 'RowHeight',{22},'ColumnWidth',{30,30,30,30,22},... 'Padding',0,'ColumnSpacing',2); % Building blocks for k = 1:4 obj.NumericField(k) = uieditfield(obj.GridLayout, 'numeric',... 'Limits', [0 255], 'RoundFractionalValues', true, ... 'FontName', 'Courier New', 'FontWeight', 'bold', ... 'ValueChangedFcn',@(o,e) obj.handleNewValue()); end end function update(obj) % Update view for k = 1:4 obj.NumericField(k).Value = obj.Value(k); end end end methods (Access=private) function handleNewValue(obj) obj.Value = [obj.NumericField.Value]; % Execute the event listeners and the ValueChangedFcn callback property notify(obj,'ValueChanged'); end end end
接下来,通过调用由 ComponentContainer
类提供的 IPAddressComponent
构造函数方法来创建组件,并将对象返回为 h
。指定一个函数,当组件值更改时,该函数在命令行窗口中显示新 IP 地址。
h = IPAddressComponent;
h.ValueChangedFcn = @(o,e) disp(['Value changed to: ', num2str(h.Value)]);
在编辑字段中输入 IP 地址 192.168.1.10
。MATLAB® 在命令行窗口中显示更新后的 IP 地址。
提示
不要在 UI 组件类的
setup
和update
方法中调用drawnow
。此类调用可能导致使用 UI 组件的 App 出现意外的屏幕更新。推荐的做法是,让 App 创建者(即您的组件用户)在需要触发屏幕更新时在其 App 代码中调用drawnow
。这些来自组件代码外部的调用会更新 App 中的所有 UI 组件,包括那些使用ComponentContainer
类创建的组件。
版本历史记录
在 R2020b 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)