Main Content

scroll

滚动到组件内的位置

说明

可滚动容器或按钮组

scroll(container,vlocation) 垂直滚动容器或按钮组。将 vlocation 指定为 'top''bottom'。有关在哪些条件下支持滚动的详细信息,请参阅支持滚动的条件

scroll(container,hlocation) 水平滚动。将 hlocation 指定为 'left''right'

示例

scroll(container,hlocation,vlocation) 垂直和水平滚动到一个角。例如,scroll(container,'right','bottom') 滚动到组件的右下角。

示例

scroll(container,comp) 滚动到容器中的指定组件。

示例

scroll(container,x,y) 滚动到指定 (x, y) 坐标。坐标是从容器或按钮组的左下角以像素为单位测量的。

scroll(container,coord) 将像素坐标指定为二元素向量 coord。例如,scroll(container,[100 150]) 将容器或按钮组滚动到坐标 (100,150) 处。

文本区域

示例

scroll(txa,vlocation) 垂直滚动文本区域。将 vlocation 指定为 'top''bottom'

scroll(tr,vlocation) 垂直滚动树。将 vlocation 指定为 'top''bottom'

示例

scroll(tr,treenode) 滚动到指定的树节点。

列表框

scroll(lb,vlocation) 垂直滚动列表框。将 vlocation 指定为 'top''bottom'

示例

scroll(lb,item) 滚动到列表框的指定项。

scroll(lb,itemdata) 滚动到包含指定项数据的列表框项。如果有多个项具有相同的项数据,该函数会滚动到第一个项。

scroll(tbl,vlocation) 垂直滚动表。将 vlocation 指定为 'top''bottom'

scroll(tbl,hlocation) 水平滚动表。将 hlocation 指定为 'left''right'

示例

scroll(tbl,tableTarget,tableIndex) 滚动到表的指定行、列或单元格。例如,scroll(uit,'Row',10) 滚动到表的第十行。

示例

全部折叠

滚动到在使用 uifigure 函数创建的图窗中的指定 (x,y) 坐标。

创建一个图窗,并将 Scrollable 属性设置为 'on'。然后添加五个子组件。为确保图窗可滚动,将第一个组件放在图窗上边缘的上方。然后使各组件的间隔足够远,以使它们不能一起放入图窗中。

% Create figure
fig = uifigure('Name','Customer','Scrollable','on');
fig.Position = [100 100 493 283];

% Title label
title = uilabel(fig,'Text','Customer Record');
title.FontSize = 18;
title.FontWeight = 'bold';
title.Position = [173 315 157 22];

% Name
name = uieditfield(fig,'text');
name.Position = [169 239 173 22];
namelabel = uilabel(fig,'Text','Name','HorizontalAlignment','right');
namelabel.Position = [116 239 38 22];

% Phone
phone = uieditfield(fig,'text');
phone.Position = [169 164 173 22];
phonelabel = uilabel(fig,'Text','Phone','HorizontalAlignment','right');
phonelabel.Position = [114 164 40 22];

% Balance
balance = uieditfield(fig,'numeric');
balance.Position = [169 89 173 22];
balancelabel = uilabel(fig,'Text','Balance','HorizontalAlignment','right');
balancelabel.Position = [105 89 49 22];

% Submit button
button = uibutton(fig,'push','Text','Submit');
button.Position = [169 14 173 22];

UI figure window with a form to fill out a customer record. The window is vertically scrollable.

默认情况下,MATLAB® 会滚动到包含子组件的区域的左上角。

滚动到位置 (1,1),这是图窗的底部。

scroll(fig,1,1)

UI figure window with a form to fill out a customer record. The window is scrolled to the bottom.

通过指定像素坐标或位置名称,以显示可滚动网格布局的子组件。

创建一个 5×2 网格布局,并将网格的 Scrollable 属性设置为 'on'。然后在该网格中添加一个标签、一个表和一个面板。将面板的 Scrollable 属性设置为 'off',然后向面板添加一个图。

fig = uifigure('Position',[782 497 435 311]);
g = uigridlayout(fig,'Scrollable','on');
g.RowHeight = {22,40,22,22,400};
g.ColumnWidth = {400,400};

lbl = uilabel(g,'Text','Tsunamis');
lbl.Layout.Row = 2;
lbl.Layout.Column = [1,2];
lbl.HorizontalAlignment = 'center';
lbl.FontSize = 28;

