Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

uibutton

创建普通按钮或状态按钮组件

说明

btn = uibutton 在新图窗中创建一个普通按钮,并返回 Button 对象。MATLAB® 调用 uifigure 函数来创建该图窗。

btn = uibutton(style) 创建指定样式的按钮。

示例

btn = uibutton(parent) 在指定的父容器中创建按钮。父容器可以是使用 uifigure 函数创建的 Figure 或其子容器之一。

示例

btn = uibutton(parent,style) 在指定的父容器中创建指定样式的 Button

示例

btn = uibutton(___,Name,Value) 使用由一个或多个 Name,Value 对组参数指定的属性创建 Button。可以将此选项与前面语法中的任何输入参数组合一起使用。

示例

全部折叠

创建一个普通按钮。

fig = uifigure;
btn = uibutton(fig);

UI figure window with a push button. The default button text is Button.

通过将样式指定为 'state',创建一个状态按钮。

fig = uifigure;
btn = uibutton(fig,'state');

UI figure window with a push button. The default button text is State Button.

fig = uifigure('Name','My Figure');
pnl = uipanel(fig);
btn = uibutton(pnl); 

UI figure window with a panel and a button. The button is in the panel.

创建状态按钮并指定属性值。

fig = uifigure;
btn = uibutton(fig,'state',...
               'Text', 'Record',...
               'Value', true,...
               'Position',[50,100, 100, 22]);

UI figure window with a state button. The button text is Record. The button is dark gray.

确定状态按钮文本的字体名称。

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 将绘制数据。

An app inside a UI figure. The app contains a plot with some data, and a button to the right of the plot.

输入参数

全部折叠

按钮的样式,指定为下列值之一:

  • 'push'

    点击一次,按钮将被按下并释放。

  • 'state'

    点击一次,按钮将保持按下或释放状态,直到再次点击为止。

父容器,指定为使用 uifigure 函数创建的 Figure 对象或其子容器之一:TabPanelButtonGroupGridLayout。如果不指定父容器,MATLAB 会调用 uifigure 函数创建新 Figure 对象充当父容器。

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

每种类型的 Button 对象支持一组不同的属性。有关每种类型的属性和描述的完整列表,请参阅相关联的属性页。

版本历史记录

在 R2016a 中推出

全部展开