首页 > 解决方案 > Matlab指南GUI工具/按钮?

问题描述

我已经在 matlab GUI 上工作了一段时间,终于让它做我想做的事情,现在我只是在努力让它变得更简洁和用户友好。我有 5 个面板并将它们全部堆叠在一起,并在面板顶部添加了 5 个按钮。有没有办法通过按下按钮将面板带到前面?

function togglebutton2_Callback(hObject, eventdata, handles)

这是我对按钮的回电。有什么我可以通过单击使面板显示在前面的添加内容吗?谢谢

标签: matlabuser-interface

解决方案


有许多工具/示例可用于创建已提交到MathWorks 文件交换的选项卡式面板。他们中的任何一个都可能会做你想做的事。但是,您也可以使用uibuttongroup. 这是一个选项卡式面板设计的简单示例:

function tab_panel

  hFigure = figure('Units', 'pixels', 'Position', [200 200 320 320]);

  hPanel1 = uipanel(hFigure, 'Units', 'pixels', 'Position', [10 10 300 270], ...
                    'BackgroundColor', 'r');
  hPanel2 = uipanel(hFigure, 'Units', 'pixels', 'Position', [10 10 300 270], ...
                    'BackgroundColor', 'g', 'Visible', 'off');
  hPanel3 = uipanel(hFigure, 'Units', 'pixels', 'Position', [10 10 300 270], ...
                    'BackgroundColor', 'b', 'Visible', 'off');

  hGroup = uibuttongroup(hFigure, 'Units', 'pixels', 'Position', [10 280 300 30], ...
                         'BorderType', 'none', 'SelectionChangedFcn', @switch_panel);
  hButton1 = uicontrol(hGroup, 'Style', 'toggle', 'Position', [0 0 40 30], ...
                       'String', 'Red');
  hButton2 = uicontrol(hGroup, 'Style', 'toggle', 'Position', [40 0 40 30], ...
                       'String', 'Green');
  hButton3 = uicontrol(hGroup, 'Style', 'toggle', 'Position', [80 0 40 30], ...
                       'String', 'Blue');

  function switch_panel(~, eventData)  % Nested callback function
    switch eventData.NewValue
      case hButton1
        set(hPanel1, 'Visible', 'on');
        set([hPanel2 hPanel3], 'Visible', 'off');
      case hButton2
        set(hPanel2, 'Visible', 'on');
        set([hPanel1 hPanel3], 'Visible', 'off');
      case hButton3
        set(hPanel3, 'Visible', 'on');
        set([hPanel1 hPanel2], 'Visible', 'off');
    end
  end

end

这将创建三个红色、绿色和蓝色的面板,以及三个切换按钮。回调由嵌套函数处理。按下按钮将通过打开其可见性同时关闭所有其他面板的可见性来显示相应的彩色面板。

在此处输入图像描述

尽管这是一个编程示例,但应该很容易将其调整为 GUIDE 设计的 GUI。


推荐阅读