uibutton
创建普通按钮或状态按钮组件
语法
说明
在新图窗中创建一个普通按钮,并返回 btn
= uibuttonButton
对象。MATLAB® 调用 uifigure
函数来创建该图窗。
创建指定样式的按钮。按钮样式可以是 btn
= uibutton(style
)"push"
或 "state"
。
创建一个按钮,其属性由一个或多个名称-值参量指定。例如,使用 btn
= uibutton(___,Name,Value
)BackgroundColor
属性指定按钮背景颜色。可将此选项与上述语法中的任何输入参量组合一起使用。
示例
创建普通按钮
创建状态按钮
设置和访问按钮属性值
在 UI 图窗中创建一个状态按钮,并通过指定属性值来自定义其外观。
fig = uifigure; b = uibutton(fig,"state", ... "Text","Play", ... "Icon","play.png", ... "IconAlignment","top", ... "Position",[100 100 50 50]);
确定该状态按钮是否处于按下状态。
b.Value
ans = logical
0
以编程方式更新按钮值,使其以按下状态出现。
b.Value = true;
编写响应以下操作的代码:点击按钮
创建一个 App,当 App 用户按下按钮时,它会绘制一些数据。
在名为 plotApp.m
的文件中,编写实现该 App 的函数:
创建一个 UI 图窗和一个网格布局管理器,以对该 App 进行布局。
在网格布局管理器中创建 UI 坐标区和一个按钮。
编写名为
plotButtonPushed
的回调函数,该函数在 UI 坐标区中绘制一些数据,并将该函数赋给ButtonPushedFcn
回调属性。有关回调的详细信息,请参阅Create Callbacks for Apps Created Programmatically。
function plotApp fig = uifigure; g = uigridlayout(fig,[2 3]); g.RowHeight = {'1x','fit'}; g.ColumnWidth = {'1x','fit','1x'}; ax = uiaxes(g); ax.Layout.Row = 1; ax.Layout.Column = [1 3]; b = uibutton(g, ... "Text","Plot Data", ... "ButtonPushedFcn", @(src,event) plotButtonPushed(ax)); b.Layout.Row = 2; b.Layout.Column = 2; end function plotButtonPushed(ax) x = linspace(0,2*pi,100); y = sin(x); plot(ax,x,y) end
运行 plotApp
函数。点击按钮以绘制数据。
输入参数
style
— 按钮的样式
"push"
(默认) | "state"
按钮的样式,指定为以下值之一:
"push"
- 点击一次,按钮将被按下并释放。"state"
- 点击一次,按钮将保持按下或释放状态,直到再次点击为止。
parent
— 父容器
Figure
对象 (默认) | Tab
对象 | Panel
对象 | ButtonGroup
对象 | GridLayout
对象
父容器,指定为使用 uifigure
函数创建的 Figure
对象或其子容器之一:Tab
、Panel
、ButtonGroup
或 GridLayout
。如果不指定父容器,MATLAB 会调用 uifigure
函数创建新 Figure
对象充当父容器。
名称-值参数
将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: uibutton(fig,BackgroundColor="blue")
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: uibutton(fig,"BackgroundColor","blue")
每种类型的 Button
对象支持一组不同的属性。有关每种类型的属性和描述的完整列表,请参阅相关联的属性页。
如果
style
为"push"
(默认值),请参阅 Button 属性。如果
style
为"state"
,请参阅 StateButton 属性。
版本历史记录
在 R2016a 中推出R2022b: 指定附加图标和图标对齐选项
向按钮添加图标时,可以使用以下附加选项:
将
Icon
属性指定为预定义图标,如'success'
。将
IconAlignment
属性指定为'leftmargin'
或'rightmargin'
,以在按钮的最左侧或最右侧显示图标,而不考虑按钮文本的位置。
R2020b: 按钮文本换行
当 UI 组件的宽度小于要显示的文本的宽度时,使用 WordWrap
属性可防止文本在水平方向上被裁剪。将 WordWrap
属性设置为 'on'
会将文本分成若干新行,以便每行都适合组件宽度。它会尽可能避免拆分词语。当该属性设置为 'off'
时,文本不会换行。
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)