将焦点设置到 uifigure 窗口

问题描述 投票:0回答:2

焦点切换到其他图形后,如何将焦点设置到

uifigure

对于

uicontrol
,可以将焦点设置在其子元素之一上。例如:

% create a new uicontrol text label
h = uicontrol('style','text','string','This is my figure');
% create a figure to switch the focus
figure;
% switch back
uicontrol(h)

但是,对于

uifigure
,采用类似的代码只会创建一个新的
uifigure

一些代码供您尝试:

% create a new uifigure
h = uifigure('Name','This is my figure');
% create a new uilabel as a child of uifigure
lh = uilabel(h)
% create a figure to switch the focus
figure;
% this creates a new uifigure then switch back
uifigure(h)
% this creates an error as the first input argument must be a valid parent for uilabel
uilabel(lh)

任何想法、见解或贡献都值得赞赏。

请注意,您的 Matlab 版本至少应为 2016a,因为此时引入了

uifigure

matlab matlab-app-designer
2个回答
7
投票

这是 MathWorks 奇怪策略的又一受害者,该策略在新的 UI 框架真正具有任何功能之前就发布了它。诚然,新框架显示出很大的前景,但在功能方面它仍然远远落后于旧的图形系统。

撇开咆哮不谈,有一个在 R2017a 中测试良好的快速解决方法:切换

uifigure
的可见性,使其脱颖而出。这是一个基本示例:

function uifigurepop(uifigurehandle)
drawnow;
uifigurehandle.Visible = 'off';
uifigurehandle.Visible = 'on';
end

如果我们将其带入示例代码中:

% create a new uifigure
h = uifigure('Name','This is my figure');
% create a new uilabel as a child of uifigure
lh = uilabel(h)
% create a figure to switch the focus
figure;
% this creates a new uifigure then switch back
uifigure()
uifigurepop(h);

您的人物现在呈现在屏幕的最上方。


0
投票

在 R2024a 中,您还可以使用

figure()
命令将 uifigure 窗口置于焦点中。只需将代码中的
uifigure(h)
替换为
figure(h)
,焦点就会切换回带有句柄
uifigure
h

© www.soinside.com 2019 - 2024. All rights reserved.