【问题标题】:How to make a simple borderless button in MATLAB GUI如何在 MATLAB GUI 中制作一个简单的无边框按钮
【发布时间】:2015-09-18 19:09:38
【问题描述】:

很简单,我正在尝试在 MATLAB GUI 中创建一个无边框按钮。原因主要是美学,所以没有必要争论为什么它应该是无边界的。

我已经知道仅使用内置的 MATLAB uicontrol 无法做到这一点,因为按钮的边框在 MATLAB 中不是可访问的属性。因此,必须访问底层 JAVA 代码(在其上编写 MATLAB)才能操作边框。这就是我迷路的地方,因为我只在 MATLAB 中编程过。

我从这里跟随了一个例子: http://undocumentedmatlab.com/blog/borderless-button-used-for-plot-properties

但我仍然没有得到无边框按钮。

这是一个简单的代码示例(注意使用 Yair Altman 的 findjobj,它可在 MATLAB 文件交换中找到):

f=figure('Menubar','none', 'Position',[200 200 300 200]);
p=uipanel(f, 'BackgroundColor', [0 0 1]);
h = uicontrol('parent', p, ...
'Style','pushbutton', ...
'String','click', ...
'TooltipString', 'you should click this' ...
'Units','normalized', ...
'Position',[0.3 0.3 0.5 0.5], ...
'BackgroundColor', [0 0 1]);
jh = findjobj(h);
jh.setBorder(javax.swing.BorderFactory.createEmptyBorder()); 
%attempt 1 does not remove border
jh.border=[];                                                
%attempt 2 does not remove border
jh.setBorder([]);                                            
%attempt 3 does not remove border
jh.border=javax.swing.BorderFactory.createEmptyBorder();     
%attempt 4 does not remove border

关于我哪里出错了有什么想法吗?谢谢!

【问题讨论】:

  • 有趣的问题,但 MATLAB GUIaesthetics 不应该用于同一段;)

标签: matlab user-interface button border borderless


【解决方案1】:

你应该添加两行:

jh.setBorderPainted(false);    
jh.setContentAreaFilled(false);

【讨论】:

    【解决方案2】:

    我不清楚你所说的“无边界”是什么意思。

    查看您发布的网页上的示例,我假设您正在寻找类似“隐形”按钮的东西。

    如果是这样,您可以考虑这种替代方式:

    • 如果你有一个按钮,你可能有一个static text uicontrol
    • 使其backgroundcolor 与 GUI 背景颜色相同(它将变为“不可见”且没有任何边框)
    • 不要在static text uicontrol中设置任何string
    • static text uicontrolenable属性设置为off
    • static text uicontrol 定义ButtonDownFcn
    • 通过按下ButtonDownFcn 中的按钮来编写您想要执行的代码

    当你在“隐形”static text uicontrol上按下鼠标按钮时,它的ButtonDownFcn会被执行。

    你只需要记住......“隐形”static text uicontrol 是。

    希望这会有所帮助。

    【讨论】:

    • 感谢您的反馈。无边框我的意思是它看起来就像漂浮在背景上的文本,没有任何矩形包围文本。我确实考虑过使用静态文本 uicontrol 来创建这种无边界外观。静态文本 uicontrol 的问题在于它给我带来了新问题。静态文本会自动对齐到其矩形空间的顶部(而不是中心)。这仍然需要对 Java 句柄对象进行编辑,因为垂直文本对齐在 MATLAB 中不是可访问的 uicontrol 属性。
    【解决方案3】:

    边框受飞越外观功能的影响。 http://undocumentedmatlab.com/blog/undocumented-button-highlighting 你需要添加

    jh.setFlyOverAppearance(true);
    

    为我工作。

    【讨论】:

      猜你喜欢
      • 2016-12-16
      • 2019-07-12
      • 2021-01-16
      • 2012-11-04
      • 1970-01-01
      • 2020-04-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多