uicontrol

时间:2017-06-15 06:29:35

标签: matlab user-interface callback uicontrol

我的GUI有两个编辑框(uicontrol),我想通过左键单击来改变它们的背景颜色。对于鼠标左键单击,ButtonDownFcn仅在uicontrol Enable属性设置为' inactive'或者'关闭',所以我切换属性以使其工作。

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

到目前为止,这是我的代码。 (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

1 个答案:

答案 0 :(得分:1)

You can use an undocumented feature of the uicontrols to set appropriate action when the moue focus is achieved.

This is done by finding the underlying java object and setting the appropriate callback.

The java object is found using "findjobj" function which you can download from the Mathworks FEX

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