MATLAB:自动调整 GUI 组件/字体的大小

标签 matlab user-interface resize screen-resolution

我在尝试使我的 MATLAB GUI 自动调整大小时遇到问题。 在网上彻底搜索帮助和大量测试后,我找不到解决方案。

我一直在我的笔记本电脑(屏幕尺寸/分辨率 = 1366x768)中开发一个简单的 GUI(使用 MATLAB,不使用 GUIDE)。一个非常简化的版本如下所示:

GUI displayed in the laptop

当我在桌面计算机(屏幕尺寸/分辨率 = 1920x1080)中运行相同的 GUI 时,它显示如下:

GUI displayed in the desktop

GUI 的尺寸会根据屏幕尺寸自动初始化(代码在本文底部提供)。如您所见(用红色箭头突出显示),组件之间的字体/间距不会自动调整大小,因此无论我们在何处运行文件,GUI 都具有相同的外观。

此外,当手动调整 GUI 大小时,会出现一些组件重叠:

GUI displayed in the desktop - After manual resizing


用于这个最小工作示例的代码如下:

function resizingGUIexample()

%% SET UP GUI
hdl.mainfig = figure(); 

% MANAGE FIGURE DIMENSIONS -------------------------------------------------------------------------------------
set(hdl.mainfig, 'Units', 'pixels');
dims              = get(0, 'ScreenSize');
screenHeight      = dims(4);
verticalMargins   = floor((0.2*screenHeight)/2);          % =10% of the screen height in each side
figureHeight      =       (0.8*screenHeight);
figureWidth       =       (0.8*screenHeight)*(4/3);       % 4/3 Aspect Ratio
set(hdl.mainfig, 'Position', [0, verticalMargins, ... 
                figureWidth, figureHeight]);

movegui(hdl.mainfig,'center')     % move GUI to center

color = get(hdl.mainfig,'Color'); % get background color to hide static texts, etc...

% AXES ---------------------------------------------------------------------------------------------------------
hdl.axes = axes('Parent',   hdl.mainfig,  ...
             'Units',   'Normalized', ...
          'Position',   [0.295 0.05 0.63 0.63*(4/3)]);

% PUSH BUTTONS -------------------------------------------------------------------------------------------------
hdl.donePB = uicontrol(hdl.mainfig,                          ...
                  'Position',   [0.85 0.91 0.075 0.075], ...
                    'String',   'Done',                  ...
                  'Fontsize',   16,                      ...
                     'Units',   'normalized',            ...
                'FontWeight',   'Bold');

% BUTTON GROUP and RADIO BUTTONS -------------------------------------------------------------------------------
hdl.buttonGroup = uibuttongroup('Parent',    hdl.mainfig,  ...
                          'FontSize',    16,           ...
                        'FontWeight',    'Bold',       ...
                   'BackgroundColor',    color,        ...
                             'Units',    'Normalized', ... 
                          'Position',    [0.05 0.69 0.2 0.2]);
titleBG = sprintf('Intensity\nNormalization');
set(hdl.buttonGroup, 'Title', titleBG);

hdl.VolumeRB = uicontrol(hdl.buttonGroup,                   ...
                             'Style',    'radiobutton', ...
                            'String',    'Volume',      ...
                          'FontSize',    14,            ...
                        'FontWeight',    'Bold',        ...
                             'Units',    'normalized',  ...
                   'BackgroundColor',    color,         ...
                          'Position',    [0.1 0.67 0.8 0.3]);

hdl.SliceRB = uicontrol(hdl.buttonGroup,                   ...
                            'Style',    'radiobutton', ...
                           'String',    'Slice',       ...
                         'FontSize',    14,            ...
                       'FontWeight',    'Bold',        ...
                            'Units',    'normalized',  ...
                  'BackgroundColor',    color,         ...
                         'Position',    [0.1 .25 0.8 0.3]);

end

关于如何解决这些问题有什么想法吗?

非常感谢。

亲切的问候,

法比奥·内里

EDIT1:我也非常愿意接受有关初始化 GUI 尺寸的更好方法和策略的建议,以避免在不同的显示器/屏幕分辨率下运行 GUI 时出现问题。

最佳答案

首先,没有使用 GUIDE 做得很好 - 您已经通过了第一个测试:)

我强烈建议您查看并使用 Ben Tordoff 的 GUI Layout Toolbox .虽然您可以使用 ResizeFcn 属性执行此类操作,但我可以告诉您,使用 GUI 布局工具箱会更容易,它只会为您处理这些事情。

管理可能在不同尺寸和分辨率的不同(可能是多个)显示器上运行的 GUI 是一件痛苦的事情。我建议预先指定您要支持的尺寸/分辨率范围,并坚持这一点(即使应用程序发现自己处于不受支持的设置时也会出错),而不是试图完全通用。如果即使在最低公分母设置上也必须使一切正常,那么您可能不得不在更正常的设置上牺牲我们的易用性。

您似乎发现了 get(0, 'ScreenSize')movegui 命令。想到的其他有用的东西是 get(0, 'MonitorPositions')get(0, 'ScreenPixelsPerInch') 和使用 OuterPosition 而不是图形的 Position 属性。

希望对您有所帮助!

关于MATLAB:自动调整 GUI 组件/字体的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16057757/

相关文章:

user-interface - 像 flutter 一样的 Google Calendar UI 设计

python - django aws s3图像在上传时调整大小并访问各种调整大小的图像

MATLAB:我可以以某种方式使用函数作为第一类对象吗?

matlab - 如何复制 MATLAB 图形并将其粘贴到 Word 文档中?

ios - Swift 中带有动画转场的漂亮用户界面

iphone - 我可以更改 UIPopover 动画 anchor 吗?

jquery div resizer 像 stackoverflow 一样

matlab - Matlab 中的 Pearson 系数和协方差计算

matlab - 在Matlab中判断一幅图像是否为灰度

java - 如何使 JPanel 透明?