【问题标题】:ButtonDownFcn on uicontroluicontrol 上的 ButtonDownFcn
【发布时间】:2017-11-17 12:20:39
【问题描述】:

我的 GUI 有两个编辑框 (uicontrol),我想通过左键单击来更改它们的背景颜色。对于鼠标左键单击,ButtonDownFcn 仅在 uicontrol Enable 属性设置为“inactive”或“off”时才有效,因此我切换该属性以使其工作。

通过按 Tab 键,我希望我的编辑框将其背景颜色重新初始化为白色,并更改下一个编辑框的背景颜色。问题是按下tab键,焦点不会改变,因为uicontrol Enable属性是'off'或'inactive'。 有解决办法吗?

到目前为止,这是我的代码。 (edit1和edit2代码相同)

function edit1_ButtonDownFcn(hObject, eventdata, handles)
set(hObject, 'Enable', 'on', 'BackgroundColor', [0.5,1,0.7]) % change enable and background color properties
uicontrol(hObject) % focus on the current object

function edit1_Callback(hObject, eventdata, handles)
set(hObject, 'Enable', 'inactive', 'BackgroundColor', [1 1 1]) % reinitialize the edit box

【问题讨论】:

    标签: matlab user-interface callback uicontrol


    【解决方案1】:

    当鼠标获得焦点时,您可以使用 uicontrols 的未记录功能来设置适当的操作。

    这是通过查找底层 java 对象并设置适当的回调来完成的。

    使用"findjobj" function which you can download from the Mathworks FEX找到java对象

    function test
      %% Create the figure and uicontols
      hFig = figure;
      uic(1) = uicontrol ( 'style', 'edit', 'position', [100 300 200 50], 'parent', hFig );
      uic(2) = uicontrol ( 'style', 'edit', 'position', [100 200 200 50], 'parent', hFig );
      % for each of the uicontrols find the java object and set the FocusGainedCallback
      for ii=1:2
        jObj = findjobj ( uic(ii) );
        set(jObj,'FocusGainedCallback', @(a,b)gainFocus( hFig, uic, ii ));
      end
      % set the defaults.
      gainFocus( hFig, uic, 1 );
    end
    function gainFocus( hFig, uic, uicIndex )
      switch uicIndex
        case 1
          index = [1 2];
        case 2
          index = [2 1];
      end
      uic(index(1)).BackgroundColor = [1  1. 1];
      uic(index(2)).BackgroundColor = [0.5 1. 0.7];
    end
    

    【讨论】:

    • 谢谢,它运行良好。只是一个细节:gainFocus 函数 (hFig) 的第一个参数没有被使用,所以它可以被移除。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 1970-01-01
    相关资源
    最近更新 更多