【问题标题】:Aligning text and edit uicontrols对齐文本和编辑 uicontrols
【发布时间】:2016-03-08 19:45:02
【问题描述】:

我正在创建 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 字段的基线。

如何对齐这些字段?

【问题讨论】:

  • 要将text uicontrol 中的文本垂直居中,您需要直接访问底层Java。 This post 显示了一般方法。修改edit uicontrol 的背景颜色没有影响,MATLAB 在控件启用时强制颜色为白色。
  • @excaza 您想将其扩展为答案吗?

标签: matlab formatting alignment uicontrol


【解决方案1】:

不幸的是,这需要访问底层 Java。该方法类似于Amro's approach herepushbutton 并利用外部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)

这给了我们:


编辑:修改编辑框的BackgroundColor属性没有任何效果(虽然设置为none使它成为一个黑框...),框将保持默认颜色。根据 The MathWorks,这是design decision

这是 MATLAB 显示 BackgroundColor 用于可编辑的文本对象。

这也很可能通过利用底层 Java 来更新,但我不熟悉。

【讨论】:

    【解决方案2】:

    另一个选择是使用 Jlabel,它是一个 Java 组件。我发现 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:

    【讨论】:

      猜你喜欢
      • 2011-12-16
      • 1970-01-01
      • 2013-07-16
      • 2013-09-10
      • 1970-01-01
      • 2021-01-24
      • 2020-07-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多