【发布时间】:2013-04-17 10:33:26
【问题描述】:
我在尝试使我的 MATLAB GUI自动调整大小时遇到问题。 在彻底搜索网络寻求帮助和大量测试后,我找不到解决方案。
我一直在我的笔记本电脑(屏幕尺寸/分辨率 = 1366x768)中开发一个简单的 GUI(使用 MATLAB,不使用 GUIDE)。一个非常简化的版本如下所示:
当我在我的台式计算机上运行相同的 GUI(屏幕尺寸/分辨率 = 1920x1080)时,它显示如下:
GUI 的尺寸会根据屏幕尺寸自动初始化(代码在本文底部提供)。如您所见(由红色箭头突出显示),组件之间的字体/间距不会自动调整大小,因此无论我们在何处运行文件,GUI 都具有相同的外观。
此外,当手动调整 GUI 大小时,会出现一些组件重叠:
用于这个最小工作示例的代码如下:
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 时出现问题的策略的建议。
【问题讨论】:
标签: matlab user-interface resize screen-resolution