tsunamis = readtable('tsunamis.xlsx');
tsunamis.Cause = categorical(tsunamis.Cause);
t = uitable(g,'Data',tsunamis);
t.Layout.Row = [3,5];
t.Layout.Column = 2;

p = uipanel(g);
p.Scrollable = 'off';
p.Layout.Row = [3,5];
p.Layout.Column = 1;
gb = geobubble(p,tsunamis.Latitude,tsunamis.Longitude,...
    tsunamis.MaxHeight,tsunamis.Cause);

Scrollable UI figure window with the upper-left portion of the grid visible

滚动到网格中的一个位置。

scroll(g,100,-30)

UI figure window scrolled 100 pixels to the right and 30 pixels down

现在使用位置名称滚动到网格的右下角。

scroll(g,'bottom','right')

UI figure window scrolled fully to the right and down, so that the lower-right portion of the grid is visible

通过将组件指定为滚动位置,将其显示在视图中。

创建一个包含两个下拉组件、一个列表框和一个表的图窗。定位组件,使它们无法同时显示在图窗中。

fig = uifigure;
fig.Scrollable = 'on';
fig.Position = [100 300 328 110];

dd1 = uidropdown(fig);
dd1.Position = [20 360 120 22];

dd2 = uidropdown(fig);
dd2.Position = [20 200 120 22];

lb = uilistbox(fig);
lb.Position = [230 300 120 80];

t = readtable('patients.xls');
uit = uitable(fig,'Data',t);
uit.Position = [375 100 300 300];

Scrollable UI figure window. The window is scrolled to the top left, and the drop-down and list box components are visible.

滚动到表。

scroll(fig,uit)

Scrollable UI figure window. The table component is visible.

以编程方式滚动到文本区域的底部。

创建一个文本区域。为其指定大小和长文本。

fig = uifigure;
txa = uitextarea(fig);
txa.Position = [100 100 80 80];
txa.Value = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor.";

Text area in a UI figure window. The text area is vertically scrollable.

滚动到文本区域的底部。

scroll(txa,"bottom")

Text area in a UI figure window. The text area is scrolled to the bottom.

通过在列表框中指定某个项,使该项显示在视图中。

创建一个列表框,其中包含与 ItemsData 相关的名称列表。

fig = uifigure('Position',[680 678 300 200]);
list = uilistbox(fig, 'Position',[70 50, 150 78]);
list.Items = {'Diane Fitzsimmons', 'Naomi Becker', 'Nick Stewart',...
           'Alex Bradford', 'Caroline Eliot', 'Leslie Bond', ...
           'Aaron Silberlicht', 'Ramu Sadasiv', 'Joyce Wu',...
           'Ann Shanahan'};
list.ItemsData = [1 2 3 4 5 6 7 8 9 10];

List box in a UI figure window. The list box displays the first four names and is vertically scrollable.

滚动到 Caroline Eliot

scroll(list,'Caroline Eliot')

List box in a UI figure window. The list box is scrolled so that the name "Caroline Eliot" is displayed at the bottom of the visible items.

通过将 Value 属性设置为 ItemsData 属性中的对应元素来选择 Caroline Eliot

list.Value = 5;

List box in a UI figure window. The item "Caroline Eliot" is highlighted.

通过指定 TreeNode 对象使某个树节点显示在视图中。

创建一个树,其中包含四个顶层节点,每个顶层节点又包含子节点。

fig = uifigure;
tree = uitree(fig,'Position',[20 20 175 100]);
    
% First level nodes
category1 = uitreenode(tree,'Text','Runners');
category2 = uitreenode(tree,'Text','Cyclists');
category3 = uitreenode(tree,'Text','Hikers');
category4 = uitreenode(tree,'Text','Swimmers');

% Second level nodes
r1 = uitreenode(category1,'Text','Joe');
r2 = uitreenode(category1,'Text','Linda');
c1 = uitreenode(category2,'Text','Rajeev');
h1 = uitreenode(category3,'Text','Jack');
s1 = uitreenode(category4,'Text','Logan');

Tree component in a UI figure window. All top-level nodes are collapsed and visible.

展开节点,使 Swimmers 滚动到视图之外。

expand(tree)

