簡體   English   中英

對齊文本和編輯uicontrols

[英]Aligning text and edit uicontrols

我正在創建GUI,我希望在那里有輸入和結果。

我有text字段作為標簽, editpopup輸入。 當我有

uicontrol('style','text','string','foo','units','centimeters','position',[1 1 1 0.5]);
uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 0.5]);

我得到foo字符串略微錯位, text字段的基線略高於edit / pop字段的基線。

我該如何對齊這些字段?

不幸的是,這需要訪問底層Java。 該方法類似於Amro的 pushbutton 方法 ,並使用外部findjobj函數:

h.t = uicontrol('style','text','string','foo','units','centimeters','position',[1 1 1 0.5]);
h.e = uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 0.5]);
jh = findjobj(h.t);
jh.setVerticalAlignment(javax.swing.JLabel.CENTER)

不幸的是,這仍然是一兩個像素:

好極了

我會說必要時只是逐個像素地碰撞文本:

oldunits = get(h.t, 'Units');
set(h.t, 'Units', 'Pixels');
pos = get(h.t, 'Position');

pos(2) = pos(2) + 1;

set(h.t, 'Position', pos)
set(h.t, 'Units', oldunits)

這給了我們:

yay2


編輯:修改編輯框的BackgroundColor屬性沒有任何效果(雖然將其設置為none使其成為黑盒...),該框將保持默認顏色。 根據MathWorks,這是一個設計決定

這是MATLAB為可編輯文本對象顯示BackgroundColor的方式中的預期行為。

這也可能最有可能通過利用底層Java來更新,但我不熟悉。

另一個選擇是使用Jlabel,它是一個Javacomponent。 我發現來自undocumentedMatlab的uicomponent很有幫助。 您可以使用以下鏈接從Mathworks File Exchange下載它: https//uk.mathworks.com/matlabcentral/fileexchange/14583-uicomponent-expands-uicontrol-to-all-java-classes

看這個例子:

figure(1);clf;

[txt1, txt2] = uicomponent(gcf, 'style','JLabel'); % simple spinner with initial value
set(txt1, 'Text', 'Test', 'units','centimeters','position',[1 1 1 1]);
set(txt2, 'horizontalAlignment', javax.swing.SwingConstants.LEFT);
set(txt2, 'VerticalAlignment', javax.swing.SwingConstants.CENTER);

uicontrol('style','edit','string','foo','units','centimeters','position',[2 1 1 1]);

它可以創建如下的UI:

在此輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM