问题描述
如何使用MATLAB GUIDE控件在GUI中显示文本文件的内容?文本文件可能非常长或非常宽,因此它应该具有垂直和水平滚动条.
How can a MATLAB GUIDE control be used to display the contents of a text file in a GUI? The text file may be very long or very wide so it should have the ability to have vertical and horizontal scroll bars.
推荐答案
多行编辑框可能是显示文本的最佳选择.示例:
A multi-line editbox may be the best choice to display the text. Example:
%# read text file lines as cell array of strings
fid = fopen( fullfile(matlabroot,'license.txt') );
str = textscan(fid, '%s', 'Delimiter','\n'); str = str{1};
fclose(fid);
%# GUI with multi-line editbox
hFig = figure('Menubar','none', 'Toolbar','none');
hPan = uipanel(hFig, 'Title','Display window', ...
'Units','normalized', 'Position',[0.05 0.05 0.9 0.9]);
hEdit = uicontrol(hPan, 'Style','edit', 'FontSize',9, ...
'Min',0, 'Max',2, 'HorizontalAlignment','left', ...
'Units','normalized', 'Position',[0 0 1 1], ...
'String',str);
%# enable horizontal scrolling
jEdit = findjobj(hEdit);
jEditbox = jEdit.getViewport().getComponent(0);
jEditbox.setWrapping(false); %# turn off word-wrapping
jEditbox.setEditable(false); %# non-editable
set(jEdit,'HorizontalScrollBarPolicy',30); %# HORIZONTAL_SCROLLBAR_AS_NEEDED
%# maintain horizontal scrollbar policy which reverts back on component resize
hjEdit = handle(jEdit,'CallbackProperties');
set(hjEdit, 'ComponentResizedCallback',...
'set(gcbo,''HorizontalScrollBarPolicy'',30)')
要启用水平滚动,我们必须获取嵌入式JScrollPane Java组件的句柄.我正在使用出色的 FINDJOBJ 函数.然后,如中所述,将HorizontalScrollBarPolicy
属性设置为javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
(= 30).发布.我还禁用了文本的编辑(只读).
To enable horizontal scrolling, we must get a handle to the embedded JScrollPane java component. I am using the excellent FINDJOBJ function. Then we set the HorizontalScrollBarPolicy
property to javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED
(= 30) as explained in this post. I also disabled editing of the text (read only).
这篇关于在MATLAB GUIDE中显示大文本文件的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!