Tree component in a UI figure window. All nodes are expanded, and the Swimmers node is not visible. The tree is vertically scrollable.

滚动到 Swimmers 节点。

scroll(tree,category4)

Tree component in a UI figure window. The Swimmers node is visible.

通过设置 Tree 对象的 SelectedNodes 属性来选择 Swimmers 节点。

tree.SelectedNodes = category4;

Tree component in a UI figure window. The Swimmers node is highlighted.

将样本患者数据以表数组形式读入工作区。然后,创建一个表 UI 组件来显示数据。

tdata = readtable("patients.xls");
vars = ["Age","Systolic","Diastolic","Smoker"];
tdata = tdata(1:40,vars);

fig = uifigure;
uit = uitable(fig,"Data",tdata);
uit.RowName = "numbered";

Table with patient data in a UI figure window. The table rows are numbered, and the table is scrolled to the top.

滚动到表的第 25 行。

scroll(uit,"row",25)

Table with patient data in a UI figure window. The table is scrolled so that row 25 is at the top of the visible data.

输入参数

全部折叠

可滚动容器或按钮组,指定为使用 uifigure 函数创建的图窗,或该图窗中的以下任何组件:网格布局、面板、按钮组或选项卡。

垂直滚动位置,指定为 'top''bottom'

水平滚动位置,指定为 'left''right'

要滚动到的 UI 组件。容器滚动以将组件显示在视图中。

x 坐标,即距容器左边缘的距离,指定为一个非负整数(以像素为单位)。容器滚动以在可见容器区域的左侧显示指定的 x 坐标。如果指定的值超出了容器的可滚动区域,则容器将在指定的方向上滚动到其所能达到的最远距离。

y 坐标,即距容器下边缘的距离,指定为一个整数(以像素为单位)。容器滚动以在可见容器区域的底部显示指定的 y 坐标。如果指定的值超出了容器的可滚动区域,则容器将在指定的方向上滚动到其所能达到的最远距离。

如果网格布局高于其父容器,您可以使用负 y 坐标滚动到网格中位于父容器下边缘下方的组件。

像素坐标,指定为由整数组成的二元素行向量。

示例: [100 150] 指定像素坐标 (100,150)

文本区域,指定为使用 uitextarea 函数创建的组件。

从 R2020b 开始支持在文本区域内滚动。

树,指定为使用 uitree 函数创建的组件。此组件可以是具有默认样式的树,也可以是用 uitree('checkbox') 创建的复选框树。

树节点,指定为使用 uitreenode 函数创建的组件。使用此参数指定要滚动到哪个树节点。

列表框,指定为使用 uilistbox 函数创建的组件。

列表框项,指定为 lbItems 属性的元素。函数滚动到列表框中的指定项。

列表框项数据,指定为 lb 的列表框 ItemsData 属性的元素。该函数会滚动到列表框中具有关联项数据的项。如果有多个这样的项,函数会滚动到第一个项。

表,指定为使用 uitable 函数创建的组件。

从 R2021a 开始支持在表内滚动,但仅当表 UI 组件位于使用 uifigure 函数创建的图窗中时才支持在表内滚动。

表滚动目标,指定为 'row''column''cell'

表滚动目标索引,指定为下表中列出的值之一。您可以指定的值的类型取决于 tableTarget 的值。

tableTarget支持的值示例结果
'row'正整数4垂直滚动以在视图中显示具有对应行索引的行。
'column'正整数3水平滚动以在视图中显示具有对应列索引的列。
'cell'正整数的二元素行向量[5 9]滚动以在视图中显示具有对应行和列索引的单元格。

详细信息

全部折叠

支持滚动的条件

要允许在容器内滚动,容器的 Scrollable 属性必须设置为 'on'。此外,还必须满足下列特定于容器的条件:

网格布局

  • 为网格的 RowHeight 属性指定的值的总和必须大于父容器的高度。

  • 为网格的 ColumnWidth 属性指定的值的总和必须大于父容器的宽度。

  • 网格中有至少一行或一列必须设置为固定的像素高度或宽度。

  • 网格必须包含组件。

网格布局以外的容器

  • 容器中子组件占用的区域必须大于容器一次可显示的区域。

  • 在容器内无法容纳的组件必须位于容器的上方或右侧。

版本历史记录

在 R2016a 中推出

全部展开