uibutton
创建普通按钮或状态按钮组件
语法
说明
在新图窗中创建一个普通按钮,并返回 btn
= uibuttonButton
对象。MATLAB® 调用 uifigure
函数来创建该图窗。
创建指定样式的按钮。btn
= uibutton(style
)
使用由一个或多个 btn
= uibutton(___,Name,Value
)Name,Value
对组参数指定的属性创建 Button
。可以将此选项与前面语法中的任何输入参数组合一起使用。
示例
创建按钮
创建一个普通按钮。
fig = uifigure; btn = uibutton(fig);
创建状态按钮
通过将样式指定为 'state'
,创建一个状态按钮。
fig = uifigure;
btn = uibutton(fig,'state');
在面板中创建按钮
fig = uifigure('Name','My Figure'); pnl = uipanel(fig); btn = uibutton(pnl);
设置和访问按钮属性值
创建状态按钮并指定属性值。
fig = uifigure; btn = uibutton(fig,'state',... 'Text', 'Record',... 'Value', true,... 'Position',[50,100, 100, 22]);
确定状态按钮文本的字体名称。
fname = btn.FontName
fname = Helvetica
更改按钮文本的字体名称。
btn.FontName = 'Arial Narrow';
编写响应以下操作的代码:点击按钮
创建一个按钮和一个 UI 坐标区。当 App 用户按下该按钮时,将创建一个图形。
在 MATLAB 路径中创建 buttonPlot.m
。以下代码将创建一个窗口,其中包含一个按钮和一个 UI 坐标区。当 App 用户点击该按钮时,ButtonPushedFcn
将绘制一些数据。
function buttonPlot % Create a figure window fig = uifigure; % Create a UI axes ax = uiaxes('Parent',fig,... 'Units','pixels',... 'Position', [104, 123, 300, 201]); % Create a push button btn = uibutton(fig,'push',... 'Position',[420, 218, 100, 22],... 'ButtonPushedFcn', @(btn,event) plotButtonPushed(btn,ax)); end % Create the function for the ButtonPushedFcn callback function plotButtonPushed(btn,ax) x = linspace(0,2*pi,100); y = sin(x); plot(ax,x,y) end
运行 buttonPlot
,然后点击普通按钮。MATLAB 将绘制数据。
输入参数
style
— 按钮的样式
'push'
(默认) | 'state'
按钮的样式,指定为下列值之一:
'push'
点击一次,按钮将被按下并释放。
'state'
点击一次,按钮将保持按下或释放状态,直到再次点击为止。
parent
— 父容器
Figure
对象 (默认) | Panel
对象 | Tab
对象 | ButtonGroup
对象 | GridLayout
对象
父容器,指定为使用 uifigure
函数创建的 Figure
对象或其子容器之一:Tab
、Panel
、ButtonGroup
或 GridLayout
。如果不指定父容器,MATLAB 会调用 uifigure
函数创建新 Figure
对象充当父容器。
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
每种类型的 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)